blob: b466d02127cbe4a24db6a8f2d81247e4f887412d [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
Greg Wardabc52162000-02-26 00:52:48 +000013\author{Greg Ward}
Andrew M. Kuchling55fa3bb2003-03-04 19:36:11 +000014\authoraddress{Email: \email{distutils-sig@python.org}}
Greg Wardabc52162000-02-26 00:52:48 +000015
Greg Warde3cca262000-08-31 16:36:31 +000016\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000017
Greg Wardabc52162000-02-26 00:52:48 +000018\begin{document}
19
Greg Wardfacb8db2000-04-09 04:32:40 +000020\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000021\begin{abstract}
22 \noindent
23 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000024 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000025 how to use the Distutils to make Python modules and extensions easily
26 available to a wider audience with very little overhead for
27 build/release/install mechanics.
28\end{abstract}
29
Fred Drakea09262e2001-03-01 18:35:43 +000030% The ugly "%begin{latexonly}" pseudo-environment supresses the table
31% of contents for HTML generation.
32%
33%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000034\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000035%end{latexonly}
36
Greg Ward16aafcd2000-04-09 04:06:44 +000037
38\section{Introduction}
Greg Warde78298a2000-04-28 17:12:24 +000039\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000040
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000041This document covers using the Distutils to distribute your Python
42modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000043you're looking for information on installing Python modules, you
44should refer to the \citetitle[../inst/inst.html]{Installing Python
45Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000046
47
Greg Wardfacb8db2000-04-09 04:32:40 +000048\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000049\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000050
51Using the Distutils is quite simple, both for module developers and for
52users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000053your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000054well-tested code, of course!) are:
55\begin{itemize}
56\item write a setup script (\file{setup.py} by convention)
57\item (optional) write a setup configuration file
58\item create a source distribution
59\item (optional) create one or more built (binary) distributions
60\end{itemize}
61Each of these tasks is covered in this document.
62
63Not all module developers have access to a multitude of platforms, so
64it's not always feasible to expect them to create a multitude of built
65distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000066\emph{packagers}, will arise to address this need. Packagers will take
67source distributions released by module developers, build them on one or
68more platforms, and release the resulting built distributions. Thus,
69users on the most popular platforms will be able to install most popular
70Python module distributions in the most natural way for their platform,
71without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000072
73
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000074\subsection{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000075\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000076
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000077The setup script is usually quite simple, although since it's written
78in Python, there are no arbitrary limits to what you can do with it,
79though you should be careful about putting arbitrarily expensive
80operations in your setup script. Unlike, say, Autoconf-style configure
81scripts, the setup script may be run multiple times in the course of
82building and installing your module distribution. If you need to
83insert potentially expensive processing steps into the Distutils
84chain, see section~\ref{extending} on extending the Distutils.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000085
86If all you want to do is distribute a module called \module{foo},
87contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000088simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000089
Greg Ward16aafcd2000-04-09 04:06:44 +000090\begin{verbatim}
91from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000092setup(name="foo",
93 version="1.0",
94 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000095\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000096
Greg Ward16aafcd2000-04-09 04:06:44 +000097Some observations:
98\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +000099\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000100 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000101\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000102 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000103 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000104\item modules are specified by module name, not filename (the same will
105 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000106\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000107 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000108 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000109\end{itemize}
110
Greg Ward370248d2000-06-24 01:45:47 +0000111To create a source distribution for this module, you would create a
112setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000113
Greg Ward16aafcd2000-04-09 04:06:44 +0000114\begin{verbatim}
115python setup.py sdist
116\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000117
Fred Drakeeff9a872000-10-26 16:41:03 +0000118which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000119Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000120\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
121\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000122
123If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000124to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
125and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000126
Greg Ward16aafcd2000-04-09 04:06:44 +0000127\begin{verbatim}
128python setup.py install
129\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000130
Greg Ward16aafcd2000-04-09 04:06:44 +0000131which will ultimately copy \file{foo.py} to the appropriate directory
132for third-party modules in their Python installation.
133
134This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000135Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000136user interface, i.e. the setup script. The difference is which
137Distutils \emph{commands} they use: the \command{sdist} command is
138almost exclusively for module developers, while \command{install} is
139more often for installers (although most developers will want to install
140their own code occasionally).
141
Greg Ward16aafcd2000-04-09 04:06:44 +0000142If you want to make things really easy for your users, you can create
143one or more built distributions for them. For instance, if you are
144running on a Windows machine, and want to make things easy for other
145Windows users, you can create an executable installer (the most
146appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000147\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000148
Greg Ward16aafcd2000-04-09 04:06:44 +0000149\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000150python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000151\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000152
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000153will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000154current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000155
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000156Other useful built distribution formats are RPM, implemented by the
157\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000158(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
159(\command{bdist_sdux}). For example, the following command will
160create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000161
Greg Ward1d8f57a2000-08-05 00:43:11 +0000162\begin{verbatim}
163python setup.py bdist_rpm
164\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000165
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000166(The \command{bdist\_rpm} command uses the \command{rpm} executable,
167therefore this has to be run on an RPM-based system such as Red Hat
168Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000169
170You can find out what distribution formats are available at any time by
171running
Fred Drakea09262e2001-03-01 18:35:43 +0000172
Greg Ward1d8f57a2000-08-05 00:43:11 +0000173\begin{verbatim}
174python setup.py bdist --help-formats
175\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000176
177
178\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000179\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000180
181If you're reading this document, you probably have a good idea of what
182modules, extensions, and so forth are. Nevertheless, just to be sure
183that everyone is operating from a common starting point, we offer the
184following glossary of common Python terms:
185\begin{description}
186\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000187 code imported by some other code. Three types of modules concern us
188 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000189
Greg Ward16aafcd2000-04-09 04:06:44 +0000190\item[pure Python module] a module written in Python and contained in a
191 single \file{.py} file (and possibly associated \file{.pyc} and/or
192 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000193
Greg Ward16aafcd2000-04-09 04:06:44 +0000194\item[extension module] a module written in the low-level language of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000195 the Python implementation: C/C++ for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000196 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000197 file, e.g. a shared object (\file{.so}) file for Python extensions on
198 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000199 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drakeeff9a872000-10-26 16:41:03 +0000200 currently, the Distutils only handles C/C++ extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000201
Greg Ward16aafcd2000-04-09 04:06:44 +0000202\item[package] a module that contains other modules; typically contained
203 in a directory in the filesystem and distinguished from other
204 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000205
Greg Ward6153fa12000-05-26 02:24:28 +0000206\item[root package] the root of the hierarchy of packages. (This isn't
207 really a package, since it doesn't have an \file{\_\_init\_\_.py}
208 file. But we have to call it something.) The vast majority of the
209 standard library is in the root package, as are many small, standalone
210 third-party modules that don't belong to a larger module collection.
211 Unlike regular packages, modules in the root package can be found in
212 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000213 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000214\end{description}
215
216
217\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000218\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000219
220The following terms apply more specifically to the domain of
221distributing Python modules using the Distutils:
222\begin{description}
223\item[module distribution] a collection of Python modules distributed
224 together as a single downloadable resource and meant to be installed
225 \emph{en masse}. Examples of some well-known module distributions are
226 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000227 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000228 is already taken in the Python context: a single module distribution
229 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000230
Greg Ward16aafcd2000-04-09 04:06:44 +0000231\item[pure module distribution] a module distribution that contains only
232 pure Python modules and packages. Sometimes referred to as a ``pure
233 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000234
Greg Ward16aafcd2000-04-09 04:06:44 +0000235\item[non-pure module distribution] a module distribution that contains
236 at least one extension module. Sometimes referred to as a ``non-pure
237 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000238
Greg Ward16aafcd2000-04-09 04:06:44 +0000239\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000240 source distribution); the directory where \file{setup.py} exists. Generally
241 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000242\end{description}
243
244
245\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000246\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000247
248The setup script is the centre of all activity in building,
249distributing, and installing modules using the Distutils. The main
250purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000251the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000252do the right thing. As we saw in section~\ref{simple-example} above,
253the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000254most information supplied to the Distutils by the module developer is
255supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000256
257Here's a slightly more involved example, which we'll follow for the next
258couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000259although the Distutils are included with Python 1.6 and later, they also
260have an independent existence so that Python 1.5.2 users can use them to
261install other module distributions. The Distutils' own setup script,
262shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000263
264\begin{verbatim}
265#!/usr/bin/env python
266
267from distutils.core import setup
268
Fred Drakea09262e2001-03-01 18:35:43 +0000269setup(name="Distutils",
270 version="1.0",
271 description="Python Distribution Utilities",
272 author="Greg Ward",
273 author_email="gward@python.net",
274 url="http://www.python.org/sigs/distutils-sig/",
275 packages=['distutils', 'distutils.command'],
276 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000277\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000278
Greg Ward16aafcd2000-04-09 04:06:44 +0000279There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000280distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000281metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000282rather than by module. This is important since the Distutils consist of
283a couple of dozen modules split into (so far) two packages; an explicit
284list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000285maintain. For more information on the additional meta-data, see
286section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000287
Greg Ward46b98e32000-04-14 01:53:36 +0000288Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000289script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000290slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000291platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000292current platform before actually using the pathname. This makes your
293setup script portable across operating systems, which of course is one
294of the major goals of the Distutils. In this spirit, all pathnames in
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000295this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000296mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000297path, the opposite of the MacOS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000298
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000299This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000300functions. If you, for example, use standard Python functions such as
301\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000302should be careful to write portable code instead of hardcoding path
303separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000304
Thomas Heller5f52f722001-02-19 17:48:03 +0000305\begin{verbatim}
306 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
307 os.listdir(os.path.join('mydir', 'subdir'))
308\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000309
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000310
Greg Ward2afffd42000-08-06 20:37:24 +0000311\subsection{Listing whole packages}
312\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000313
314The \option{packages} option tells the Distutils to process (build,
315distribute, install, etc.) all pure Python modules found in each package
316mentioned in the \option{packages} list. In order to do this, of
317course, there has to be a correspondence between package names and
318directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000319obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000320\file{distutils} relative to the distribution root. Thus, when you say
321\code{packages = ['foo']} in your setup script, you are promising that
322the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
323be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000324the directory where your setup script lives. If you break this
325promise, the Distutils will issue a warning but still process the broken
326package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000327
328If you use a different convention to lay out your source directory,
329that's no problem: you just have to supply the \option{package\_dir}
330option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000331you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000332``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000333\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
334and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000335
Greg Ward16aafcd2000-04-09 04:06:44 +0000336\begin{verbatim}
337package_dir = {'': 'lib'}
338\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000339
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000340in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000341and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000342directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000343you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000344\file{lib/foo/\_\_init\_\_.py} exists.
345
Greg Ward1ecc2512000-04-19 22:36:24 +0000346Another possible convention is to put the \module{foo} package right in
347\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000348would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000349
Greg Ward16aafcd2000-04-09 04:06:44 +0000350\begin{verbatim}
351package_dir = {'foo': 'lib'}
352\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000353
Greg Ward59d382e2000-05-26 01:04:47 +0000354A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
355dictionary implicitly applies to all packages below \var{package}, so
356the \module{foo.bar} case is automatically handled here. In this
357example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
358to look for \file{lib/\_\_init\_\_.py} and
359\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
360\option{package\_dir} applies recursively, you must explicitly list all
361packages in \option{packages}: the Distutils will \emph{not} recursively
362scan your source tree looking for any directory with an
363\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000364
365
366\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000367\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000368
369For a small module distribution, you might prefer to list all modules
370rather than listing packages---especially the case of a single module
371that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000372simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000373slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000374
Greg Ward16aafcd2000-04-09 04:06:44 +0000375\begin{verbatim}
376py_modules = ['mod1', 'pkg.mod2']
377\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000378
Greg Ward16aafcd2000-04-09 04:06:44 +0000379This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000380other in the \module{pkg} package. Again, the default package/directory
381layout implies that these two modules can be found in \file{mod1.py} and
382\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000383And again, you can override the package/directory correspondence using
384the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000385
386
387\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000388\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000389
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000390% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000391Just as writing Python extension modules is a bit more complicated than
392writing pure Python modules, describing them to the Distutils is a bit
393more complicated. Unlike pure modules, it's not enough just to list
394modules or packages and expect the Distutils to go out and find the
395right files; you have to specify the extension name, source file(s), and
396any compile/link requirements (include directories, libraries to link
397with, etc.).
398
399All of this is done through another keyword argument to
400\function{setup()}, the \option{extensions} option. \option{extensions}
401is just a list of \class{Extension} instances, each of which describes a
402single extension module. Suppose your distribution includes a single
403extension, called \module{foo} and implemented by \file{foo.c}. If no
404additional instructions to the compiler/linker are needed, describing
405this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000406
Greg Ward2afffd42000-08-06 20:37:24 +0000407\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000408uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000409\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000410
Greg Ward2afffd42000-08-06 20:37:24 +0000411The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000412\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000413script for a module distribution that contains only this one extension
414and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000415
Greg Ward2afffd42000-08-06 20:37:24 +0000416\begin{verbatim}
417from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000418setup(name="foo", version="1.0",
419 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000420\end{verbatim}
421
422The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000423machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000424great deal of flexibility in describing Python extensions, which is
425explained in the following sections.
426
427
428\subsubsection{Extension names and packages}
429
430The first argument to the \class{Extension} constructor is always the
431name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000432
Greg Ward2afffd42000-08-06 20:37:24 +0000433\begin{verbatim}
434Extension("foo", ["src/foo1.c", "src/foo2.c"])
435\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000436
Greg Ward2afffd42000-08-06 20:37:24 +0000437describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000438
Greg Ward2afffd42000-08-06 20:37:24 +0000439\begin{verbatim}
440Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
441\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000442
Greg Ward2afffd42000-08-06 20:37:24 +0000443describes the same extension in the \module{pkg} package. The source
444files and resulting object code are identical in both cases; the only
445difference is where in the filesystem (and therefore where in Python's
446namespace hierarchy) the resulting extension lives.
447
448If you have a number of extensions all in the same package (or all under
449the same base package), use the \option{ext\_package} keyword argument
450to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000451
Greg Ward2afffd42000-08-06 20:37:24 +0000452\begin{verbatim}
453setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000454 ext_package="pkg",
455 ext_modules=[Extension("foo", ["foo.c"]),
456 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000457 )
458\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000459
Greg Ward2afffd42000-08-06 20:37:24 +0000460will compile \file{foo.c} to the extension \module{pkg.foo}, and
461\file{bar.c} to \module{pkg.subpkg.bar}.
462
463
464\subsubsection{Extension source files}
465
466The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000467source files. Since the Distutils currently only support C, \Cpp, and
468Objective-C extensions, these are normally C/\Cpp/Objective-C source
469files. (Be sure to use appropriate extensions to distinguish \Cpp\
470source files: \file{.cc} and \file{.cpp} seem to be recognized by both
471\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000472
473However, you can also include SWIG interface (\file{.i}) files in the
474list; the \command{build\_ext} command knows how to deal with SWIG
475extensions: it will run SWIG on the interface file and compile the
476resulting C/C++ file into your extension.
477
478\XXX{SWIG support is rough around the edges and largely untested;
479 especially SWIG support of C++ extensions! Explain in more detail
480 here when the interface firms up.}
481
482On some platforms, you can include non-source files that are processed
483by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000484means Windows message text (\file{.mc}) files and resource definition
485(\file{.rc}) files for Visual C++. These will be compiled to binary resource
486(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000487
488
489\subsubsection{Preprocessor options}
490
491Three optional arguments to \class{Extension} will help if you need to
492specify include directories to search or preprocessor macros to
493define/undefine: \code{include\_dirs}, \code{define\_macros}, and
494\code{undef\_macros}.
495
496For example, if your extension requires header files in the
497\file{include} directory under your distribution root, use the
498\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000499
Greg Ward2afffd42000-08-06 20:37:24 +0000500\begin{verbatim}
501Extension("foo", ["foo.c"], include_dirs=["include"])
502\end{verbatim}
503
504You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000505extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000506\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000507
Greg Ward2afffd42000-08-06 20:37:24 +0000508\begin{verbatim}
509Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
510\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000511
Greg Ward2afffd42000-08-06 20:37:24 +0000512You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000513distribute your code: it's probably better to write C code like
514\begin{verbatim}
515#include <X11/Xlib.h>
516\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000517
518If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000519you can take advantage of the fact that header files are installed in a
520consistent way by the Distutils \command{install\_header} command. For
521example, the Numerical Python header files are installed (on a standard
522Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
523(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000524installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000525directory---\file{/usr/local/include/python1.5} in this case---is always
526included in the search path when building Python extensions, the best
527approach is to write C code like
528\begin{verbatim}
529#include <Numerical/arrayobject.h>
530\end{verbatim}
531If you must put the \file{Numerical} include directory right into your
532header search path, though, you can find that directory using the
533Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000534
Greg Ward2afffd42000-08-06 20:37:24 +0000535\begin{verbatim}
536from distutils.sysconfig import get_python_inc
537incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
538setup(...,
539 Extension(..., include_dirs=[incdir]))
540\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000541
Greg Ward2afffd42000-08-06 20:37:24 +0000542Even though this is quite portable---it will work on any Python
543installation, regardless of platform---it's probably easier to just
544write your C code in the sensible way.
545
546You can define and undefine pre-processor macros with the
547\code{define\_macros} and \code{undef\_macros} options.
548\code{define\_macros} takes a list of \code{(name, value)} tuples, where
549\code{name} is the name of the macro to define (a string) and
550\code{value} is its value: either a string or \code{None}. (Defining a
551macro \code{FOO} to \code{None} is the equivalent of a bare
552\code{\#define FOO} in your C source: with most compilers, this sets
553\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
554a list of macros to undefine.
555
556For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000557
Greg Ward2afffd42000-08-06 20:37:24 +0000558\begin{verbatim}
559Extension(...,
560 define_macros=[('NDEBUG', '1')],
561 ('HAVE_STRFTIME', None),
562 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
563\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000564
Greg Ward2afffd42000-08-06 20:37:24 +0000565is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000566
Greg Ward2afffd42000-08-06 20:37:24 +0000567\begin{verbatim}
568#define NDEBUG 1
569#define HAVE_STRFTIME
570#undef HAVE_FOO
571#undef HAVE_BAR
572\end{verbatim}
573
574
575\subsubsection{Library options}
576
577You can also specify the libraries to link against when building your
578extension, and the directories to search for those libraries. The
579\code{libraries} option is a list of libraries to link against,
580\code{library\_dirs} is a list of directories to search for libraries at
581link-time, and \code{runtime\_library\_dirs} is a list of directories to
582search for shared (dynamically loaded) libraries at run-time.
583
584For example, if you need to link against libraries known to be in the
585standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000586
Greg Ward2afffd42000-08-06 20:37:24 +0000587\begin{verbatim}
588Extension(...,
589 libraries=["gdbm", "readline"])
590\end{verbatim}
591
592If you need to link with libraries in a non-standard location, you'll
593have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000594
Greg Ward2afffd42000-08-06 20:37:24 +0000595\begin{verbatim}
596Extension(...,
597 library_dirs=["/usr/X11R6/lib"],
598 libraries=["X11", "Xt"])
599\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000600
Greg Ward2afffd42000-08-06 20:37:24 +0000601(Again, this sort of non-portable construct should be avoided if you
602intend to distribute your code.)
603
Thomas Heller5f52f722001-02-19 17:48:03 +0000604\XXX{Should mention clib libraries here or somewhere else!}
605
606\subsubsection{Other options}
607
608There are still some other options which can be used to handle special
609cases.
610
611The \option{extra\_objects} option is a list of object files to be passed
612to the linker. These files must not have extensions, as the default
613extension for the compiler is used.
614
615\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000616to specify additional command line options for the respective compiler and
617linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000618
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000619\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000620of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000621is not needed when building compiled extensions: Distutils
622will automatically add \code{initmodule}
623to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000624
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000625\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000626So far we have been dealing with pure and non-pure Python modules,
627which are usually not run by themselves but imported by scripts.
628
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000629Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000630started from the command line. Scripts don't require Distutils to do
631anything very complicated. The only clever feature is that if the
632first line of the script starts with \code{\#!} and contains the word
633``python'', the Distutils will adjust the first line to refer to the
634current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000635
636The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000637in this way. From the PyXML setup script:
638
639\begin{verbatim}
640setup (...
641 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
642 )
643\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000644
645
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000646\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000647
Thomas Heller5f52f722001-02-19 17:48:03 +0000648The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000649files needed by the module distribution: configuration files, message
650catalogs, data files, anything which doesn't fit in the previous
651categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000652
Fred Drake632bda32002-03-08 22:02:06 +0000653\option{data\_files} specifies a sequence of (\var{directory},
654\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000655
Thomas Heller5f52f722001-02-19 17:48:03 +0000656\begin{verbatim}
657setup(...
658 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000659 ('config', ['cfg/data.cfg']),
660 ('/etc/init.d', ['init-script'])]
661 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000662\end{verbatim}
663
664Note that you can specify the directory names where the data files
665will be installed, but you cannot rename the data files themselves.
666
Fred Drake632bda32002-03-08 22:02:06 +0000667Each (\var{directory}, \var{files}) pair in the sequence specifies the
668installation directory and the files to install there. If
669\var{directory} is a relative path, it is interpreted relative to the
670installation prefix (Python's \code{sys.prefix} for pure-Python
671packages, \code{sys.exec_prefix} for packages that contain extension
672modules). Each file name in \var{files} is interpreted relative to
673the \file{setup.py} script at the top of the package source
674distribution. No directory information from \var{files} is used to
675determine the final location of the installed file; only the name of
676the file is used.
677
Thomas Heller5f52f722001-02-19 17:48:03 +0000678You can specify the \option{data\_files} options as a simple sequence
679of files without specifying a target directory, but this is not recommended,
680and the \command{install} command will print a warning in this case.
681To install data files directly in the target directory, an empty
682string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000683
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000684\subsection{Additional meta-data}
685\label{meta-data}
686
687The setup script may include additional meta-data beyond the name and
688version. This information includes:
689
Fred Drakec440af52003-04-25 16:43:28 +0000690\begin{tableiv}{l|l|l|c}{code}%
691 {Meta-Data}{Description}{Value}{Notes}
692 \lineiv{name}{name of the package}
693 {short string}{(1)}
694 \lineiv{version}{version of this release}
695 {short string}{(1)(2)}
696 \lineiv{author}{package author's name}
697 {short string}{(3)}
698 \lineiv{author_email}{email address of the package author}
699 {email address}{(3)}
700 \lineiv{maintainer}{package maintainer's name}
701 {short string}{(3)}
702 \lineiv{maintainer_email}{email address of the package maintainer}
703 {email address}{(3)}
704 \lineiv{url}{home page for the package}
705 {URL}{(1)}
706 \lineiv{description}{short, summary description of the package}
707 {short string}{}
708 \lineiv{long_description}{longer description of the package}
709 {long string}{}
710 \lineiv{download_url}{location where the package may be downloaded}
711 {URL}{(4)}
712 \lineiv{classifiers}{a list of Trove classifiers}
713 {list of strings}{(4)}
714\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000715
716\noindent Notes:
717\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000718\item[(1)] These fields are required.
719\item[(2)] It is recommended that versions take the form
720 \emph{major.minor\optional{.patch\optional{.sub}}}.
721\item[(3)] Either the author or the maintainer must be identified.
722\item[(4)] These fields should not be used if your package is to be
723 compatible with Python versions prior to 2.2.3 or 2.3. The list is
724 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
725
726\item["short string"] A single line of text, not more than 200 characters.
727\item["long string"] Multiple lines of plain text in ReStructuredText
728 format (see \url{http://docutils.sf.net/}).
729\item["list of strings"] See below.
730\end{description}
731
732None of the string values may be Unicode.
733
734Encoding the version information is an art in itself. Python packages
735generally adhere to the version format
736\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7370 for
738initial, experimental releases of software. It is incremented for
739releases that represent major milestones in a package. The minor
740number is incremented when important new features are added to the
741package. The patch number increments when bug-fix releases are
742made. Additional trailing version information is sometimes used to
743indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
744where functionality and API may change), "b1,b2,...,bN" (for beta
745releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
746pre-release release testing). Some examples:
747
748\begin{description}
749\item[0.1.0] the first, experimental release of a package
750\item[1.0.1a2] the second alpha release of the first patch version of 1.0
751\end{description}
752
753\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000754
755\begin{verbatim}
756setup(...
Fred Drakec440af52003-04-25 16:43:28 +0000757 classifiers = [
Fred Drake2a046232003-03-31 16:23:09 +0000758 'Development Status :: 4 - Beta',
759 'Environment :: Console',
760 'Environment :: Web Environment',
761 'Intended Audience :: End Users/Desktop',
762 'Intended Audience :: Developers',
763 'Intended Audience :: System Administrators',
764 'License :: OSI Approved :: Python Software Foundation License',
765 'Operating System :: MacOS :: MacOS X',
766 'Operating System :: Microsoft :: Windows',
767 'Operating System :: POSIX',
768 'Programming Language :: Python',
769 'Topic :: Communications :: Email',
770 'Topic :: Office/Business',
771 'Topic :: Software Development :: Bug Tracking',
772 ],
Fred Drake2a046232003-03-31 16:23:09 +0000773 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000774\end{verbatim}
775
Fred Drakec440af52003-04-25 16:43:28 +0000776If you wish to include classifiers in your \file{setup.py} file and also
777wish to remain backwards-compatible with Python releases prior to 2.2.3,
778then you can include the following code fragment in your \file{setup.py}
779before the \code{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000780
781\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000782# patch distutils if it can't cope with the "classifiers" or
783# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000784if sys.version < '2.2.3':
785 from distutils.dist import DistributionMetadata
786 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000787 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000788\end{verbatim}
789
Greg Ward16aafcd2000-04-09 04:06:44 +0000790
791\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000792\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000793
Greg Ward16aafcd2000-04-09 04:06:44 +0000794Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000795distribution \emph{a priori}: you may need to get some information from
796the user, or from the user's system, in order to proceed. As long as
797that information is fairly simple---a list of directories to search for
798C header files or libraries, for example---then providing a
799configuration file, \file{setup.cfg}, for users to edit is a cheap and
800easy way to solicit it. Configuration files also let you provide
801default values for any command option, which the installer can then
802override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000803
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000804% (If you have more advanced needs, such as determining which extensions
805% to build based on what capabilities are present on the target system,
806% then you need the Distutils ``auto-configuration'' facility. This
807% started to appear in Distutils 0.9 but, as of this writing, isn't mature
808% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000809
Greg Ward47f99a62000-09-04 20:07:15 +0000810The setup configuration file is a useful middle-ground between the setup
811script---which, ideally, would be opaque to installers\footnote{This
812 ideal probably won't be achieved until auto-configuration is fully
813 supported by the Distutils.}---and the command-line to the setup
814script, which is outside of your control and entirely up to the
815installer. In fact, \file{setup.cfg} (and any other Distutils
816configuration files present on the target system) are processed after
817the contents of the setup script, but before the command-line. This has
818several useful consequences:
819\begin{itemize}
820\item installers can override some of what you put in \file{setup.py} by
821 editing \file{setup.cfg}
822\item you can provide non-standard defaults for options that are not
823 easily set in \file{setup.py}
824\item installers can override anything in \file{setup.cfg} using the
825 command-line options to \file{setup.py}
826\end{itemize}
827
828The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000829
Greg Ward47f99a62000-09-04 20:07:15 +0000830\begin{verbatim}
831[command]
832option=value
833...
834\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000835
Greg Ward47f99a62000-09-04 20:07:15 +0000836where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000837\command{build\_py}, \command{install}), and \var{option} is one of
838the options that command supports. Any number of options can be
839supplied for each command, and any number of command sections can be
840included in the file. Blank lines are ignored, as are comments, which
841run from a \character{\#} character until the end of the line. Long
842option values can be split across multiple lines simply by indenting
843the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000844
845You can find out the list of options supported by a particular command
846with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000847
Greg Ward47f99a62000-09-04 20:07:15 +0000848\begin{verbatim}
849> python setup.py --help build_ext
850[...]
851Options for 'build_ext' command:
852 --build-lib (-b) directory for compiled extension modules
853 --build-temp (-t) directory for temporary files (build by-products)
854 --inplace (-i) ignore build-lib and put compiled extensions into the
855 source directory alongside your pure Python modules
856 --include-dirs (-I) list of directories to search for header files
857 --define (-D) C preprocessor macros to define
858 --undef (-U) C preprocessor macros to undefine
859[...]
860\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000861
Greg Ward47f99a62000-09-04 20:07:15 +0000862Note that an option spelled \longprogramopt{foo-bar} on the command-line
863is spelled \option{foo\_bar} in configuration files.
864
865For example, say you want your extensions to be built
866``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000867want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000868in the same source directory as your pure Python modules
869\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
870\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000871
Greg Ward47f99a62000-09-04 20:07:15 +0000872\begin{verbatim}
873python setup.py build_ext --inplace
874\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000875
Greg Ward47f99a62000-09-04 20:07:15 +0000876But this requires that you always specify the \command{build\_ext}
877command explicitly, and remember to provide \longprogramopt{inplace}.
878An easier way is to ``set and forget'' this option, by encoding it in
879\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000880
Greg Ward47f99a62000-09-04 20:07:15 +0000881\begin{verbatim}
882[build_ext]
883inplace=1
884\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000885
Greg Ward47f99a62000-09-04 20:07:15 +0000886This will affect all builds of this module distribution, whether or not
887you explcitly specify \command{build\_ext}. If you include
888\file{setup.cfg} in your source distribution, it will also affect
889end-user builds---which is probably a bad idea for this option, since
890always building extensions in-place would break installation of the
891module distribution. In certain peculiar cases, though, modules are
892built right in their installation directory, so this is conceivably a
893useful ability. (Distributing extensions that expect to be built in
894their installation directory is almost always a bad idea, though.)
895
896Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000897change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000898everything required to generate a ``spec'' file for creating an RPM
899distribution. Some of this information comes from the setup script, and
900some is automatically generated by the Distutils (such as the list of
901files installed). But some of it has to be supplied as options to
902\command{bdist\_rpm}, which would be very tedious to do on the
903command-line for every run. Hence, here is a snippet from the
904Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000905
Greg Ward47f99a62000-09-04 20:07:15 +0000906\begin{verbatim}
907[bdist_rpm]
908release = 1
909packager = Greg Ward <gward@python.net>
910doc_files = CHANGES.txt
911 README.txt
912 USAGE.txt
913 doc/
914 examples/
915\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000916
Greg Ward47f99a62000-09-04 20:07:15 +0000917Note that the \option{doc\_files} option is simply a
918whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000919
920
Fred Drakea09262e2001-03-01 18:35:43 +0000921\begin{seealso}
922 \seetitle[../inst/config-syntax.html]{Installing Python
923 Modules}{More information on the configuration files is
924 available in the manual for system administrators.}
925\end{seealso}
926
927
Greg Ward16aafcd2000-04-09 04:06:44 +0000928\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000929\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000930
Greg Warde78298a2000-04-28 17:12:24 +0000931As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000932\command{sdist} command to create a source distribution. In the
933simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000934
Greg Ward16aafcd2000-04-09 04:06:44 +0000935\begin{verbatim}
936python setup.py sdist
937\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000938
Greg Ward19c67f82000-06-24 01:33:16 +0000939(assuming you haven't specified any \command{sdist} options in the setup
940script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000941default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000942tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
943\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000944
Greg Wardd5767a52000-04-19 22:48:09 +0000945You can specify as many formats as you like using the
946\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000947
Greg Ward16aafcd2000-04-09 04:06:44 +0000948\begin{verbatim}
949python setup.py sdist --formats=gztar,zip
950\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000951
Greg Ward16aafcd2000-04-09 04:06:44 +0000952to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000953\begin{tableiii}{l|l|c}{code}%
954 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000955 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
956 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000957 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000958 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000959 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000960\end{tableiii}
961
962\noindent Notes:
963\begin{description}
964\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000965\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000966\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000967 \module{zipfile} module (part of the standard Python library since
968 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000969\item[(4)] requires external utilities: \program{tar} and possibly one
970 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000971\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000972
973
Greg Ward54589d42000-09-06 01:37:35 +0000974
975\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000976\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000977
Greg Ward54589d42000-09-06 01:37:35 +0000978If you don't supply an explicit list of files (or instructions on how to
979generate one), the \command{sdist} command puts a minimal default set
980into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000981\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000982\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000983 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000984\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000985 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000986 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000987\item anything that looks like a test script: \file{test/test*.py}
988 (currently, the Distutils don't do anything with test scripts except
989 include them in source distributions, but in the future there will be
990 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000991\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
992 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000993\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000994
Greg Ward16aafcd2000-04-09 04:06:44 +0000995Sometimes this is enough, but usually you will want to specify
996additional files to distribute. The typical way to do this is to write
997a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000998manifest template is just a list of instructions for how to generate
999your manifest file, \file{MANIFEST}, which is the exact list of files to
1000include in your source distribution. The \command{sdist} command
1001processes this template and generates a manifest based on its
1002instructions and what it finds in the filesystem.
1003
1004If you prefer to roll your own manifest file, the format is simple: one
1005filename per line, regular files (or symlinks to them) only. If you do
1006supply your own \file{MANIFEST}, you must specify everything: the
1007default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001008
1009The manifest template has one command per line, where each command
1010specifies a set of files to include or exclude from the source
1011distribution. For an example, again we turn to the Distutils' own
1012manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001013
Greg Ward16aafcd2000-04-09 04:06:44 +00001014\begin{verbatim}
1015include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001016recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001017prune examples/sample?/build
1018\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001019
Greg Ward16aafcd2000-04-09 04:06:44 +00001020The meanings should be fairly clear: include all files in the
1021distribution root matching \code{*.txt}, all files anywhere under the
1022\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001023exclude all directories matching \code{examples/sample?/build}. All of
1024this is done \emph{after} the standard include set, so you can exclude
1025files from the standard set with explicit instructions in the manifest
1026template. (Or, you can use the \longprogramopt{no-defaults} option to
1027disable the standard set entirely.) There are several other commands
1028available in the manifest template mini-language; see
1029section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001030
Greg Ward54589d42000-09-06 01:37:35 +00001031The order of commands in the manifest template matters: initially, we
1032have the list of default files as described above, and each command in
1033the template adds to or removes from that list of files. Once we have
1034fully processed the manifest template, we remove files that should not
1035be included in the source distribution:
1036\begin{itemize}
1037\item all files in the Distutils ``build'' tree (default \file{build/})
1038\item all files in directories named \file{RCS} or \file{CVS}
1039\end{itemize}
1040Now we have our complete list of files, which is written to the manifest
1041for future reference, and then used to build the source distribution
1042archive(s).
1043
1044You can disable the default set of included files with the
1045\longprogramopt{no-defaults} option, and you can disable the standard
1046exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001047
Greg Ward46b98e32000-04-14 01:53:36 +00001048Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001049\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001050Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001051\begin{enumerate}
1052\item include all Python source files in the \file{distutils} and
1053 \file{distutils/command} subdirectories (because packages
1054 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001055 \option{packages} option in the setup script---see
1056 section~\ref{setup-script})
1057\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1058 (standard files)
1059\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001060\item include \file{*.txt} in the distribution root (this will find
1061 \file{README.txt} a second time, but such redundancies are weeded out
1062 later)
Greg Ward54589d42000-09-06 01:37:35 +00001063\item include anything matching \file{*.txt} or \file{*.py} in the
1064 sub-tree under \file{examples},
1065\item exclude all files in the sub-trees starting at directories
1066 matching \file{examples/sample?/build}---this may exclude files
1067 included by the previous two steps, so it's important that the
1068 \code{prune} command in the manifest template comes after the
1069 \code{recursive-include} command
1070\item exclude the entire \file{build} tree, and any \file{RCS} or
1071 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001072\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001073Just like in the setup script, file and directory names in the manifest
1074template should always be slash-separated; the Distutils will take care
1075of converting them to the standard representation on your platform.
1076That way, the manifest template is portable across operating systems.
1077
Greg Ward16aafcd2000-04-09 04:06:44 +00001078
1079\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001080\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001081
1082The normal course of operations for the \command{sdist} command is as
1083follows:
1084\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001085\item if the manifest file, \file{MANIFEST} doesn't exist, read
1086 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001087\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001088 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001089\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1090 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1091 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001092\item use the list of files now in \file{MANIFEST} (either just
1093 generated or read in) to create the source distribution archive(s)
1094\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001095There are a couple of options that modify this behaviour. First, use
1096the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001097disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001098
Greg Ward54589d42000-09-06 01:37:35 +00001099Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001100example, if you have added or removed files or directories that match an
1101existing pattern in the manifest template, you should regenerate the
1102manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001103
Greg Ward16aafcd2000-04-09 04:06:44 +00001104\begin{verbatim}
1105python setup.py sdist --force-manifest
1106\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001107
1108Or, you might just want to (re)generate the manifest, but not create a
1109source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001110
Greg Ward16aafcd2000-04-09 04:06:44 +00001111\begin{verbatim}
1112python setup.py sdist --manifest-only
1113\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001114
Greg Ward54589d42000-09-06 01:37:35 +00001115\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1116\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1117\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001118
1119
1120\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001121\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001122
Greg Ward46b98e32000-04-14 01:53:36 +00001123A ``built distribution'' is what you're probably used to thinking of
1124either as a ``binary package'' or an ``installer'' (depending on your
1125background). It's not necessarily binary, though, because it might
1126contain only Python source code and/or byte-code; and we don't call it a
1127package, because that word is already spoken for in Python. (And
1128``installer'' is a term specific to the Windows world. \XXX{do Mac
1129 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001130
Greg Ward46b98e32000-04-14 01:53:36 +00001131A built distribution is how you make life as easy as possible for
1132installers of your module distribution: for users of RPM-based Linux
1133systems, it's a binary RPM; for Windows users, it's an executable
1134installer; for Debian-based Linux users, it's a Debian package; and so
1135forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001136distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001137designed to enable module developers to concentrate on their
1138specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001139intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001140distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001141are packagers.
1142
1143Of course, the module developer could be his own packager; or the
1144packager could be a volunteer ``out there'' somewhere who has access to
1145a platform which the original developer does not; or it could be
1146software periodically grabbing new source distributions and turning them
1147into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001148access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001149setup script and the \command{bdist} command family to generate built
1150distributions.
1151
1152As a simple example, if I run the following command in the Distutils
1153source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001154
Greg Ward46b98e32000-04-14 01:53:36 +00001155\begin{verbatim}
1156python setup.py bdist
1157\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001158
Greg Ward46b98e32000-04-14 01:53:36 +00001159then the Distutils builds my module distribution (the Distutils itself
1160in this case), does a ``fake'' installation (also in the \file{build}
1161directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001162platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001163file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001164file is considered ``dumb'' because it has to be unpacked in a specific
1165location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001166
Fred Drakeeff9a872000-10-26 16:41:03 +00001167Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001168\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001169from the right place installs the Distutils just as though you had
1170downloaded the source distribution and run \code{python setup.py
1171 install}. (The ``right place'' is either the root of the filesystem or
1172Python's \filevar{prefix} directory, depending on the options given to
1173the \command{bdist\_dumb} command; the default is to make dumb
1174distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001175
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001176Obviously, for pure Python distributions, this isn't any simpler than
1177just running \code{python setup.py install}---but for non-pure
1178distributions, which include extensions that would need to be
1179compiled, it can mean the difference between someone being able to use
1180your extensions or not. And creating ``smart'' built distributions,
1181such as an RPM package or an executable installer for Windows, is far
1182more convenient for users even if your distribution doesn't include
1183any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001184
Greg Wardb6528972000-09-07 02:40:37 +00001185The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001186similar to the \command{sdist} command, which you can use to select the
1187types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001188
Greg Ward46b98e32000-04-14 01:53:36 +00001189\begin{verbatim}
1190python setup.py bdist --format=zip
1191\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001192
Fred Drakeeff9a872000-10-26 16:41:03 +00001193would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001194\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001195unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001196
1197The available formats for built distributions are:
1198\begin{tableiii}{l|l|c}{code}%
1199 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001200 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1201 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1202 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1203 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1204 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001205 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1206 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1207 \lineiii{rpm}{RPM}{(5)}
1208% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001209 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001210\end{tableiii}
1211
1212\noindent Notes:
1213\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001214\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001215\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001216\item[(3)] requires external utilities: \program{tar} and possibly one
1217 of \program{gzip}, \program{bzip2}, or \program{compress}
1218\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001219 \module{zipfile} module (part of the standard Python library since
1220 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001221\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1222 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001223\end{description}
1224
1225You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001226\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001227directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001228\command{bdist} ``sub-commands'' actually generate several similar
1229formats; for instance, the \command{bdist\_dumb} command generates all
1230the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1231\code{zip}), and \command{bdist\_rpm} generates both binary and source
1232RPMs. The \command{bdist} sub-commands, and the formats generated by
1233each, are:
1234\begin{tableii}{l|l}{command}%
1235 {Command}{Formats}
1236 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1237 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001238 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001239\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001240
Greg Wardb6528972000-09-07 02:40:37 +00001241The following sections give details on the individual \command{bdist\_*}
1242commands.
1243
1244
1245\subsection{Creating dumb built distributions}
1246\label{creating-dumb}
1247
1248\XXX{Need to document absolute vs. prefix-relative packages here, but
1249 first I have to implement it!}
1250
1251
1252\subsection{Creating RPM packages}
1253\label{creating-rpms}
1254
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001255The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001256Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1257RPM-based Linux distributions) is your usual environment, creating RPM
1258packages for other users of that same distribution is trivial.
1259Depending on the complexity of your module distribution and differences
1260between Linux distributions, you may also be able to create RPMs that
1261work on different RPM-based distributions.
1262
1263The usual way to create an RPM of your module distribution is to run the
1264\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001265
Greg Wardb6528972000-09-07 02:40:37 +00001266\begin{verbatim}
1267python setup.py bdist_rpm
1268\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001269
Greg Wardb6528972000-09-07 02:40:37 +00001270or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001271
Greg Wardb6528972000-09-07 02:40:37 +00001272\begin{verbatim}
1273python setup.py bdist --formats=rpm
1274\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001275
Greg Wardb6528972000-09-07 02:40:37 +00001276The former allows you to specify RPM-specific options; the latter allows
1277you to easily specify multiple formats in one run. If you need to do
1278both, you can explicitly specify multiple \command{bdist\_*} commands
1279and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001280
Greg Wardb6528972000-09-07 02:40:37 +00001281\begin{verbatim}
1282python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1283 bdist_wininst --target_version="2.0"
1284\end{verbatim}
1285
1286Creating RPM packages is driven by a \file{.spec} file, much as using
1287the Distutils is driven by the setup script. To make your life easier,
1288the \command{bdist\_rpm} command normally creates a \file{.spec} file
1289based on the information you supply in the setup script, on the command
1290line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001291sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001292script as follows:
1293\begin{tableii}{l|l}{textrm}%
1294 {RPM \file{.spec} file option or section}{Distutils setup script option}
1295 \lineii{Name}{\option{name}}
1296 \lineii{Summary (in preamble)}{\option{description}}
1297 \lineii{Version}{\option{version}}
1298 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1299 \option{maintainer} and \option{maintainer\_email}}
1300 \lineii{Copyright}{\option{licence}}
1301 \lineii{Url}{\option{url}}
1302 \lineii{\%description (section)}{\option{long\_description}}
1303\end{tableii}
1304
1305Additionally, there many options in \file{.spec} files that don't have
1306corresponding options in the setup script. Most of these are handled
1307through options to the \command{bdist\_rpm} command as follows:
1308\begin{tableiii}{l|l|l}{textrm}%
1309 {RPM \file{.spec} file option or section}%
1310 {\command{bdist\_rpm} option}%
1311 {default value}
1312 \lineiii{Release}{\option{release}}{``1''}
1313 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1314 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001315 \lineiii{Packager}{\option{packager}}{(none)}
1316 \lineiii{Provides}{\option{provides}}{(none)}
1317 \lineiii{Requires}{\option{requires}}{(none)}
1318 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1319 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001320 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1321 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1322 \lineiii{Icon}{\option{icon}}{(none)}
1323\end{tableiii}
1324Obviously, supplying even a few of these options on the command-line
1325would be tedious and error-prone, so it's usually best to put them in
1326the setup configuration file, \file{setup.cfg}---see
1327section~\ref{setup-config}. If you distribute or package many Python
1328module distributions, you might want to put options that apply to all of
1329them in your personal Distutils configuration file
1330(\file{\textasciitilde/.pydistutils.cfg}).
1331
1332There are three steps to building a binary RPM package, all of which are
1333handled automatically by the Distutils:
1334\begin{enumerate}
1335\item create a \file{.spec} file, which describes the package (analogous
1336 to the Distutils setup script; in fact, much of the information in the
1337 setup script winds up in the \file{.spec} file)
1338\item create the source RPM
1339\item create the ``binary'' RPM (which may or may not contain binary
1340 code, depending on whether your module distribution contains Python
1341 extensions)
1342\end{enumerate}
1343Normally, RPM bundles the last two steps together; when you use the
1344Distutils, all three steps are typically bundled together.
1345
1346If you wish, you can separate these three steps. You can use the
1347\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1348create the \file{.spec} file and exit; in this case, the \file{.spec}
1349file will be written to the ``distribution directory''---normally
1350\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1351option. (Normally, the \file{.spec} file winds up deep in the ``build
1352tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1353
1354\XXX{this isn't implemented yet---is it needed?!}
1355You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001356\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001357\longprogramopt{spec-only}, this gives you an opportunity to customize
1358the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001359
Greg Wardb6528972000-09-07 02:40:37 +00001360\begin{verbatim}
1361> python setup.py bdist_rpm --spec-only
1362# ...edit dist/FooBar-1.0.spec
1363> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1364\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001365
Greg Wardb6528972000-09-07 02:40:37 +00001366(Although a better way to do this is probably to override the standard
1367\command{bdist\_rpm} command with one that writes whatever else you want
1368to the \file{.spec} file; see section~\ref{extending} for information on
1369extending the Distutils.)
1370
1371
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001372\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001373\label{creating-wininst}
1374
Thomas Hellere61f3652002-11-15 20:13:26 +00001375Executable installers are the natural format for binary distributions
1376on Windows. They display a nice graphical user interface, display
1377some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001378from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001379options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001380
Thomas Hellere61f3652002-11-15 20:13:26 +00001381Since the metadata is taken from the setup script, creating Windows
1382installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001383
Thomas Heller5f52f722001-02-19 17:48:03 +00001384\begin{verbatim}
1385python setup.py bdist_wininst
1386\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001387
Thomas Heller36343f62002-11-15 19:20:56 +00001388or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001389
Thomas Heller5f52f722001-02-19 17:48:03 +00001390\begin{verbatim}
1391python setup.py bdist --formats=wininst
1392\end{verbatim}
1393
Thomas Hellere61f3652002-11-15 20:13:26 +00001394If you have a pure module distribution (only containing pure Python
1395modules and packages), the resulting installer will be version
1396independent and have a name like \file{foo-1.0.win32.exe}. These
1397installers can even be created on \UNIX{} or MacOS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001398
1399If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001400created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001401The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001402\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001403for every Python version you want to support.
1404
1405The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001406installation on the target system in normal and optimizing mode. If
1407you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001408\command{bdist_wininst} command with the
1409\longprogramopt{no-target-compile} and/or the
1410\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001411
Fred Drake0e9bfa32002-11-15 20:34:52 +00001412By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001413when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001414a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001415
1416The installer will also display a large title on the desktop
1417background window when it is run, which is constructed from the name
1418of your distribution and the version number. This can be changed to
1419another text by using the \longprogramopt{title} option.
1420
1421The installer file will be written to the ``distribution directory''
1422--- normally \file{dist/}, but customizable with the
1423\longprogramopt{dist-dir} option.
1424
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001425\subsubsection{The Postinstallation script}
1426\label{postinstallation-script}
1427
1428Starting with Python 2.3, a postinstallation script can be specified
1429which the \longprogramopt{install-script} option. The basename of the
1430script must be specified, and the script filename must also be listed
1431in the scripts argument to the setup function.
1432
1433This script will be run at installation time on the target system
1434after all the files have been copied, with argv[1] set to '-install',
1435and again at uninstallation time before the files are removed with argv[1]
1436set to '-remove'.
1437
1438The installation script runs embedded in the windows installer, every
1439output (sys.stdout, sys.stderr) is redirected into a buffer and will
1440be displayed in the GUI after the script has finished.
1441
1442Some functions especially useful in this context are available in the
1443installation script.
1444
1445\begin{verbatim}
1446dir_created(pathname)
1447file_created(pathname)
1448\end{verbatim}
1449
1450These functions should be called when a directory or file is created
1451by the postinstall script at installation time. It will register the
1452pathname with the uninstaller, so that it will be removed when the
1453distribution is uninstalled. To be safe, directories are only removed
1454if they are empty.
1455
1456\begin{verbatim}
1457get_special_folder_path(csidl_string)
1458\end{verbatim}
1459
1460This function can be used to retrieve special folder locations on
1461Windows like the Start Menu or the Desktop. It returns the full path
1462to the folder. 'csidl_string' must be on of the following strings:
1463
1464\begin{verbatim}
1465"CSIDL_APPDATA"
1466
1467"CSIDL_COMMON_STARTMENU"
1468"CSIDL_STARTMENU"
1469
1470"CSIDL_COMMON_DESKTOPDIRECTORY"
1471"CSIDL_DESKTOPDIRECTORY"
1472
1473"CSIDL_COMMON_STARTUP"
1474"CSIDL_STARTUP"
1475
1476"CSIDL_COMMON_PROGRAMS"
1477"CSIDL_PROGRAMS"
1478
1479"CSIDL_FONTS"
1480\end{verbatim}
1481
1482If the folder cannot be retrieved, OSError is raised.
1483
1484Which folders are available depends on the exact Windows version, and probably
1485also the configuration. For details refer to Microsoft's documentation of the
Thomas Heller63b4dd32002-12-12 19:35:00 +00001486\code{SHGetSpecialFolderPath} function.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001487
1488\begin{verbatim}
1489create_shortcut(target, description, filename[, arguments[,
1490 workdir[, iconpath[, iconindex]]]])
1491\end{verbatim}
1492
1493This function creates a shortcut.
Thomas Heller63b4dd32002-12-12 19:35:00 +00001494\var{target} is the path to the program to be started by the shortcut.
1495\var{description} is the description of the sortcut.
1496\var{filename} is the title of the shortcut that the user will see.
1497\var{arguments} specifies the command line arguments, if any.
1498\var{workdir} is the working directory for the program.
1499\var{iconpath} is the file containing the icon for the shortcut,
1500and \var{iconindex} is the index of the icon in the file
1501\var{iconpath}. Again, for details consult the Microsoft
1502documentation for the \code{IShellLink} interface.
Greg Wardb6528972000-09-07 02:40:37 +00001503
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001504\section{Registering with the Package Index}
1505\label{package-index}
1506
1507The Python Package Index (PyPI) holds meta-data describing distributions
1508packaged with distutils. The distutils command \command{register} is
1509used to submit your distribution's meta-data to the index. It is invoked
1510as follows:
1511
1512\begin{verbatim}
1513python setup.py register
1514\end{verbatim}
1515
1516Distutils will respond with the following prompt:
1517
1518\begin{verbatim}
1519running register
1520We need to know who you are, so please choose either:
1521 1. use your existing login,
1522 2. register as a new user,
1523 3. have the server generate a new password for you (and email it to you), or
1524 4. quit
1525Your selection [default 1]:
1526\end{verbatim}
1527
1528\noindent Note: if your username and password are saved locally, you will
1529not see this menu.
1530
1531If you have not registered with PyPI, then you will need to do so now. You
1532should choose option 2, and enter your details as required. Soon after
1533submitting your details, you will receive an email which will be used to
1534confirm your registration.
1535
1536Once you are registered, you may choose option 1 from the menu. You will
1537be prompted for your PyPI username and password, and \command{register}
1538will then submit your meta-data to the index.
1539
1540You may submit any number of versions of your distribution to the index. If
1541you alter the meta-data for a particular version, you may submit it again
1542and the index will be updated.
1543
1544PyPI holds a record for each (name, version) combination submitted. The
1545first user to submit information for a given name is designated the Owner
1546of that name. They may submit changes through the \command{register}
1547command or through the web interface. They may also designate other users
1548as Owners or Maintainers. Maintainers may edit the package information, but
1549not designate other Owners or Maintainers.
1550
1551By default PyPI will list all versions of a given package. To hide certain
1552versions, the Hidden property should be set to yes. This must be edited
1553through the web interface.
1554
1555
1556
Greg Ward007c04a2002-05-10 14:45:59 +00001557\section{Examples}
1558\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001559
Greg Ward007c04a2002-05-10 14:45:59 +00001560\subsection{Pure Python distribution (by module)}
1561\label{pure-mod}
1562
1563If you're just distributing a couple of modules, especially if they
1564don't live in a particular package, you can specify them individually
1565using the \option{py\_modules} option in the setup script.
1566
1567In the simplest case, you'll have two files to worry about: a setup
1568script and the single module you're distributing, \file{foo.py} in this
1569example:
1570\begin{verbatim}
1571<root>/
1572 setup.py
1573 foo.py
1574\end{verbatim}
1575(In all diagrams in this section, \verb|<root>| will refer to the
1576distribution root directory.) A minimal setup script to describe this
1577situation would be:
1578\begin{verbatim}
1579from distutils.core import setup
1580setup(name = "foo", version = "1.0",
1581 py_modules = ["foo"])
1582\end{verbatim}
1583Note that the name of the distribution is specified independently with
1584the \option{name} option, and there's no rule that says it has to be the
1585same as the name of the sole module in the distribution (although that's
1586probably a good convention to follow). However, the distribution name
1587is used to generate filenames, so you should stick to letters, digits,
1588underscores, and hyphens.
1589
1590Since \option{py\_modules} is a list, you can of course specify multiple
1591modules, eg. if you're distributing modules \module{foo} and
1592\module{bar}, your setup might look like this:
1593\begin{verbatim}
1594<root>/
1595 setup.py
1596 foo.py
1597 bar.py
1598\end{verbatim}
1599and the setup script might be
1600\begin{verbatim}
1601from distutils.core import setup
1602setup(name = "foobar", version = "1.0",
1603 py_modules = ["foo", "bar"])
1604\end{verbatim}
1605
1606You can put module source files into another directory, but if you have
1607enough modules to do that, it's probably easier to specify modules by
1608package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001609
1610
Greg Ward007c04a2002-05-10 14:45:59 +00001611\subsection{Pure Python distribution (by package)}
1612\label{pure-pkg}
1613
1614If you have more than a couple of modules to distribute, especially if
1615they are in multiple packages, it's probably easier to specify whole
1616packages rather than individual modules. This works even if your
1617modules are not in a package; you can just tell the Distutils to process
1618modules from the root package, and that works the same as any other
1619package (except that you don't have to have an \file{\_\_init\_\_.py}
1620file).
1621
1622The setup script from the last example could also be written as
1623\begin{verbatim}
1624from distutils.core import setup
1625setup(name = "foobar", version = "1.0",
1626 packages = [""])
1627\end{verbatim}
1628(The empty string stands for the root package.)
1629
1630If those two files are moved into a subdirectory, but remain in the root
1631package, e.g.:
1632\begin{verbatim}
1633<root>/
1634 setup.py
1635 src/ foo.py
1636 bar.py
1637\end{verbatim}
1638then you would still specify the root package, but you have to tell the
1639Distutils where source files in the root package live:
1640\begin{verbatim}
1641from distutils.core import setup
1642setup(name = "foobar", version = "1.0",
1643 package_dir = {"": "src"},
1644 packages = [""])
1645\end{verbatim}
1646
1647More typically, though, you will want to distribute multiple modules in
1648the same package (or in sub-packages). For example, if the \module{foo}
1649and \module{bar} modules belong in package \module{foobar}, one way to
1650layout your source tree is
1651\begin{verbatim}
1652<root>/
1653 setup.py
1654 foobar/
1655 __init__.py
1656 foo.py
1657 bar.py
1658\end{verbatim}
1659This is in fact the default layout expected by the Distutils, and the
1660one that requires the least work to describe in your setup script:
1661\begin{verbatim}
1662from distutils.core import setup
1663setup(name = "foobar", version = "1.0",
1664 packages = ["foobar"])
1665\end{verbatim}
1666
1667If you want to put modules in directories not named for their package,
1668then you need to use the \option{package\_dir} option again. For
1669example, if the \file{src} directory holds modules in the
1670\module{foobar} package:
1671\begin{verbatim}
1672<root>/
1673 setup.py
1674 src/
1675 __init__.py
1676 foo.py
1677 bar.py
1678\end{verbatim}
1679an appropriate setup script would be
1680\begin{verbatim}
1681from distutils.core import setup
1682setup(name = "foobar", version = "1.0",
1683 package_dir = {"foobar" : "src"},
1684 packages = ["foobar"])
1685\end{verbatim}
1686
1687Or, you might put modules from your main package right in the
1688distribution root:
1689\begin{verbatim}
1690<root>/
1691 setup.py
1692 __init__.py
1693 foo.py
1694 bar.py
1695\end{verbatim}
1696in which case your setup script would be
1697\begin{verbatim}
1698from distutils.core import setup
1699setup(name = "foobar", version = "1.0",
1700 package_dir = {"foobar" : ""},
1701 packages = ["foobar"])
1702\end{verbatim}
1703(The empty string also stands for the current directory.)
1704
1705If you have sub-packages, they must be explicitly listed in
1706\option{packages}, but any entries in \option{package\_dir}
1707automatically extend to sub-packages. (In other words, the Distutils
1708does \emph{not} scan your source tree, trying to figure out which
1709directories correspond to Python packages by looking for
1710\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1711sub-package:
1712\begin{verbatim}
1713<root>/
1714 setup.py
1715 foobar/
1716 __init__.py
1717 foo.py
1718 bar.py
1719 subfoo/
1720 __init__.py
1721 blah.py
1722\end{verbatim}
1723then the corresponding setup script would be
1724\begin{verbatim}
1725from distutils.core import setup
1726setup(name = "foobar", version = "1.0",
1727 packages = ["foobar", "foobar.subfoo"])
1728\end{verbatim}
1729(Again, the empty string in \option{package\_dir} stands for the current
1730directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001731
1732
Greg Ward007c04a2002-05-10 14:45:59 +00001733\subsection{Single extension module}
1734\label{single-ext}
1735
1736Extension modules are specified using the \option{ext\_modules} option.
1737\option{package\_dir} has no effect on where extension source files are
1738found; it only affects the source for pure Python modules. The simplest
1739case, a single extension module in a single C source file, is:
1740\begin{verbatim}
1741<root>/
1742 setup.py
1743 foo.c
1744\end{verbatim}
1745If the \module{foo} extension belongs in the root package, the setup
1746script for this could be
1747\begin{verbatim}
1748from distutils.core import setup
1749setup(name = "foobar", version = "1.0",
1750 ext_modules = [Extension("foo", ["foo.c"])])
1751\end{verbatim}
1752
1753If the extension actually belongs in a package, say \module{foopkg},
1754then
1755
1756With exactly the same source tree layout, this extension can be put in
1757the \module{foopkg} package simply by changing the name of the
1758extension:
1759\begin{verbatim}
1760from distutils.core import setup
1761setup(name = "foobar", version = "1.0",
1762 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1763\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001764
1765
Fred Drakea09262e2001-03-01 18:35:43 +00001766%\subsection{Multiple extension modules}
1767%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001768
1769
Fred Drakea09262e2001-03-01 18:35:43 +00001770%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001771
1772
Fred Drakea09262e2001-03-01 18:35:43 +00001773%\section{Extending the Distutils}
1774%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001775
1776
Fred Drakea09262e2001-03-01 18:35:43 +00001777%\subsection{Extending existing commands}
1778%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001779
1780
Fred Drakea09262e2001-03-01 18:35:43 +00001781%\subsection{Writing new commands}
1782%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001783
Fred Drakea09262e2001-03-01 18:35:43 +00001784%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001785
Greg Ward4a9e7222000-04-25 02:57:36 +00001786
1787
Greg Ward16aafcd2000-04-09 04:06:44 +00001788\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001789\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001790
1791
Fred Drakea09262e2001-03-01 18:35:43 +00001792%\subsection{Building modules: the \protect\command{build} command family}
1793%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001794
Fred Drakea09262e2001-03-01 18:35:43 +00001795%\subsubsection{\protect\command{build}}
1796%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001797
Fred Drakea09262e2001-03-01 18:35:43 +00001798%\subsubsection{\protect\command{build\_py}}
1799%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001800
Fred Drakea09262e2001-03-01 18:35:43 +00001801%\subsubsection{\protect\command{build\_ext}}
1802%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001803
Fred Drakea09262e2001-03-01 18:35:43 +00001804%\subsubsection{\protect\command{build\_clib}}
1805%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001806
1807
Greg Wardfacb8db2000-04-09 04:32:40 +00001808\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001809\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001810
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001811The install command ensures that the build commands have been run and then
1812runs the subcommands \command{install\_lib},
1813\command{install\_data} and
1814\command{install\_scripts}.
1815
Fred Drakea09262e2001-03-01 18:35:43 +00001816%\subsubsection{\protect\command{install\_lib}}
1817%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001818
1819\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001820\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001821This command installs all data files provided with the distribution.
1822
1823\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001824\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001825This command installs all (Python) scripts in the distribution.
1826
Greg Ward16aafcd2000-04-09 04:06:44 +00001827
Fred Drakea09262e2001-03-01 18:35:43 +00001828%\subsection{Cleaning up: the \protect\command{clean} command}
1829%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001830
1831
Fred Drakeeff9a872000-10-26 16:41:03 +00001832\subsection{Creating a source distribution: the
1833 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001834\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001835
1836
1837\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001838
Greg Ward16aafcd2000-04-09 04:06:44 +00001839The manifest template commands are:
1840\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001841 \lineii{include \var{pat1} \var{pat2} ... }
1842 {include all files matching any of the listed patterns}
1843 \lineii{exclude \var{pat1} \var{pat2} ... }
1844 {exclude all files matching any of the listed patterns}
1845 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1846 {include all files under \var{dir} matching any of the listed patterns}
1847 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1848 {exclude all files under \var{dir} matching any of the listed patterns}
1849 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001850 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001851 any of the listed patterns}
1852 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001853 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001854 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001855 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1856 \lineii{graft \var{dir}}{include all files under \var{dir}}
1857\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001858The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001859sequence of regular filename characters, \code{?} matches any single
1860regular filename character, and \code{[\var{range}]} matches any of the
1861characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001862\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001863platform-specific: on \UNIX{} it is anything except slash; on Windows
1864anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001865
Fred Drakeeff9a872000-10-26 16:41:03 +00001866\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001867
1868
Fred Drakea09262e2001-03-01 18:35:43 +00001869%\subsection{Creating a built distribution: the
1870% \protect\command{bdist} command family}
1871%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001872
1873
Fred Drakeab70b382001-08-02 15:13:15 +00001874%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001875
Fred Drakeab70b382001-08-02 15:13:15 +00001876%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001877
Fred Drakeab70b382001-08-02 15:13:15 +00001878%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001879
Fred Drakeab70b382001-08-02 15:13:15 +00001880%\subsubsection{\protect\command{bdist\_wininst}}
1881
1882
1883\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001884
1885
Greg Wardabc52162000-02-26 00:52:48 +00001886\end{document}