blob: 91bb228db0d0005a856c8a9ce182cbe34cd962cc [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
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000082building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000083
84If all you want to do is distribute a module called \module{foo},
85contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000086simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000087
Greg Ward16aafcd2000-04-09 04:06:44 +000088\begin{verbatim}
89from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000090setup(name="foo",
91 version="1.0",
92 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000093\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000094
Greg Ward16aafcd2000-04-09 04:06:44 +000095Some observations:
96\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +000097\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +000098 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +000099\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000100 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000101 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000102\item modules are specified by module name, not filename (the same will
103 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000104\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000105 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000106 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000107\end{itemize}
108
Greg Ward370248d2000-06-24 01:45:47 +0000109To create a source distribution for this module, you would create a
110setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000111
Greg Ward16aafcd2000-04-09 04:06:44 +0000112\begin{verbatim}
113python setup.py sdist
114\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000115
Fred Drakeeff9a872000-10-26 16:41:03 +0000116which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000117Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000118\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
119\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000120
121If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000122to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
123and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000124
Greg Ward16aafcd2000-04-09 04:06:44 +0000125\begin{verbatim}
126python setup.py install
127\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000128
Greg Ward16aafcd2000-04-09 04:06:44 +0000129which will ultimately copy \file{foo.py} to the appropriate directory
130for third-party modules in their Python installation.
131
132This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000133Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000134user interface, i.e. the setup script. The difference is which
135Distutils \emph{commands} they use: the \command{sdist} command is
136almost exclusively for module developers, while \command{install} is
137more often for installers (although most developers will want to install
138their own code occasionally).
139
Greg Ward16aafcd2000-04-09 04:06:44 +0000140If you want to make things really easy for your users, you can create
141one or more built distributions for them. For instance, if you are
142running on a Windows machine, and want to make things easy for other
143Windows users, you can create an executable installer (the most
144appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000145\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000146
Greg Ward16aafcd2000-04-09 04:06:44 +0000147\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000148python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000149\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000150
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000151will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000152current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000153
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000154Other useful built distribution formats are RPM, implemented by the
155\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000156(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
157(\command{bdist_sdux}). For example, the following command will
158create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000159
Greg Ward1d8f57a2000-08-05 00:43:11 +0000160\begin{verbatim}
161python setup.py bdist_rpm
162\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000163
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000164(The \command{bdist\_rpm} command uses the \command{rpm} executable,
165therefore this has to be run on an RPM-based system such as Red Hat
166Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000167
168You can find out what distribution formats are available at any time by
169running
Fred Drakea09262e2001-03-01 18:35:43 +0000170
Greg Ward1d8f57a2000-08-05 00:43:11 +0000171\begin{verbatim}
172python setup.py bdist --help-formats
173\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000174
175
176\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000177\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000178
179If you're reading this document, you probably have a good idea of what
180modules, extensions, and so forth are. Nevertheless, just to be sure
181that everyone is operating from a common starting point, we offer the
182following glossary of common Python terms:
183\begin{description}
184\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000185 code imported by some other code. Three types of modules concern us
186 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000187
Greg Ward16aafcd2000-04-09 04:06:44 +0000188\item[pure Python module] a module written in Python and contained in a
189 single \file{.py} file (and possibly associated \file{.pyc} and/or
190 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000191
Greg Ward16aafcd2000-04-09 04:06:44 +0000192\item[extension module] a module written in the low-level language of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000193 the Python implementation: C/C++ for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000194 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000195 file, e.g. a shared object (\file{.so}) file for Python extensions on
196 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000197 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drakeeff9a872000-10-26 16:41:03 +0000198 currently, the Distutils only handles C/C++ extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000199
Greg Ward16aafcd2000-04-09 04:06:44 +0000200\item[package] a module that contains other modules; typically contained
201 in a directory in the filesystem and distinguished from other
202 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000203
Greg Ward6153fa12000-05-26 02:24:28 +0000204\item[root package] the root of the hierarchy of packages. (This isn't
205 really a package, since it doesn't have an \file{\_\_init\_\_.py}
206 file. But we have to call it something.) The vast majority of the
207 standard library is in the root package, as are many small, standalone
208 third-party modules that don't belong to a larger module collection.
209 Unlike regular packages, modules in the root package can be found in
210 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000211 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000212\end{description}
213
214
215\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000216\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000217
218The following terms apply more specifically to the domain of
219distributing Python modules using the Distutils:
220\begin{description}
221\item[module distribution] a collection of Python modules distributed
222 together as a single downloadable resource and meant to be installed
223 \emph{en masse}. Examples of some well-known module distributions are
224 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000225 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000226 is already taken in the Python context: a single module distribution
227 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000228
Greg Ward16aafcd2000-04-09 04:06:44 +0000229\item[pure module distribution] a module distribution that contains only
230 pure Python modules and packages. Sometimes referred to as a ``pure
231 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000232
Greg Ward16aafcd2000-04-09 04:06:44 +0000233\item[non-pure module distribution] a module distribution that contains
234 at least one extension module. Sometimes referred to as a ``non-pure
235 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000236
Greg Ward16aafcd2000-04-09 04:06:44 +0000237\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000238 source distribution); the directory where \file{setup.py} exists. Generally
239 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000240\end{description}
241
242
243\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000244\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000245
246The setup script is the centre of all activity in building,
247distributing, and installing modules using the Distutils. The main
248purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000249the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000250do the right thing. As we saw in section~\ref{simple-example} above,
251the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000252most information supplied to the Distutils by the module developer is
253supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000254
255Here's a slightly more involved example, which we'll follow for the next
256couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000257although the Distutils are included with Python 1.6 and later, they also
258have an independent existence so that Python 1.5.2 users can use them to
259install other module distributions. The Distutils' own setup script,
260shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000261
262\begin{verbatim}
263#!/usr/bin/env python
264
265from distutils.core import setup
266
Fred Drakea09262e2001-03-01 18:35:43 +0000267setup(name="Distutils",
268 version="1.0",
269 description="Python Distribution Utilities",
270 author="Greg Ward",
271 author_email="gward@python.net",
272 url="http://www.python.org/sigs/distutils-sig/",
273 packages=['distutils', 'distutils.command'],
274 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000275\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000276
Greg Ward16aafcd2000-04-09 04:06:44 +0000277There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000278distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000279metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000280rather than by module. This is important since the Distutils consist of
281a couple of dozen modules split into (so far) two packages; an explicit
282list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000283maintain. For more information on the additional meta-data, see
284section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000285
Greg Ward46b98e32000-04-14 01:53:36 +0000286Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000287script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000288slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000289platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000290current platform before actually using the pathname. This makes your
291setup script portable across operating systems, which of course is one
292of the major goals of the Distutils. In this spirit, all pathnames in
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000293this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000294mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000295path, the opposite of the MacOS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000296
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000297This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000298functions. If you, for example, use standard Python functions such as
299\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000300should be careful to write portable code instead of hardcoding path
301separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000302
Thomas Heller5f52f722001-02-19 17:48:03 +0000303\begin{verbatim}
304 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
305 os.listdir(os.path.join('mydir', 'subdir'))
306\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000307
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000308
Greg Ward2afffd42000-08-06 20:37:24 +0000309\subsection{Listing whole packages}
310\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000311
312The \option{packages} option tells the Distutils to process (build,
313distribute, install, etc.) all pure Python modules found in each package
314mentioned in the \option{packages} list. In order to do this, of
315course, there has to be a correspondence between package names and
316directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000317obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000318\file{distutils} relative to the distribution root. Thus, when you say
319\code{packages = ['foo']} in your setup script, you are promising that
320the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
321be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000322the directory where your setup script lives. If you break this
323promise, the Distutils will issue a warning but still process the broken
324package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000325
326If you use a different convention to lay out your source directory,
327that's no problem: you just have to supply the \option{package\_dir}
328option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000329you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000330``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000331\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
332and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000333
Greg Ward16aafcd2000-04-09 04:06:44 +0000334\begin{verbatim}
335package_dir = {'': 'lib'}
336\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000337
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000338in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000339and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000340directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000341you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000342\file{lib/foo/\_\_init\_\_.py} exists.
343
Greg Ward1ecc2512000-04-19 22:36:24 +0000344Another possible convention is to put the \module{foo} package right in
345\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000346would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000347
Greg Ward16aafcd2000-04-09 04:06:44 +0000348\begin{verbatim}
349package_dir = {'foo': 'lib'}
350\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000351
Greg Ward59d382e2000-05-26 01:04:47 +0000352A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
353dictionary implicitly applies to all packages below \var{package}, so
354the \module{foo.bar} case is automatically handled here. In this
355example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
356to look for \file{lib/\_\_init\_\_.py} and
357\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
358\option{package\_dir} applies recursively, you must explicitly list all
359packages in \option{packages}: the Distutils will \emph{not} recursively
360scan your source tree looking for any directory with an
361\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000362
363
364\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000365\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000366
367For a small module distribution, you might prefer to list all modules
368rather than listing packages---especially the case of a single module
369that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000370simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000371slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000372
Greg Ward16aafcd2000-04-09 04:06:44 +0000373\begin{verbatim}
374py_modules = ['mod1', 'pkg.mod2']
375\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000376
Greg Ward16aafcd2000-04-09 04:06:44 +0000377This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000378other in the \module{pkg} package. Again, the default package/directory
379layout implies that these two modules can be found in \file{mod1.py} and
380\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000381And again, you can override the package/directory correspondence using
382the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000383
384
385\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000386\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000387
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000388% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000389Just as writing Python extension modules is a bit more complicated than
390writing pure Python modules, describing them to the Distutils is a bit
391more complicated. Unlike pure modules, it's not enough just to list
392modules or packages and expect the Distutils to go out and find the
393right files; you have to specify the extension name, source file(s), and
394any compile/link requirements (include directories, libraries to link
395with, etc.).
396
397All of this is done through another keyword argument to
398\function{setup()}, the \option{extensions} option. \option{extensions}
399is just a list of \class{Extension} instances, each of which describes a
400single extension module. Suppose your distribution includes a single
401extension, called \module{foo} and implemented by \file{foo.c}. If no
402additional instructions to the compiler/linker are needed, describing
403this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000404
Greg Ward2afffd42000-08-06 20:37:24 +0000405\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000406uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000407\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000408
Greg Ward2afffd42000-08-06 20:37:24 +0000409The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000410\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000411script for a module distribution that contains only this one extension
412and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000413
Greg Ward2afffd42000-08-06 20:37:24 +0000414\begin{verbatim}
415from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000416setup(name="foo", version="1.0",
417 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000418\end{verbatim}
419
420The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000421machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000422great deal of flexibility in describing Python extensions, which is
423explained in the following sections.
424
425
426\subsubsection{Extension names and packages}
427
428The first argument to the \class{Extension} constructor is always the
429name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000430
Greg Ward2afffd42000-08-06 20:37:24 +0000431\begin{verbatim}
432Extension("foo", ["src/foo1.c", "src/foo2.c"])
433\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000434
Greg Ward2afffd42000-08-06 20:37:24 +0000435describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000436
Greg Ward2afffd42000-08-06 20:37:24 +0000437\begin{verbatim}
438Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
439\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000440
Greg Ward2afffd42000-08-06 20:37:24 +0000441describes the same extension in the \module{pkg} package. The source
442files and resulting object code are identical in both cases; the only
443difference is where in the filesystem (and therefore where in Python's
444namespace hierarchy) the resulting extension lives.
445
446If you have a number of extensions all in the same package (or all under
447the same base package), use the \option{ext\_package} keyword argument
448to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000449
Greg Ward2afffd42000-08-06 20:37:24 +0000450\begin{verbatim}
451setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000452 ext_package="pkg",
453 ext_modules=[Extension("foo", ["foo.c"]),
454 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000455 )
456\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000457
Greg Ward2afffd42000-08-06 20:37:24 +0000458will compile \file{foo.c} to the extension \module{pkg.foo}, and
459\file{bar.c} to \module{pkg.subpkg.bar}.
460
461
462\subsubsection{Extension source files}
463
464The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000465source files. Since the Distutils currently only support C, \Cpp, and
466Objective-C extensions, these are normally C/\Cpp/Objective-C source
467files. (Be sure to use appropriate extensions to distinguish \Cpp\
468source files: \file{.cc} and \file{.cpp} seem to be recognized by both
469\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000470
471However, you can also include SWIG interface (\file{.i}) files in the
472list; the \command{build\_ext} command knows how to deal with SWIG
473extensions: it will run SWIG on the interface file and compile the
474resulting C/C++ file into your extension.
475
476\XXX{SWIG support is rough around the edges and largely untested;
477 especially SWIG support of C++ extensions! Explain in more detail
478 here when the interface firms up.}
479
480On some platforms, you can include non-source files that are processed
481by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000482means Windows message text (\file{.mc}) files and resource definition
483(\file{.rc}) files for Visual C++. These will be compiled to binary resource
484(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000485
486
487\subsubsection{Preprocessor options}
488
489Three optional arguments to \class{Extension} will help if you need to
490specify include directories to search or preprocessor macros to
491define/undefine: \code{include\_dirs}, \code{define\_macros}, and
492\code{undef\_macros}.
493
494For example, if your extension requires header files in the
495\file{include} directory under your distribution root, use the
496\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000497
Greg Ward2afffd42000-08-06 20:37:24 +0000498\begin{verbatim}
499Extension("foo", ["foo.c"], include_dirs=["include"])
500\end{verbatim}
501
502You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000503extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000504\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000505
Greg Ward2afffd42000-08-06 20:37:24 +0000506\begin{verbatim}
507Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
508\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000509
Greg Ward2afffd42000-08-06 20:37:24 +0000510You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000511distribute your code: it's probably better to write C code like
512\begin{verbatim}
513#include <X11/Xlib.h>
514\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000515
516If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000517you can take advantage of the fact that header files are installed in a
518consistent way by the Distutils \command{install\_header} command. For
519example, the Numerical Python header files are installed (on a standard
520Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
521(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000522installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000523directory---\file{/usr/local/include/python1.5} in this case---is always
524included in the search path when building Python extensions, the best
525approach is to write C code like
526\begin{verbatim}
527#include <Numerical/arrayobject.h>
528\end{verbatim}
529If you must put the \file{Numerical} include directory right into your
530header search path, though, you can find that directory using the
531Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000532
Greg Ward2afffd42000-08-06 20:37:24 +0000533\begin{verbatim}
534from distutils.sysconfig import get_python_inc
535incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
536setup(...,
537 Extension(..., include_dirs=[incdir]))
538\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000539
Greg Ward2afffd42000-08-06 20:37:24 +0000540Even though this is quite portable---it will work on any Python
541installation, regardless of platform---it's probably easier to just
542write your C code in the sensible way.
543
544You can define and undefine pre-processor macros with the
545\code{define\_macros} and \code{undef\_macros} options.
546\code{define\_macros} takes a list of \code{(name, value)} tuples, where
547\code{name} is the name of the macro to define (a string) and
548\code{value} is its value: either a string or \code{None}. (Defining a
549macro \code{FOO} to \code{None} is the equivalent of a bare
550\code{\#define FOO} in your C source: with most compilers, this sets
551\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
552a list of macros to undefine.
553
554For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000555
Greg Ward2afffd42000-08-06 20:37:24 +0000556\begin{verbatim}
557Extension(...,
558 define_macros=[('NDEBUG', '1')],
559 ('HAVE_STRFTIME', None),
560 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
561\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000562
Greg Ward2afffd42000-08-06 20:37:24 +0000563is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000564
Greg Ward2afffd42000-08-06 20:37:24 +0000565\begin{verbatim}
566#define NDEBUG 1
567#define HAVE_STRFTIME
568#undef HAVE_FOO
569#undef HAVE_BAR
570\end{verbatim}
571
572
573\subsubsection{Library options}
574
575You can also specify the libraries to link against when building your
576extension, and the directories to search for those libraries. The
577\code{libraries} option is a list of libraries to link against,
578\code{library\_dirs} is a list of directories to search for libraries at
579link-time, and \code{runtime\_library\_dirs} is a list of directories to
580search for shared (dynamically loaded) libraries at run-time.
581
582For example, if you need to link against libraries known to be in the
583standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000584
Greg Ward2afffd42000-08-06 20:37:24 +0000585\begin{verbatim}
586Extension(...,
587 libraries=["gdbm", "readline"])
588\end{verbatim}
589
590If you need to link with libraries in a non-standard location, you'll
591have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000592
Greg Ward2afffd42000-08-06 20:37:24 +0000593\begin{verbatim}
594Extension(...,
595 library_dirs=["/usr/X11R6/lib"],
596 libraries=["X11", "Xt"])
597\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000598
Greg Ward2afffd42000-08-06 20:37:24 +0000599(Again, this sort of non-portable construct should be avoided if you
600intend to distribute your code.)
601
Thomas Heller5f52f722001-02-19 17:48:03 +0000602\XXX{Should mention clib libraries here or somewhere else!}
603
604\subsubsection{Other options}
605
606There are still some other options which can be used to handle special
607cases.
608
609The \option{extra\_objects} option is a list of object files to be passed
610to the linker. These files must not have extensions, as the default
611extension for the compiler is used.
612
613\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000614to specify additional command line options for the respective compiler and
615linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000616
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000617\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000618of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000619is not needed when building compiled extensions: Distutils
620will automatically add \code{initmodule}
621to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000622
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000623\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000624So far we have been dealing with pure and non-pure Python modules,
625which are usually not run by themselves but imported by scripts.
626
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000627Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000628started from the command line. Scripts don't require Distutils to do
629anything very complicated. The only clever feature is that if the
630first line of the script starts with \code{\#!} and contains the word
631``python'', the Distutils will adjust the first line to refer to the
632current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000633
634The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000635in this way. From the PyXML setup script:
636
637\begin{verbatim}
638setup (...
639 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
640 )
641\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000642
643
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000644\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000645
Thomas Heller5f52f722001-02-19 17:48:03 +0000646The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000647files needed by the module distribution: configuration files, message
648catalogs, data files, anything which doesn't fit in the previous
649categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000650
Fred Drake632bda32002-03-08 22:02:06 +0000651\option{data\_files} specifies a sequence of (\var{directory},
652\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000653
Thomas Heller5f52f722001-02-19 17:48:03 +0000654\begin{verbatim}
655setup(...
656 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000657 ('config', ['cfg/data.cfg']),
658 ('/etc/init.d', ['init-script'])]
659 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000660\end{verbatim}
661
662Note that you can specify the directory names where the data files
663will be installed, but you cannot rename the data files themselves.
664
Fred Drake632bda32002-03-08 22:02:06 +0000665Each (\var{directory}, \var{files}) pair in the sequence specifies the
666installation directory and the files to install there. If
667\var{directory} is a relative path, it is interpreted relative to the
668installation prefix (Python's \code{sys.prefix} for pure-Python
669packages, \code{sys.exec_prefix} for packages that contain extension
670modules). Each file name in \var{files} is interpreted relative to
671the \file{setup.py} script at the top of the package source
672distribution. No directory information from \var{files} is used to
673determine the final location of the installed file; only the name of
674the file is used.
675
Thomas Heller5f52f722001-02-19 17:48:03 +0000676You can specify the \option{data\_files} options as a simple sequence
677of files without specifying a target directory, but this is not recommended,
678and the \command{install} command will print a warning in this case.
679To install data files directly in the target directory, an empty
680string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000681
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000682\subsection{Additional meta-data}
683\label{meta-data}
684
685The setup script may include additional meta-data beyond the name and
686version. This information includes:
687
Fred Drakec440af52003-04-25 16:43:28 +0000688\begin{tableiv}{l|l|l|c}{code}%
689 {Meta-Data}{Description}{Value}{Notes}
690 \lineiv{name}{name of the package}
691 {short string}{(1)}
692 \lineiv{version}{version of this release}
693 {short string}{(1)(2)}
694 \lineiv{author}{package author's name}
695 {short string}{(3)}
696 \lineiv{author_email}{email address of the package author}
697 {email address}{(3)}
698 \lineiv{maintainer}{package maintainer's name}
699 {short string}{(3)}
700 \lineiv{maintainer_email}{email address of the package maintainer}
701 {email address}{(3)}
702 \lineiv{url}{home page for the package}
703 {URL}{(1)}
704 \lineiv{description}{short, summary description of the package}
705 {short string}{}
706 \lineiv{long_description}{longer description of the package}
707 {long string}{}
708 \lineiv{download_url}{location where the package may be downloaded}
709 {URL}{(4)}
710 \lineiv{classifiers}{a list of Trove classifiers}
711 {list of strings}{(4)}
712\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000713
714\noindent Notes:
715\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000716\item[(1)] These fields are required.
717\item[(2)] It is recommended that versions take the form
718 \emph{major.minor\optional{.patch\optional{.sub}}}.
719\item[(3)] Either the author or the maintainer must be identified.
720\item[(4)] These fields should not be used if your package is to be
721 compatible with Python versions prior to 2.2.3 or 2.3. The list is
722 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
723
724\item["short string"] A single line of text, not more than 200 characters.
725\item["long string"] Multiple lines of plain text in ReStructuredText
726 format (see \url{http://docutils.sf.net/}).
727\item["list of strings"] See below.
728\end{description}
729
730None of the string values may be Unicode.
731
732Encoding the version information is an art in itself. Python packages
733generally adhere to the version format
734\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7350 for
736initial, experimental releases of software. It is incremented for
737releases that represent major milestones in a package. The minor
738number is incremented when important new features are added to the
739package. The patch number increments when bug-fix releases are
740made. Additional trailing version information is sometimes used to
741indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
742where functionality and API may change), "b1,b2,...,bN" (for beta
743releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
744pre-release release testing). Some examples:
745
746\begin{description}
747\item[0.1.0] the first, experimental release of a package
748\item[1.0.1a2] the second alpha release of the first patch version of 1.0
749\end{description}
750
751\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000752
753\begin{verbatim}
754setup(...
Fred Drakec440af52003-04-25 16:43:28 +0000755 classifiers = [
Fred Drake2a046232003-03-31 16:23:09 +0000756 'Development Status :: 4 - Beta',
757 'Environment :: Console',
758 'Environment :: Web Environment',
759 'Intended Audience :: End Users/Desktop',
760 'Intended Audience :: Developers',
761 'Intended Audience :: System Administrators',
762 'License :: OSI Approved :: Python Software Foundation License',
763 'Operating System :: MacOS :: MacOS X',
764 'Operating System :: Microsoft :: Windows',
765 'Operating System :: POSIX',
766 'Programming Language :: Python',
767 'Topic :: Communications :: Email',
768 'Topic :: Office/Business',
769 'Topic :: Software Development :: Bug Tracking',
770 ],
Fred Drake2a046232003-03-31 16:23:09 +0000771 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000772\end{verbatim}
773
Fred Drakec440af52003-04-25 16:43:28 +0000774If you wish to include classifiers in your \file{setup.py} file and also
775wish to remain backwards-compatible with Python releases prior to 2.2.3,
776then you can include the following code fragment in your \file{setup.py}
777before the \code{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000778
779\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000780# patch distutils if it can't cope with the "classifiers" or
781# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000782if sys.version < '2.2.3':
783 from distutils.dist import DistributionMetadata
784 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000785 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000786\end{verbatim}
787
Greg Ward16aafcd2000-04-09 04:06:44 +0000788
789\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000790\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000791
Greg Ward16aafcd2000-04-09 04:06:44 +0000792Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000793distribution \emph{a priori}: you may need to get some information from
794the user, or from the user's system, in order to proceed. As long as
795that information is fairly simple---a list of directories to search for
796C header files or libraries, for example---then providing a
797configuration file, \file{setup.cfg}, for users to edit is a cheap and
798easy way to solicit it. Configuration files also let you provide
799default values for any command option, which the installer can then
800override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000801
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000802% (If you have more advanced needs, such as determining which extensions
803% to build based on what capabilities are present on the target system,
804% then you need the Distutils ``auto-configuration'' facility. This
805% started to appear in Distutils 0.9 but, as of this writing, isn't mature
806% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000807
Greg Ward47f99a62000-09-04 20:07:15 +0000808The setup configuration file is a useful middle-ground between the setup
809script---which, ideally, would be opaque to installers\footnote{This
810 ideal probably won't be achieved until auto-configuration is fully
811 supported by the Distutils.}---and the command-line to the setup
812script, which is outside of your control and entirely up to the
813installer. In fact, \file{setup.cfg} (and any other Distutils
814configuration files present on the target system) are processed after
815the contents of the setup script, but before the command-line. This has
816several useful consequences:
817\begin{itemize}
818\item installers can override some of what you put in \file{setup.py} by
819 editing \file{setup.cfg}
820\item you can provide non-standard defaults for options that are not
821 easily set in \file{setup.py}
822\item installers can override anything in \file{setup.cfg} using the
823 command-line options to \file{setup.py}
824\end{itemize}
825
826The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000827
Greg Ward47f99a62000-09-04 20:07:15 +0000828\begin{verbatim}
829[command]
830option=value
831...
832\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000833
Greg Ward47f99a62000-09-04 20:07:15 +0000834where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000835\command{build\_py}, \command{install}), and \var{option} is one of
836the options that command supports. Any number of options can be
837supplied for each command, and any number of command sections can be
838included in the file. Blank lines are ignored, as are comments, which
839run from a \character{\#} character until the end of the line. Long
840option values can be split across multiple lines simply by indenting
841the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000842
843You can find out the list of options supported by a particular command
844with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000845
Greg Ward47f99a62000-09-04 20:07:15 +0000846\begin{verbatim}
847> python setup.py --help build_ext
848[...]
849Options for 'build_ext' command:
850 --build-lib (-b) directory for compiled extension modules
851 --build-temp (-t) directory for temporary files (build by-products)
852 --inplace (-i) ignore build-lib and put compiled extensions into the
853 source directory alongside your pure Python modules
854 --include-dirs (-I) list of directories to search for header files
855 --define (-D) C preprocessor macros to define
856 --undef (-U) C preprocessor macros to undefine
857[...]
858\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000859
Greg Ward47f99a62000-09-04 20:07:15 +0000860Note that an option spelled \longprogramopt{foo-bar} on the command-line
861is spelled \option{foo\_bar} in configuration files.
862
863For example, say you want your extensions to be built
864``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000865want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000866in the same source directory as your pure Python modules
867\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
868\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000869
Greg Ward47f99a62000-09-04 20:07:15 +0000870\begin{verbatim}
871python setup.py build_ext --inplace
872\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000873
Greg Ward47f99a62000-09-04 20:07:15 +0000874But this requires that you always specify the \command{build\_ext}
875command explicitly, and remember to provide \longprogramopt{inplace}.
876An easier way is to ``set and forget'' this option, by encoding it in
877\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000878
Greg Ward47f99a62000-09-04 20:07:15 +0000879\begin{verbatim}
880[build_ext]
881inplace=1
882\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000883
Greg Ward47f99a62000-09-04 20:07:15 +0000884This will affect all builds of this module distribution, whether or not
885you explcitly specify \command{build\_ext}. If you include
886\file{setup.cfg} in your source distribution, it will also affect
887end-user builds---which is probably a bad idea for this option, since
888always building extensions in-place would break installation of the
889module distribution. In certain peculiar cases, though, modules are
890built right in their installation directory, so this is conceivably a
891useful ability. (Distributing extensions that expect to be built in
892their installation directory is almost always a bad idea, though.)
893
894Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000895change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000896everything required to generate a ``spec'' file for creating an RPM
897distribution. Some of this information comes from the setup script, and
898some is automatically generated by the Distutils (such as the list of
899files installed). But some of it has to be supplied as options to
900\command{bdist\_rpm}, which would be very tedious to do on the
901command-line for every run. Hence, here is a snippet from the
902Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000903
Greg Ward47f99a62000-09-04 20:07:15 +0000904\begin{verbatim}
905[bdist_rpm]
906release = 1
907packager = Greg Ward <gward@python.net>
908doc_files = CHANGES.txt
909 README.txt
910 USAGE.txt
911 doc/
912 examples/
913\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000914
Greg Ward47f99a62000-09-04 20:07:15 +0000915Note that the \option{doc\_files} option is simply a
916whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000917
918
Fred Drakea09262e2001-03-01 18:35:43 +0000919\begin{seealso}
920 \seetitle[../inst/config-syntax.html]{Installing Python
921 Modules}{More information on the configuration files is
922 available in the manual for system administrators.}
923\end{seealso}
924
925
Greg Ward16aafcd2000-04-09 04:06:44 +0000926\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000927\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000928
Greg Warde78298a2000-04-28 17:12:24 +0000929As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000930\command{sdist} command to create a source distribution. In the
931simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000932
Greg Ward16aafcd2000-04-09 04:06:44 +0000933\begin{verbatim}
934python setup.py sdist
935\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000936
Greg Ward19c67f82000-06-24 01:33:16 +0000937(assuming you haven't specified any \command{sdist} options in the setup
938script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000939default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000940tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
941\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000942
Greg Wardd5767a52000-04-19 22:48:09 +0000943You can specify as many formats as you like using the
944\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000945
Greg Ward16aafcd2000-04-09 04:06:44 +0000946\begin{verbatim}
947python setup.py sdist --formats=gztar,zip
948\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000949
Greg Ward16aafcd2000-04-09 04:06:44 +0000950to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000951\begin{tableiii}{l|l|c}{code}%
952 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000953 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
954 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000955 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000956 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000957 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000958\end{tableiii}
959
960\noindent Notes:
961\begin{description}
962\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000963\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000964\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000965 \module{zipfile} module (part of the standard Python library since
966 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000967\item[(4)] requires external utilities: \program{tar} and possibly one
968 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000969\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000970
971
Greg Ward54589d42000-09-06 01:37:35 +0000972
973\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000974\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000975
Greg Ward54589d42000-09-06 01:37:35 +0000976If you don't supply an explicit list of files (or instructions on how to
977generate one), the \command{sdist} command puts a minimal default set
978into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000979\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000980\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000981 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000982\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000983 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000984 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000985\item anything that looks like a test script: \file{test/test*.py}
986 (currently, the Distutils don't do anything with test scripts except
987 include them in source distributions, but in the future there will be
988 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000989\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
990 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000991\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000992
Greg Ward16aafcd2000-04-09 04:06:44 +0000993Sometimes this is enough, but usually you will want to specify
994additional files to distribute. The typical way to do this is to write
995a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000996manifest template is just a list of instructions for how to generate
997your manifest file, \file{MANIFEST}, which is the exact list of files to
998include in your source distribution. The \command{sdist} command
999processes this template and generates a manifest based on its
1000instructions and what it finds in the filesystem.
1001
1002If you prefer to roll your own manifest file, the format is simple: one
1003filename per line, regular files (or symlinks to them) only. If you do
1004supply your own \file{MANIFEST}, you must specify everything: the
1005default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001006
1007The manifest template has one command per line, where each command
1008specifies a set of files to include or exclude from the source
1009distribution. For an example, again we turn to the Distutils' own
1010manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001011
Greg Ward16aafcd2000-04-09 04:06:44 +00001012\begin{verbatim}
1013include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001014recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001015prune examples/sample?/build
1016\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001017
Greg Ward16aafcd2000-04-09 04:06:44 +00001018The meanings should be fairly clear: include all files in the
1019distribution root matching \code{*.txt}, all files anywhere under the
1020\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001021exclude all directories matching \code{examples/sample?/build}. All of
1022this is done \emph{after} the standard include set, so you can exclude
1023files from the standard set with explicit instructions in the manifest
1024template. (Or, you can use the \longprogramopt{no-defaults} option to
1025disable the standard set entirely.) There are several other commands
1026available in the manifest template mini-language; see
1027section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001028
Greg Ward54589d42000-09-06 01:37:35 +00001029The order of commands in the manifest template matters: initially, we
1030have the list of default files as described above, and each command in
1031the template adds to or removes from that list of files. Once we have
1032fully processed the manifest template, we remove files that should not
1033be included in the source distribution:
1034\begin{itemize}
1035\item all files in the Distutils ``build'' tree (default \file{build/})
1036\item all files in directories named \file{RCS} or \file{CVS}
1037\end{itemize}
1038Now we have our complete list of files, which is written to the manifest
1039for future reference, and then used to build the source distribution
1040archive(s).
1041
1042You can disable the default set of included files with the
1043\longprogramopt{no-defaults} option, and you can disable the standard
1044exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001045
Greg Ward46b98e32000-04-14 01:53:36 +00001046Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001047\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001048Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001049\begin{enumerate}
1050\item include all Python source files in the \file{distutils} and
1051 \file{distutils/command} subdirectories (because packages
1052 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001053 \option{packages} option in the setup script---see
1054 section~\ref{setup-script})
1055\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1056 (standard files)
1057\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001058\item include \file{*.txt} in the distribution root (this will find
1059 \file{README.txt} a second time, but such redundancies are weeded out
1060 later)
Greg Ward54589d42000-09-06 01:37:35 +00001061\item include anything matching \file{*.txt} or \file{*.py} in the
1062 sub-tree under \file{examples},
1063\item exclude all files in the sub-trees starting at directories
1064 matching \file{examples/sample?/build}---this may exclude files
1065 included by the previous two steps, so it's important that the
1066 \code{prune} command in the manifest template comes after the
1067 \code{recursive-include} command
1068\item exclude the entire \file{build} tree, and any \file{RCS} or
1069 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001070\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001071Just like in the setup script, file and directory names in the manifest
1072template should always be slash-separated; the Distutils will take care
1073of converting them to the standard representation on your platform.
1074That way, the manifest template is portable across operating systems.
1075
Greg Ward16aafcd2000-04-09 04:06:44 +00001076
1077\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001078\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001079
1080The normal course of operations for the \command{sdist} command is as
1081follows:
1082\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001083\item if the manifest file, \file{MANIFEST} doesn't exist, read
1084 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001085\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001086 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001087\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1088 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1089 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001090\item use the list of files now in \file{MANIFEST} (either just
1091 generated or read in) to create the source distribution archive(s)
1092\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001093There are a couple of options that modify this behaviour. First, use
1094the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001095disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001096
Greg Ward54589d42000-09-06 01:37:35 +00001097Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001098example, if you have added or removed files or directories that match an
1099existing pattern in the manifest template, you should regenerate the
1100manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001101
Greg Ward16aafcd2000-04-09 04:06:44 +00001102\begin{verbatim}
1103python setup.py sdist --force-manifest
1104\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001105
1106Or, you might just want to (re)generate the manifest, but not create a
1107source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001108
Greg Ward16aafcd2000-04-09 04:06:44 +00001109\begin{verbatim}
1110python setup.py sdist --manifest-only
1111\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001112
Greg Ward54589d42000-09-06 01:37:35 +00001113\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1114\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1115\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001116
1117
1118\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001119\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001120
Greg Ward46b98e32000-04-14 01:53:36 +00001121A ``built distribution'' is what you're probably used to thinking of
1122either as a ``binary package'' or an ``installer'' (depending on your
1123background). It's not necessarily binary, though, because it might
1124contain only Python source code and/or byte-code; and we don't call it a
1125package, because that word is already spoken for in Python. (And
1126``installer'' is a term specific to the Windows world. \XXX{do Mac
1127 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001128
Greg Ward46b98e32000-04-14 01:53:36 +00001129A built distribution is how you make life as easy as possible for
1130installers of your module distribution: for users of RPM-based Linux
1131systems, it's a binary RPM; for Windows users, it's an executable
1132installer; for Debian-based Linux users, it's a Debian package; and so
1133forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001134distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001135designed to enable module developers to concentrate on their
1136specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001137intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001138distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001139are packagers.
1140
1141Of course, the module developer could be his own packager; or the
1142packager could be a volunteer ``out there'' somewhere who has access to
1143a platform which the original developer does not; or it could be
1144software periodically grabbing new source distributions and turning them
1145into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001146access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001147setup script and the \command{bdist} command family to generate built
1148distributions.
1149
1150As a simple example, if I run the following command in the Distutils
1151source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001152
Greg Ward46b98e32000-04-14 01:53:36 +00001153\begin{verbatim}
1154python setup.py bdist
1155\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001156
Greg Ward46b98e32000-04-14 01:53:36 +00001157then the Distutils builds my module distribution (the Distutils itself
1158in this case), does a ``fake'' installation (also in the \file{build}
1159directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001160platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001161file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001162file is considered ``dumb'' because it has to be unpacked in a specific
1163location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001164
Fred Drakeeff9a872000-10-26 16:41:03 +00001165Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001166\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001167from the right place installs the Distutils just as though you had
1168downloaded the source distribution and run \code{python setup.py
1169 install}. (The ``right place'' is either the root of the filesystem or
1170Python's \filevar{prefix} directory, depending on the options given to
1171the \command{bdist\_dumb} command; the default is to make dumb
1172distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001173
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001174Obviously, for pure Python distributions, this isn't any simpler than
1175just running \code{python setup.py install}---but for non-pure
1176distributions, which include extensions that would need to be
1177compiled, it can mean the difference between someone being able to use
1178your extensions or not. And creating ``smart'' built distributions,
1179such as an RPM package or an executable installer for Windows, is far
1180more convenient for users even if your distribution doesn't include
1181any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001182
Greg Wardb6528972000-09-07 02:40:37 +00001183The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001184similar to the \command{sdist} command, which you can use to select the
1185types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001186
Greg Ward46b98e32000-04-14 01:53:36 +00001187\begin{verbatim}
1188python setup.py bdist --format=zip
1189\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001190
Fred Drakeeff9a872000-10-26 16:41:03 +00001191would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001192\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001193unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001194
1195The available formats for built distributions are:
1196\begin{tableiii}{l|l|c}{code}%
1197 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001198 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1199 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1200 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1201 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1202 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001203 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1204 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1205 \lineiii{rpm}{RPM}{(5)}
1206% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001207 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001208\end{tableiii}
1209
1210\noindent Notes:
1211\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001212\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001213\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001214\item[(3)] requires external utilities: \program{tar} and possibly one
1215 of \program{gzip}, \program{bzip2}, or \program{compress}
1216\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001217 \module{zipfile} module (part of the standard Python library since
1218 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001219\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1220 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001221\end{description}
1222
1223You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001224\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001225directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001226\command{bdist} ``sub-commands'' actually generate several similar
1227formats; for instance, the \command{bdist\_dumb} command generates all
1228the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1229\code{zip}), and \command{bdist\_rpm} generates both binary and source
1230RPMs. The \command{bdist} sub-commands, and the formats generated by
1231each, are:
1232\begin{tableii}{l|l}{command}%
1233 {Command}{Formats}
1234 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1235 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001236 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001237\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001238
Greg Wardb6528972000-09-07 02:40:37 +00001239The following sections give details on the individual \command{bdist\_*}
1240commands.
1241
1242
1243\subsection{Creating dumb built distributions}
1244\label{creating-dumb}
1245
1246\XXX{Need to document absolute vs. prefix-relative packages here, but
1247 first I have to implement it!}
1248
1249
1250\subsection{Creating RPM packages}
1251\label{creating-rpms}
1252
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001253The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001254Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1255RPM-based Linux distributions) is your usual environment, creating RPM
1256packages for other users of that same distribution is trivial.
1257Depending on the complexity of your module distribution and differences
1258between Linux distributions, you may also be able to create RPMs that
1259work on different RPM-based distributions.
1260
1261The usual way to create an RPM of your module distribution is to run the
1262\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001263
Greg Wardb6528972000-09-07 02:40:37 +00001264\begin{verbatim}
1265python setup.py bdist_rpm
1266\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001267
Greg Wardb6528972000-09-07 02:40:37 +00001268or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001269
Greg Wardb6528972000-09-07 02:40:37 +00001270\begin{verbatim}
1271python setup.py bdist --formats=rpm
1272\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001273
Greg Wardb6528972000-09-07 02:40:37 +00001274The former allows you to specify RPM-specific options; the latter allows
1275you to easily specify multiple formats in one run. If you need to do
1276both, you can explicitly specify multiple \command{bdist\_*} commands
1277and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001278
Greg Wardb6528972000-09-07 02:40:37 +00001279\begin{verbatim}
1280python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1281 bdist_wininst --target_version="2.0"
1282\end{verbatim}
1283
1284Creating RPM packages is driven by a \file{.spec} file, much as using
1285the Distutils is driven by the setup script. To make your life easier,
1286the \command{bdist\_rpm} command normally creates a \file{.spec} file
1287based on the information you supply in the setup script, on the command
1288line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001289sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001290script as follows:
1291\begin{tableii}{l|l}{textrm}%
1292 {RPM \file{.spec} file option or section}{Distutils setup script option}
1293 \lineii{Name}{\option{name}}
1294 \lineii{Summary (in preamble)}{\option{description}}
1295 \lineii{Version}{\option{version}}
1296 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1297 \option{maintainer} and \option{maintainer\_email}}
1298 \lineii{Copyright}{\option{licence}}
1299 \lineii{Url}{\option{url}}
1300 \lineii{\%description (section)}{\option{long\_description}}
1301\end{tableii}
1302
1303Additionally, there many options in \file{.spec} files that don't have
1304corresponding options in the setup script. Most of these are handled
1305through options to the \command{bdist\_rpm} command as follows:
1306\begin{tableiii}{l|l|l}{textrm}%
1307 {RPM \file{.spec} file option or section}%
1308 {\command{bdist\_rpm} option}%
1309 {default value}
1310 \lineiii{Release}{\option{release}}{``1''}
1311 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1312 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001313 \lineiii{Packager}{\option{packager}}{(none)}
1314 \lineiii{Provides}{\option{provides}}{(none)}
1315 \lineiii{Requires}{\option{requires}}{(none)}
1316 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1317 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001318 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1319 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1320 \lineiii{Icon}{\option{icon}}{(none)}
1321\end{tableiii}
1322Obviously, supplying even a few of these options on the command-line
1323would be tedious and error-prone, so it's usually best to put them in
1324the setup configuration file, \file{setup.cfg}---see
1325section~\ref{setup-config}. If you distribute or package many Python
1326module distributions, you might want to put options that apply to all of
1327them in your personal Distutils configuration file
1328(\file{\textasciitilde/.pydistutils.cfg}).
1329
1330There are three steps to building a binary RPM package, all of which are
1331handled automatically by the Distutils:
1332\begin{enumerate}
1333\item create a \file{.spec} file, which describes the package (analogous
1334 to the Distutils setup script; in fact, much of the information in the
1335 setup script winds up in the \file{.spec} file)
1336\item create the source RPM
1337\item create the ``binary'' RPM (which may or may not contain binary
1338 code, depending on whether your module distribution contains Python
1339 extensions)
1340\end{enumerate}
1341Normally, RPM bundles the last two steps together; when you use the
1342Distutils, all three steps are typically bundled together.
1343
1344If you wish, you can separate these three steps. You can use the
1345\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1346create the \file{.spec} file and exit; in this case, the \file{.spec}
1347file will be written to the ``distribution directory''---normally
1348\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1349option. (Normally, the \file{.spec} file winds up deep in the ``build
1350tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1351
1352\XXX{this isn't implemented yet---is it needed?!}
1353You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001354\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001355\longprogramopt{spec-only}, this gives you an opportunity to customize
1356the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001357
Greg Wardb6528972000-09-07 02:40:37 +00001358\begin{verbatim}
1359> python setup.py bdist_rpm --spec-only
1360# ...edit dist/FooBar-1.0.spec
1361> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1362\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001363
Greg Wardb6528972000-09-07 02:40:37 +00001364(Although a better way to do this is probably to override the standard
1365\command{bdist\_rpm} command with one that writes whatever else you want
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +00001366to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001367
1368
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001369\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001370\label{creating-wininst}
1371
Thomas Hellere61f3652002-11-15 20:13:26 +00001372Executable installers are the natural format for binary distributions
1373on Windows. They display a nice graphical user interface, display
1374some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001375from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001376options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001377
Thomas Hellere61f3652002-11-15 20:13:26 +00001378Since the metadata is taken from the setup script, creating Windows
1379installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001380
Thomas Heller5f52f722001-02-19 17:48:03 +00001381\begin{verbatim}
1382python setup.py bdist_wininst
1383\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001384
Thomas Heller36343f62002-11-15 19:20:56 +00001385or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001386
Thomas Heller5f52f722001-02-19 17:48:03 +00001387\begin{verbatim}
1388python setup.py bdist --formats=wininst
1389\end{verbatim}
1390
Thomas Hellere61f3652002-11-15 20:13:26 +00001391If you have a pure module distribution (only containing pure Python
1392modules and packages), the resulting installer will be version
1393independent and have a name like \file{foo-1.0.win32.exe}. These
1394installers can even be created on \UNIX{} or MacOS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001395
1396If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001397created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001398The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001399\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001400for every Python version you want to support.
1401
1402The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001403installation on the target system in normal and optimizing mode. If
1404you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001405\command{bdist_wininst} command with the
1406\longprogramopt{no-target-compile} and/or the
1407\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001408
Fred Drake0e9bfa32002-11-15 20:34:52 +00001409By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001410when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001411a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001412
1413The installer will also display a large title on the desktop
1414background window when it is run, which is constructed from the name
1415of your distribution and the version number. This can be changed to
1416another text by using the \longprogramopt{title} option.
1417
1418The installer file will be written to the ``distribution directory''
1419--- normally \file{dist/}, but customizable with the
1420\longprogramopt{dist-dir} option.
1421
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001422\subsubsection{The Postinstallation script}
1423\label{postinstallation-script}
1424
1425Starting with Python 2.3, a postinstallation script can be specified
1426which the \longprogramopt{install-script} option. The basename of the
1427script must be specified, and the script filename must also be listed
1428in the scripts argument to the setup function.
1429
1430This script will be run at installation time on the target system
1431after all the files have been copied, with argv[1] set to '-install',
1432and again at uninstallation time before the files are removed with argv[1]
1433set to '-remove'.
1434
1435The installation script runs embedded in the windows installer, every
1436output (sys.stdout, sys.stderr) is redirected into a buffer and will
1437be displayed in the GUI after the script has finished.
1438
1439Some functions especially useful in this context are available in the
1440installation script.
1441
1442\begin{verbatim}
1443dir_created(pathname)
1444file_created(pathname)
1445\end{verbatim}
1446
1447These functions should be called when a directory or file is created
1448by the postinstall script at installation time. It will register the
1449pathname with the uninstaller, so that it will be removed when the
1450distribution is uninstalled. To be safe, directories are only removed
1451if they are empty.
1452
1453\begin{verbatim}
1454get_special_folder_path(csidl_string)
1455\end{verbatim}
1456
1457This function can be used to retrieve special folder locations on
1458Windows like the Start Menu or the Desktop. It returns the full path
1459to the folder. 'csidl_string' must be on of the following strings:
1460
1461\begin{verbatim}
1462"CSIDL_APPDATA"
1463
1464"CSIDL_COMMON_STARTMENU"
1465"CSIDL_STARTMENU"
1466
1467"CSIDL_COMMON_DESKTOPDIRECTORY"
1468"CSIDL_DESKTOPDIRECTORY"
1469
1470"CSIDL_COMMON_STARTUP"
1471"CSIDL_STARTUP"
1472
1473"CSIDL_COMMON_PROGRAMS"
1474"CSIDL_PROGRAMS"
1475
1476"CSIDL_FONTS"
1477\end{verbatim}
1478
1479If the folder cannot be retrieved, OSError is raised.
1480
1481Which folders are available depends on the exact Windows version, and probably
1482also the configuration. For details refer to Microsoft's documentation of the
Thomas Heller63b4dd32002-12-12 19:35:00 +00001483\code{SHGetSpecialFolderPath} function.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001484
1485\begin{verbatim}
1486create_shortcut(target, description, filename[, arguments[,
1487 workdir[, iconpath[, iconindex]]]])
1488\end{verbatim}
1489
1490This function creates a shortcut.
Thomas Heller63b4dd32002-12-12 19:35:00 +00001491\var{target} is the path to the program to be started by the shortcut.
1492\var{description} is the description of the sortcut.
1493\var{filename} is the title of the shortcut that the user will see.
1494\var{arguments} specifies the command line arguments, if any.
1495\var{workdir} is the working directory for the program.
1496\var{iconpath} is the file containing the icon for the shortcut,
1497and \var{iconindex} is the index of the icon in the file
1498\var{iconpath}. Again, for details consult the Microsoft
1499documentation for the \code{IShellLink} interface.
Greg Wardb6528972000-09-07 02:40:37 +00001500
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001501\section{Registering with the Package Index}
1502\label{package-index}
1503
1504The Python Package Index (PyPI) holds meta-data describing distributions
1505packaged with distutils. The distutils command \command{register} is
1506used to submit your distribution's meta-data to the index. It is invoked
1507as follows:
1508
1509\begin{verbatim}
1510python setup.py register
1511\end{verbatim}
1512
1513Distutils will respond with the following prompt:
1514
1515\begin{verbatim}
1516running register
1517We need to know who you are, so please choose either:
1518 1. use your existing login,
1519 2. register as a new user,
1520 3. have the server generate a new password for you (and email it to you), or
1521 4. quit
1522Your selection [default 1]:
1523\end{verbatim}
1524
1525\noindent Note: if your username and password are saved locally, you will
1526not see this menu.
1527
1528If you have not registered with PyPI, then you will need to do so now. You
1529should choose option 2, and enter your details as required. Soon after
1530submitting your details, you will receive an email which will be used to
1531confirm your registration.
1532
1533Once you are registered, you may choose option 1 from the menu. You will
1534be prompted for your PyPI username and password, and \command{register}
1535will then submit your meta-data to the index.
1536
1537You may submit any number of versions of your distribution to the index. If
1538you alter the meta-data for a particular version, you may submit it again
1539and the index will be updated.
1540
1541PyPI holds a record for each (name, version) combination submitted. The
1542first user to submit information for a given name is designated the Owner
1543of that name. They may submit changes through the \command{register}
1544command or through the web interface. They may also designate other users
1545as Owners or Maintainers. Maintainers may edit the package information, but
1546not designate other Owners or Maintainers.
1547
1548By default PyPI will list all versions of a given package. To hide certain
1549versions, the Hidden property should be set to yes. This must be edited
1550through the web interface.
1551
1552
1553
Greg Ward007c04a2002-05-10 14:45:59 +00001554\section{Examples}
1555\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001556
Greg Ward007c04a2002-05-10 14:45:59 +00001557\subsection{Pure Python distribution (by module)}
1558\label{pure-mod}
1559
1560If you're just distributing a couple of modules, especially if they
1561don't live in a particular package, you can specify them individually
1562using the \option{py\_modules} option in the setup script.
1563
1564In the simplest case, you'll have two files to worry about: a setup
1565script and the single module you're distributing, \file{foo.py} in this
1566example:
1567\begin{verbatim}
1568<root>/
1569 setup.py
1570 foo.py
1571\end{verbatim}
1572(In all diagrams in this section, \verb|<root>| will refer to the
1573distribution root directory.) A minimal setup script to describe this
1574situation would be:
1575\begin{verbatim}
1576from distutils.core import setup
1577setup(name = "foo", version = "1.0",
1578 py_modules = ["foo"])
1579\end{verbatim}
1580Note that the name of the distribution is specified independently with
1581the \option{name} option, and there's no rule that says it has to be the
1582same as the name of the sole module in the distribution (although that's
1583probably a good convention to follow). However, the distribution name
1584is used to generate filenames, so you should stick to letters, digits,
1585underscores, and hyphens.
1586
1587Since \option{py\_modules} is a list, you can of course specify multiple
1588modules, eg. if you're distributing modules \module{foo} and
1589\module{bar}, your setup might look like this:
1590\begin{verbatim}
1591<root>/
1592 setup.py
1593 foo.py
1594 bar.py
1595\end{verbatim}
1596and the setup script might be
1597\begin{verbatim}
1598from distutils.core import setup
1599setup(name = "foobar", version = "1.0",
1600 py_modules = ["foo", "bar"])
1601\end{verbatim}
1602
1603You can put module source files into another directory, but if you have
1604enough modules to do that, it's probably easier to specify modules by
1605package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001606
1607
Greg Ward007c04a2002-05-10 14:45:59 +00001608\subsection{Pure Python distribution (by package)}
1609\label{pure-pkg}
1610
1611If you have more than a couple of modules to distribute, especially if
1612they are in multiple packages, it's probably easier to specify whole
1613packages rather than individual modules. This works even if your
1614modules are not in a package; you can just tell the Distutils to process
1615modules from the root package, and that works the same as any other
1616package (except that you don't have to have an \file{\_\_init\_\_.py}
1617file).
1618
1619The setup script from the last example could also be written as
1620\begin{verbatim}
1621from distutils.core import setup
1622setup(name = "foobar", version = "1.0",
1623 packages = [""])
1624\end{verbatim}
1625(The empty string stands for the root package.)
1626
1627If those two files are moved into a subdirectory, but remain in the root
1628package, e.g.:
1629\begin{verbatim}
1630<root>/
1631 setup.py
1632 src/ foo.py
1633 bar.py
1634\end{verbatim}
1635then you would still specify the root package, but you have to tell the
1636Distutils where source files in the root package live:
1637\begin{verbatim}
1638from distutils.core import setup
1639setup(name = "foobar", version = "1.0",
1640 package_dir = {"": "src"},
1641 packages = [""])
1642\end{verbatim}
1643
1644More typically, though, you will want to distribute multiple modules in
1645the same package (or in sub-packages). For example, if the \module{foo}
1646and \module{bar} modules belong in package \module{foobar}, one way to
1647layout your source tree is
1648\begin{verbatim}
1649<root>/
1650 setup.py
1651 foobar/
1652 __init__.py
1653 foo.py
1654 bar.py
1655\end{verbatim}
1656This is in fact the default layout expected by the Distutils, and the
1657one that requires the least work to describe in your setup script:
1658\begin{verbatim}
1659from distutils.core import setup
1660setup(name = "foobar", version = "1.0",
1661 packages = ["foobar"])
1662\end{verbatim}
1663
1664If you want to put modules in directories not named for their package,
1665then you need to use the \option{package\_dir} option again. For
1666example, if the \file{src} directory holds modules in the
1667\module{foobar} package:
1668\begin{verbatim}
1669<root>/
1670 setup.py
1671 src/
1672 __init__.py
1673 foo.py
1674 bar.py
1675\end{verbatim}
1676an appropriate setup script would be
1677\begin{verbatim}
1678from distutils.core import setup
1679setup(name = "foobar", version = "1.0",
1680 package_dir = {"foobar" : "src"},
1681 packages = ["foobar"])
1682\end{verbatim}
1683
1684Or, you might put modules from your main package right in the
1685distribution root:
1686\begin{verbatim}
1687<root>/
1688 setup.py
1689 __init__.py
1690 foo.py
1691 bar.py
1692\end{verbatim}
1693in which case your setup script would be
1694\begin{verbatim}
1695from distutils.core import setup
1696setup(name = "foobar", version = "1.0",
1697 package_dir = {"foobar" : ""},
1698 packages = ["foobar"])
1699\end{verbatim}
1700(The empty string also stands for the current directory.)
1701
1702If you have sub-packages, they must be explicitly listed in
1703\option{packages}, but any entries in \option{package\_dir}
1704automatically extend to sub-packages. (In other words, the Distutils
1705does \emph{not} scan your source tree, trying to figure out which
1706directories correspond to Python packages by looking for
1707\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1708sub-package:
1709\begin{verbatim}
1710<root>/
1711 setup.py
1712 foobar/
1713 __init__.py
1714 foo.py
1715 bar.py
1716 subfoo/
1717 __init__.py
1718 blah.py
1719\end{verbatim}
1720then the corresponding setup script would be
1721\begin{verbatim}
1722from distutils.core import setup
1723setup(name = "foobar", version = "1.0",
1724 packages = ["foobar", "foobar.subfoo"])
1725\end{verbatim}
1726(Again, the empty string in \option{package\_dir} stands for the current
1727directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001728
1729
Greg Ward007c04a2002-05-10 14:45:59 +00001730\subsection{Single extension module}
1731\label{single-ext}
1732
1733Extension modules are specified using the \option{ext\_modules} option.
1734\option{package\_dir} has no effect on where extension source files are
1735found; it only affects the source for pure Python modules. The simplest
1736case, a single extension module in a single C source file, is:
1737\begin{verbatim}
1738<root>/
1739 setup.py
1740 foo.c
1741\end{verbatim}
1742If the \module{foo} extension belongs in the root package, the setup
1743script for this could be
1744\begin{verbatim}
1745from distutils.core import setup
1746setup(name = "foobar", version = "1.0",
1747 ext_modules = [Extension("foo", ["foo.c"])])
1748\end{verbatim}
1749
1750If the extension actually belongs in a package, say \module{foopkg},
1751then
1752
1753With exactly the same source tree layout, this extension can be put in
1754the \module{foopkg} package simply by changing the name of the
1755extension:
1756\begin{verbatim}
1757from distutils.core import setup
1758setup(name = "foobar", version = "1.0",
1759 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1760\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001761
1762
Fred Drakea09262e2001-03-01 18:35:43 +00001763%\subsection{Multiple extension modules}
1764%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001765
1766
Fred Drakea09262e2001-03-01 18:35:43 +00001767%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001768
1769
Fred Drakea09262e2001-03-01 18:35:43 +00001770%\section{Extending the Distutils}
1771%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001772
1773
Fred Drakea09262e2001-03-01 18:35:43 +00001774%\subsection{Extending existing commands}
1775%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001776
1777
Fred Drakea09262e2001-03-01 18:35:43 +00001778%\subsection{Writing new commands}
1779%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001780
Fred Drakea09262e2001-03-01 18:35:43 +00001781%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001782
Greg Ward4a9e7222000-04-25 02:57:36 +00001783
1784
Greg Ward16aafcd2000-04-09 04:06:44 +00001785\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001786\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001787
1788
Fred Drakea09262e2001-03-01 18:35:43 +00001789%\subsection{Building modules: the \protect\command{build} command family}
1790%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001791
Fred Drakea09262e2001-03-01 18:35:43 +00001792%\subsubsection{\protect\command{build}}
1793%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001794
Fred Drakea09262e2001-03-01 18:35:43 +00001795%\subsubsection{\protect\command{build\_py}}
1796%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001797
Fred Drakea09262e2001-03-01 18:35:43 +00001798%\subsubsection{\protect\command{build\_ext}}
1799%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001800
Fred Drakea09262e2001-03-01 18:35:43 +00001801%\subsubsection{\protect\command{build\_clib}}
1802%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001803
1804
Greg Wardfacb8db2000-04-09 04:32:40 +00001805\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001806\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001807
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001808The install command ensures that the build commands have been run and then
1809runs the subcommands \command{install\_lib},
1810\command{install\_data} and
1811\command{install\_scripts}.
1812
Fred Drakea09262e2001-03-01 18:35:43 +00001813%\subsubsection{\protect\command{install\_lib}}
1814%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001815
1816\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001817\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001818This command installs all data files provided with the distribution.
1819
1820\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001821\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001822This command installs all (Python) scripts in the distribution.
1823
Greg Ward16aafcd2000-04-09 04:06:44 +00001824
Fred Drakea09262e2001-03-01 18:35:43 +00001825%\subsection{Cleaning up: the \protect\command{clean} command}
1826%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001827
1828
Fred Drakeeff9a872000-10-26 16:41:03 +00001829\subsection{Creating a source distribution: the
1830 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001831\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001832
1833
1834\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001835
Greg Ward16aafcd2000-04-09 04:06:44 +00001836The manifest template commands are:
1837\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001838 \lineii{include \var{pat1} \var{pat2} ... }
1839 {include all files matching any of the listed patterns}
1840 \lineii{exclude \var{pat1} \var{pat2} ... }
1841 {exclude all files matching any of the listed patterns}
1842 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1843 {include all files under \var{dir} matching any of the listed patterns}
1844 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1845 {exclude all files under \var{dir} matching any of the listed patterns}
1846 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001847 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001848 any of the listed patterns}
1849 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001850 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001851 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001852 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1853 \lineii{graft \var{dir}}{include all files under \var{dir}}
1854\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001855The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001856sequence of regular filename characters, \code{?} matches any single
1857regular filename character, and \code{[\var{range}]} matches any of the
1858characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001859\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001860platform-specific: on \UNIX{} it is anything except slash; on Windows
1861anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001862
Fred Drakeeff9a872000-10-26 16:41:03 +00001863\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001864
1865
Fred Drakea09262e2001-03-01 18:35:43 +00001866%\subsection{Creating a built distribution: the
1867% \protect\command{bdist} command family}
1868%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001869
1870
Fred Drakeab70b382001-08-02 15:13:15 +00001871%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001872
Fred Drakeab70b382001-08-02 15:13:15 +00001873%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001874
Fred Drakeab70b382001-08-02 15:13:15 +00001875%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001876
Fred Drakeab70b382001-08-02 15:13:15 +00001877%\subsubsection{\protect\command{bdist\_wininst}}
1878
1879
1880\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001881
1882
Greg Wardabc52162000-02-26 00:52:48 +00001883\end{document}