blob: 30637527aea64c0a40f9601c32e4d1dd2e082a10 [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
Fred Drake2884d6d2003-07-02 12:27:43 +0000193 the Python implementation: C/\Cpp{} 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 Drake2884d6d2003-07-02 12:27:43 +0000198 currently, the Distutils only handles C/\Cpp{} 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
Fred Drake2884d6d2003-07-02 12:27:43 +0000474resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000475
476\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000477 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000478 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
Fred Drake2884d6d2003-07-02 12:27:43 +0000483(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000484(\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(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000558 define_macros=[('NDEBUG', '1'),
559 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000560 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
Thomas Heller675580f2003-06-30 19:33:29 +0000789\subsection{Debugging the setup script}
790\label{meta-data}
791
792Sometimes things go wrong, and the setup script doesn't do what the
793developer wants.
794
795Distutils catches any exceptions when running the setup script, and
796print a simple error message before the script is terminated. The
797motivation for this behaviour is to not confuse administrators who
798don't know much about Python and are trying to install a package. If
799they get a big long traceback from deep inside the guts of Distutils,
800they may think the package or the Python installation is broken
801because they don't read all the way down to the bottom and see that
802it's a permission problem.
803
804On the other hand, this doesn't help the developer to find the cause
805of the failure. For this purpose, the DISTUTILS_DEBUG environment
806variable can be set to anything except an empty string, and distutils
807will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000808full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000809
Greg Ward16aafcd2000-04-09 04:06:44 +0000810\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000811\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000812
Greg Ward16aafcd2000-04-09 04:06:44 +0000813Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000814distribution \emph{a priori}: you may need to get some information from
815the user, or from the user's system, in order to proceed. As long as
816that information is fairly simple---a list of directories to search for
817C header files or libraries, for example---then providing a
818configuration file, \file{setup.cfg}, for users to edit is a cheap and
819easy way to solicit it. Configuration files also let you provide
820default values for any command option, which the installer can then
821override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000822
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000823% (If you have more advanced needs, such as determining which extensions
824% to build based on what capabilities are present on the target system,
825% then you need the Distutils ``auto-configuration'' facility. This
826% started to appear in Distutils 0.9 but, as of this writing, isn't mature
827% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000828
Greg Ward47f99a62000-09-04 20:07:15 +0000829The setup configuration file is a useful middle-ground between the setup
830script---which, ideally, would be opaque to installers\footnote{This
831 ideal probably won't be achieved until auto-configuration is fully
832 supported by the Distutils.}---and the command-line to the setup
833script, which is outside of your control and entirely up to the
834installer. In fact, \file{setup.cfg} (and any other Distutils
835configuration files present on the target system) are processed after
836the contents of the setup script, but before the command-line. This has
837several useful consequences:
838\begin{itemize}
839\item installers can override some of what you put in \file{setup.py} by
840 editing \file{setup.cfg}
841\item you can provide non-standard defaults for options that are not
842 easily set in \file{setup.py}
843\item installers can override anything in \file{setup.cfg} using the
844 command-line options to \file{setup.py}
845\end{itemize}
846
847The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000848
Greg Ward47f99a62000-09-04 20:07:15 +0000849\begin{verbatim}
850[command]
851option=value
852...
853\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000854
Greg Ward47f99a62000-09-04 20:07:15 +0000855where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000856\command{build\_py}, \command{install}), and \var{option} is one of
857the options that command supports. Any number of options can be
858supplied for each command, and any number of command sections can be
859included in the file. Blank lines are ignored, as are comments, which
860run from a \character{\#} character until the end of the line. Long
861option values can be split across multiple lines simply by indenting
862the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000863
864You can find out the list of options supported by a particular command
865with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000866
Greg Ward47f99a62000-09-04 20:07:15 +0000867\begin{verbatim}
868> python setup.py --help build_ext
869[...]
870Options for 'build_ext' command:
871 --build-lib (-b) directory for compiled extension modules
872 --build-temp (-t) directory for temporary files (build by-products)
873 --inplace (-i) ignore build-lib and put compiled extensions into the
874 source directory alongside your pure Python modules
875 --include-dirs (-I) list of directories to search for header files
876 --define (-D) C preprocessor macros to define
877 --undef (-U) C preprocessor macros to undefine
878[...]
879\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000880
Greg Ward47f99a62000-09-04 20:07:15 +0000881Note that an option spelled \longprogramopt{foo-bar} on the command-line
882is spelled \option{foo\_bar} in configuration files.
883
884For example, say you want your extensions to be built
885``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000886want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000887in the same source directory as your pure Python modules
888\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
889\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000890
Greg Ward47f99a62000-09-04 20:07:15 +0000891\begin{verbatim}
892python setup.py build_ext --inplace
893\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000894
Greg Ward47f99a62000-09-04 20:07:15 +0000895But this requires that you always specify the \command{build\_ext}
896command explicitly, and remember to provide \longprogramopt{inplace}.
897An easier way is to ``set and forget'' this option, by encoding it in
898\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000899
Greg Ward47f99a62000-09-04 20:07:15 +0000900\begin{verbatim}
901[build_ext]
902inplace=1
903\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000904
Greg Ward47f99a62000-09-04 20:07:15 +0000905This will affect all builds of this module distribution, whether or not
906you explcitly specify \command{build\_ext}. If you include
907\file{setup.cfg} in your source distribution, it will also affect
908end-user builds---which is probably a bad idea for this option, since
909always building extensions in-place would break installation of the
910module distribution. In certain peculiar cases, though, modules are
911built right in their installation directory, so this is conceivably a
912useful ability. (Distributing extensions that expect to be built in
913their installation directory is almost always a bad idea, though.)
914
915Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000916change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000917everything required to generate a ``spec'' file for creating an RPM
918distribution. Some of this information comes from the setup script, and
919some is automatically generated by the Distutils (such as the list of
920files installed). But some of it has to be supplied as options to
921\command{bdist\_rpm}, which would be very tedious to do on the
922command-line for every run. Hence, here is a snippet from the
923Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000924
Greg Ward47f99a62000-09-04 20:07:15 +0000925\begin{verbatim}
926[bdist_rpm]
927release = 1
928packager = Greg Ward <gward@python.net>
929doc_files = CHANGES.txt
930 README.txt
931 USAGE.txt
932 doc/
933 examples/
934\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000935
Greg Ward47f99a62000-09-04 20:07:15 +0000936Note that the \option{doc\_files} option is simply a
937whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000938
939
Fred Drakea09262e2001-03-01 18:35:43 +0000940\begin{seealso}
941 \seetitle[../inst/config-syntax.html]{Installing Python
942 Modules}{More information on the configuration files is
943 available in the manual for system administrators.}
944\end{seealso}
945
946
Greg Ward16aafcd2000-04-09 04:06:44 +0000947\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000948\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000949
Greg Warde78298a2000-04-28 17:12:24 +0000950As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000951\command{sdist} command to create a source distribution. In the
952simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000953
Greg Ward16aafcd2000-04-09 04:06:44 +0000954\begin{verbatim}
955python setup.py sdist
956\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000957
Greg Ward19c67f82000-06-24 01:33:16 +0000958(assuming you haven't specified any \command{sdist} options in the setup
959script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000960default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000961tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
962\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000963
Greg Wardd5767a52000-04-19 22:48:09 +0000964You can specify as many formats as you like using the
965\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000966
Greg Ward16aafcd2000-04-09 04:06:44 +0000967\begin{verbatim}
968python setup.py sdist --formats=gztar,zip
969\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000970
Greg Ward16aafcd2000-04-09 04:06:44 +0000971to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000972\begin{tableiii}{l|l|c}{code}%
973 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000974 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
975 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000976 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000977 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000978 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000979\end{tableiii}
980
981\noindent Notes:
982\begin{description}
983\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000984\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000985\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000986 \module{zipfile} module (part of the standard Python library since
987 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000988\item[(4)] requires external utilities: \program{tar} and possibly one
989 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000990\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000991
992
Greg Ward54589d42000-09-06 01:37:35 +0000993
994\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000995\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000996
Greg Ward54589d42000-09-06 01:37:35 +0000997If you don't supply an explicit list of files (or instructions on how to
998generate one), the \command{sdist} command puts a minimal default set
999into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001000\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001001\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001002 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001003\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001004 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +00001005 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +00001006\item anything that looks like a test script: \file{test/test*.py}
1007 (currently, the Distutils don't do anything with test scripts except
1008 include them in source distributions, but in the future there will be
1009 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001010\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1011 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001012\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001013
Greg Ward16aafcd2000-04-09 04:06:44 +00001014Sometimes this is enough, but usually you will want to specify
1015additional files to distribute. The typical way to do this is to write
1016a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001017manifest template is just a list of instructions for how to generate
1018your manifest file, \file{MANIFEST}, which is the exact list of files to
1019include in your source distribution. The \command{sdist} command
1020processes this template and generates a manifest based on its
1021instructions and what it finds in the filesystem.
1022
1023If you prefer to roll your own manifest file, the format is simple: one
1024filename per line, regular files (or symlinks to them) only. If you do
1025supply your own \file{MANIFEST}, you must specify everything: the
1026default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001027
1028The manifest template has one command per line, where each command
1029specifies a set of files to include or exclude from the source
1030distribution. For an example, again we turn to the Distutils' own
1031manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001032
Greg Ward16aafcd2000-04-09 04:06:44 +00001033\begin{verbatim}
1034include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001035recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001036prune examples/sample?/build
1037\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001038
Greg Ward16aafcd2000-04-09 04:06:44 +00001039The meanings should be fairly clear: include all files in the
1040distribution root matching \code{*.txt}, all files anywhere under the
1041\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001042exclude all directories matching \code{examples/sample?/build}. All of
1043this is done \emph{after} the standard include set, so you can exclude
1044files from the standard set with explicit instructions in the manifest
1045template. (Or, you can use the \longprogramopt{no-defaults} option to
1046disable the standard set entirely.) There are several other commands
1047available in the manifest template mini-language; see
1048section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001049
Greg Ward54589d42000-09-06 01:37:35 +00001050The order of commands in the manifest template matters: initially, we
1051have the list of default files as described above, and each command in
1052the template adds to or removes from that list of files. Once we have
1053fully processed the manifest template, we remove files that should not
1054be included in the source distribution:
1055\begin{itemize}
1056\item all files in the Distutils ``build'' tree (default \file{build/})
1057\item all files in directories named \file{RCS} or \file{CVS}
1058\end{itemize}
1059Now we have our complete list of files, which is written to the manifest
1060for future reference, and then used to build the source distribution
1061archive(s).
1062
1063You can disable the default set of included files with the
1064\longprogramopt{no-defaults} option, and you can disable the standard
1065exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001066
Greg Ward46b98e32000-04-14 01:53:36 +00001067Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001068\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001069Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001070\begin{enumerate}
1071\item include all Python source files in the \file{distutils} and
1072 \file{distutils/command} subdirectories (because packages
1073 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001074 \option{packages} option in the setup script---see
1075 section~\ref{setup-script})
1076\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1077 (standard files)
1078\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001079\item include \file{*.txt} in the distribution root (this will find
1080 \file{README.txt} a second time, but such redundancies are weeded out
1081 later)
Greg Ward54589d42000-09-06 01:37:35 +00001082\item include anything matching \file{*.txt} or \file{*.py} in the
1083 sub-tree under \file{examples},
1084\item exclude all files in the sub-trees starting at directories
1085 matching \file{examples/sample?/build}---this may exclude files
1086 included by the previous two steps, so it's important that the
1087 \code{prune} command in the manifest template comes after the
1088 \code{recursive-include} command
1089\item exclude the entire \file{build} tree, and any \file{RCS} or
1090 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001091\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001092Just like in the setup script, file and directory names in the manifest
1093template should always be slash-separated; the Distutils will take care
1094of converting them to the standard representation on your platform.
1095That way, the manifest template is portable across operating systems.
1096
Greg Ward16aafcd2000-04-09 04:06:44 +00001097
1098\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001099\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001100
1101The normal course of operations for the \command{sdist} command is as
1102follows:
1103\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001104\item if the manifest file, \file{MANIFEST} doesn't exist, read
1105 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001106\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001107 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001108\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1109 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1110 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001111\item use the list of files now in \file{MANIFEST} (either just
1112 generated or read in) to create the source distribution archive(s)
1113\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001114There are a couple of options that modify this behaviour. First, use
1115the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001116disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001117
Greg Ward54589d42000-09-06 01:37:35 +00001118Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001119example, if you have added or removed files or directories that match an
1120existing pattern in the manifest template, you should regenerate the
1121manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001122
Greg Ward16aafcd2000-04-09 04:06:44 +00001123\begin{verbatim}
1124python setup.py sdist --force-manifest
1125\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001126
1127Or, you might just want to (re)generate the manifest, but not create a
1128source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001129
Greg Ward16aafcd2000-04-09 04:06:44 +00001130\begin{verbatim}
1131python setup.py sdist --manifest-only
1132\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001133
Greg Ward54589d42000-09-06 01:37:35 +00001134\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1135\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1136\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001137
1138
1139\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001140\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001141
Greg Ward46b98e32000-04-14 01:53:36 +00001142A ``built distribution'' is what you're probably used to thinking of
1143either as a ``binary package'' or an ``installer'' (depending on your
1144background). It's not necessarily binary, though, because it might
1145contain only Python source code and/or byte-code; and we don't call it a
1146package, because that word is already spoken for in Python. (And
1147``installer'' is a term specific to the Windows world. \XXX{do Mac
1148 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001149
Greg Ward46b98e32000-04-14 01:53:36 +00001150A built distribution is how you make life as easy as possible for
1151installers of your module distribution: for users of RPM-based Linux
1152systems, it's a binary RPM; for Windows users, it's an executable
1153installer; for Debian-based Linux users, it's a Debian package; and so
1154forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001155distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001156designed to enable module developers to concentrate on their
1157specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001158intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001159distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001160are packagers.
1161
1162Of course, the module developer could be his own packager; or the
1163packager could be a volunteer ``out there'' somewhere who has access to
1164a platform which the original developer does not; or it could be
1165software periodically grabbing new source distributions and turning them
1166into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001167access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001168setup script and the \command{bdist} command family to generate built
1169distributions.
1170
1171As a simple example, if I run the following command in the Distutils
1172source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001173
Greg Ward46b98e32000-04-14 01:53:36 +00001174\begin{verbatim}
1175python setup.py bdist
1176\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001177
Greg Ward46b98e32000-04-14 01:53:36 +00001178then the Distutils builds my module distribution (the Distutils itself
1179in this case), does a ``fake'' installation (also in the \file{build}
1180directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001181platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001182file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001183file is considered ``dumb'' because it has to be unpacked in a specific
1184location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001185
Fred Drakeeff9a872000-10-26 16:41:03 +00001186Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001187\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001188from the right place installs the Distutils just as though you had
1189downloaded the source distribution and run \code{python setup.py
1190 install}. (The ``right place'' is either the root of the filesystem or
1191Python's \filevar{prefix} directory, depending on the options given to
1192the \command{bdist\_dumb} command; the default is to make dumb
1193distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001194
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001195Obviously, for pure Python distributions, this isn't any simpler than
1196just running \code{python setup.py install}---but for non-pure
1197distributions, which include extensions that would need to be
1198compiled, it can mean the difference between someone being able to use
1199your extensions or not. And creating ``smart'' built distributions,
1200such as an RPM package or an executable installer for Windows, is far
1201more convenient for users even if your distribution doesn't include
1202any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001203
Greg Wardb6528972000-09-07 02:40:37 +00001204The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001205similar to the \command{sdist} command, which you can use to select the
1206types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001207
Greg Ward46b98e32000-04-14 01:53:36 +00001208\begin{verbatim}
1209python setup.py bdist --format=zip
1210\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001211
Fred Drakeeff9a872000-10-26 16:41:03 +00001212would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001213\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001214unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001215
1216The available formats for built distributions are:
1217\begin{tableiii}{l|l|c}{code}%
1218 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001219 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1220 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1221 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1222 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1223 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001224 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1225 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1226 \lineiii{rpm}{RPM}{(5)}
1227% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001228 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001229\end{tableiii}
1230
1231\noindent Notes:
1232\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001233\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001234\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001235\item[(3)] requires external utilities: \program{tar} and possibly one
1236 of \program{gzip}, \program{bzip2}, or \program{compress}
1237\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001238 \module{zipfile} module (part of the standard Python library since
1239 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001240\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1241 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001242\end{description}
1243
1244You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001245\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001246directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001247\command{bdist} ``sub-commands'' actually generate several similar
1248formats; for instance, the \command{bdist\_dumb} command generates all
1249the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1250\code{zip}), and \command{bdist\_rpm} generates both binary and source
1251RPMs. The \command{bdist} sub-commands, and the formats generated by
1252each, are:
1253\begin{tableii}{l|l}{command}%
1254 {Command}{Formats}
1255 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1256 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001257 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001258\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001259
Greg Wardb6528972000-09-07 02:40:37 +00001260The following sections give details on the individual \command{bdist\_*}
1261commands.
1262
1263
1264\subsection{Creating dumb built distributions}
1265\label{creating-dumb}
1266
1267\XXX{Need to document absolute vs. prefix-relative packages here, but
1268 first I have to implement it!}
1269
1270
1271\subsection{Creating RPM packages}
1272\label{creating-rpms}
1273
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001274The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001275Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1276RPM-based Linux distributions) is your usual environment, creating RPM
1277packages for other users of that same distribution is trivial.
1278Depending on the complexity of your module distribution and differences
1279between Linux distributions, you may also be able to create RPMs that
1280work on different RPM-based distributions.
1281
1282The usual way to create an RPM of your module distribution is to run the
1283\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001284
Greg Wardb6528972000-09-07 02:40:37 +00001285\begin{verbatim}
1286python setup.py bdist_rpm
1287\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001288
Greg Wardb6528972000-09-07 02:40:37 +00001289or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001290
Greg Wardb6528972000-09-07 02:40:37 +00001291\begin{verbatim}
1292python setup.py bdist --formats=rpm
1293\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001294
Greg Wardb6528972000-09-07 02:40:37 +00001295The former allows you to specify RPM-specific options; the latter allows
1296you to easily specify multiple formats in one run. If you need to do
1297both, you can explicitly specify multiple \command{bdist\_*} commands
1298and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001299
Greg Wardb6528972000-09-07 02:40:37 +00001300\begin{verbatim}
1301python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1302 bdist_wininst --target_version="2.0"
1303\end{verbatim}
1304
1305Creating RPM packages is driven by a \file{.spec} file, much as using
1306the Distutils is driven by the setup script. To make your life easier,
1307the \command{bdist\_rpm} command normally creates a \file{.spec} file
1308based on the information you supply in the setup script, on the command
1309line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001310sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001311script as follows:
1312\begin{tableii}{l|l}{textrm}%
1313 {RPM \file{.spec} file option or section}{Distutils setup script option}
1314 \lineii{Name}{\option{name}}
1315 \lineii{Summary (in preamble)}{\option{description}}
1316 \lineii{Version}{\option{version}}
1317 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1318 \option{maintainer} and \option{maintainer\_email}}
1319 \lineii{Copyright}{\option{licence}}
1320 \lineii{Url}{\option{url}}
1321 \lineii{\%description (section)}{\option{long\_description}}
1322\end{tableii}
1323
1324Additionally, there many options in \file{.spec} files that don't have
1325corresponding options in the setup script. Most of these are handled
1326through options to the \command{bdist\_rpm} command as follows:
1327\begin{tableiii}{l|l|l}{textrm}%
1328 {RPM \file{.spec} file option or section}%
1329 {\command{bdist\_rpm} option}%
1330 {default value}
1331 \lineiii{Release}{\option{release}}{``1''}
1332 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1333 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001334 \lineiii{Packager}{\option{packager}}{(none)}
1335 \lineiii{Provides}{\option{provides}}{(none)}
1336 \lineiii{Requires}{\option{requires}}{(none)}
1337 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1338 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001339 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1340 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1341 \lineiii{Icon}{\option{icon}}{(none)}
1342\end{tableiii}
1343Obviously, supplying even a few of these options on the command-line
1344would be tedious and error-prone, so it's usually best to put them in
1345the setup configuration file, \file{setup.cfg}---see
1346section~\ref{setup-config}. If you distribute or package many Python
1347module distributions, you might want to put options that apply to all of
1348them in your personal Distutils configuration file
1349(\file{\textasciitilde/.pydistutils.cfg}).
1350
1351There are three steps to building a binary RPM package, all of which are
1352handled automatically by the Distutils:
1353\begin{enumerate}
1354\item create a \file{.spec} file, which describes the package (analogous
1355 to the Distutils setup script; in fact, much of the information in the
1356 setup script winds up in the \file{.spec} file)
1357\item create the source RPM
1358\item create the ``binary'' RPM (which may or may not contain binary
1359 code, depending on whether your module distribution contains Python
1360 extensions)
1361\end{enumerate}
1362Normally, RPM bundles the last two steps together; when you use the
1363Distutils, all three steps are typically bundled together.
1364
1365If you wish, you can separate these three steps. You can use the
1366\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1367create the \file{.spec} file and exit; in this case, the \file{.spec}
1368file will be written to the ``distribution directory''---normally
1369\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1370option. (Normally, the \file{.spec} file winds up deep in the ``build
1371tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1372
1373\XXX{this isn't implemented yet---is it needed?!}
1374You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001375\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001376\longprogramopt{spec-only}, this gives you an opportunity to customize
1377the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001378
Greg Wardb6528972000-09-07 02:40:37 +00001379\begin{verbatim}
1380> python setup.py bdist_rpm --spec-only
1381# ...edit dist/FooBar-1.0.spec
1382> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1383\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001384
Greg Wardb6528972000-09-07 02:40:37 +00001385(Although a better way to do this is probably to override the standard
1386\command{bdist\_rpm} command with one that writes whatever else you want
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +00001387to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001388
1389
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001390\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001391\label{creating-wininst}
1392
Thomas Hellere61f3652002-11-15 20:13:26 +00001393Executable installers are the natural format for binary distributions
1394on Windows. They display a nice graphical user interface, display
1395some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001396from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001397options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001398
Thomas Hellere61f3652002-11-15 20:13:26 +00001399Since the metadata is taken from the setup script, creating Windows
1400installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001401
Thomas Heller5f52f722001-02-19 17:48:03 +00001402\begin{verbatim}
1403python setup.py bdist_wininst
1404\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001405
Thomas Heller36343f62002-11-15 19:20:56 +00001406or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001407
Thomas Heller5f52f722001-02-19 17:48:03 +00001408\begin{verbatim}
1409python setup.py bdist --formats=wininst
1410\end{verbatim}
1411
Thomas Hellere61f3652002-11-15 20:13:26 +00001412If you have a pure module distribution (only containing pure Python
1413modules and packages), the resulting installer will be version
1414independent and have a name like \file{foo-1.0.win32.exe}. These
1415installers can even be created on \UNIX{} or MacOS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001416
1417If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001418created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001419The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001420\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001421for every Python version you want to support.
1422
1423The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001424installation on the target system in normal and optimizing mode. If
1425you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001426\command{bdist_wininst} command with the
1427\longprogramopt{no-target-compile} and/or the
1428\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001429
Fred Drake0e9bfa32002-11-15 20:34:52 +00001430By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001431when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001432a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001433
1434The installer will also display a large title on the desktop
1435background window when it is run, which is constructed from the name
1436of your distribution and the version number. This can be changed to
1437another text by using the \longprogramopt{title} option.
1438
1439The installer file will be written to the ``distribution directory''
1440--- normally \file{dist/}, but customizable with the
1441\longprogramopt{dist-dir} option.
1442
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001443\subsubsection{The Postinstallation script}
1444\label{postinstallation-script}
1445
1446Starting with Python 2.3, a postinstallation script can be specified
1447which the \longprogramopt{install-script} option. The basename of the
1448script must be specified, and the script filename must also be listed
1449in the scripts argument to the setup function.
1450
1451This script will be run at installation time on the target system
1452after all the files have been copied, with argv[1] set to '-install',
1453and again at uninstallation time before the files are removed with argv[1]
1454set to '-remove'.
1455
1456The installation script runs embedded in the windows installer, every
1457output (sys.stdout, sys.stderr) is redirected into a buffer and will
1458be displayed in the GUI after the script has finished.
1459
1460Some functions especially useful in this context are available in the
1461installation script.
1462
1463\begin{verbatim}
Thomas Heller41e28092003-10-16 19:40:48 +00001464directory_created(pathname)
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001465file_created(pathname)
1466\end{verbatim}
1467
1468These functions should be called when a directory or file is created
1469by the postinstall script at installation time. It will register the
1470pathname with the uninstaller, so that it will be removed when the
1471distribution is uninstalled. To be safe, directories are only removed
1472if they are empty.
1473
1474\begin{verbatim}
1475get_special_folder_path(csidl_string)
1476\end{verbatim}
1477
1478This function can be used to retrieve special folder locations on
1479Windows like the Start Menu or the Desktop. It returns the full path
Thomas Hellera425dbc2003-09-17 17:11:01 +00001480to the folder. 'csidl_string' must be one of the following strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001481
1482\begin{verbatim}
1483"CSIDL_APPDATA"
1484
1485"CSIDL_COMMON_STARTMENU"
1486"CSIDL_STARTMENU"
1487
1488"CSIDL_COMMON_DESKTOPDIRECTORY"
1489"CSIDL_DESKTOPDIRECTORY"
1490
1491"CSIDL_COMMON_STARTUP"
1492"CSIDL_STARTUP"
1493
1494"CSIDL_COMMON_PROGRAMS"
1495"CSIDL_PROGRAMS"
1496
1497"CSIDL_FONTS"
1498\end{verbatim}
1499
1500If the folder cannot be retrieved, OSError is raised.
1501
1502Which folders are available depends on the exact Windows version, and probably
1503also the configuration. For details refer to Microsoft's documentation of the
Thomas Heller63b4dd32002-12-12 19:35:00 +00001504\code{SHGetSpecialFolderPath} function.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001505
1506\begin{verbatim}
1507create_shortcut(target, description, filename[, arguments[,
1508 workdir[, iconpath[, iconindex]]]])
1509\end{verbatim}
1510
1511This function creates a shortcut.
Thomas Heller63b4dd32002-12-12 19:35:00 +00001512\var{target} is the path to the program to be started by the shortcut.
1513\var{description} is the description of the sortcut.
1514\var{filename} is the title of the shortcut that the user will see.
1515\var{arguments} specifies the command line arguments, if any.
1516\var{workdir} is the working directory for the program.
1517\var{iconpath} is the file containing the icon for the shortcut,
1518and \var{iconindex} is the index of the icon in the file
1519\var{iconpath}. Again, for details consult the Microsoft
1520documentation for the \code{IShellLink} interface.
Greg Wardb6528972000-09-07 02:40:37 +00001521
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001522\section{Registering with the Package Index}
1523\label{package-index}
1524
1525The Python Package Index (PyPI) holds meta-data describing distributions
1526packaged with distutils. The distutils command \command{register} is
1527used to submit your distribution's meta-data to the index. It is invoked
1528as follows:
1529
1530\begin{verbatim}
1531python setup.py register
1532\end{verbatim}
1533
1534Distutils will respond with the following prompt:
1535
1536\begin{verbatim}
1537running register
1538We need to know who you are, so please choose either:
1539 1. use your existing login,
1540 2. register as a new user,
1541 3. have the server generate a new password for you (and email it to you), or
1542 4. quit
1543Your selection [default 1]:
1544\end{verbatim}
1545
1546\noindent Note: if your username and password are saved locally, you will
1547not see this menu.
1548
1549If you have not registered with PyPI, then you will need to do so now. You
1550should choose option 2, and enter your details as required. Soon after
1551submitting your details, you will receive an email which will be used to
1552confirm your registration.
1553
1554Once you are registered, you may choose option 1 from the menu. You will
1555be prompted for your PyPI username and password, and \command{register}
1556will then submit your meta-data to the index.
1557
1558You may submit any number of versions of your distribution to the index. If
1559you alter the meta-data for a particular version, you may submit it again
1560and the index will be updated.
1561
1562PyPI holds a record for each (name, version) combination submitted. The
1563first user to submit information for a given name is designated the Owner
1564of that name. They may submit changes through the \command{register}
1565command or through the web interface. They may also designate other users
1566as Owners or Maintainers. Maintainers may edit the package information, but
1567not designate other Owners or Maintainers.
1568
1569By default PyPI will list all versions of a given package. To hide certain
1570versions, the Hidden property should be set to yes. This must be edited
1571through the web interface.
1572
1573
1574
Greg Ward007c04a2002-05-10 14:45:59 +00001575\section{Examples}
1576\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001577
Greg Ward007c04a2002-05-10 14:45:59 +00001578\subsection{Pure Python distribution (by module)}
1579\label{pure-mod}
1580
1581If you're just distributing a couple of modules, especially if they
1582don't live in a particular package, you can specify them individually
1583using the \option{py\_modules} option in the setup script.
1584
1585In the simplest case, you'll have two files to worry about: a setup
1586script and the single module you're distributing, \file{foo.py} in this
1587example:
1588\begin{verbatim}
1589<root>/
1590 setup.py
1591 foo.py
1592\end{verbatim}
1593(In all diagrams in this section, \verb|<root>| will refer to the
1594distribution root directory.) A minimal setup script to describe this
1595situation would be:
1596\begin{verbatim}
1597from distutils.core import setup
1598setup(name = "foo", version = "1.0",
1599 py_modules = ["foo"])
1600\end{verbatim}
1601Note that the name of the distribution is specified independently with
1602the \option{name} option, and there's no rule that says it has to be the
1603same as the name of the sole module in the distribution (although that's
1604probably a good convention to follow). However, the distribution name
1605is used to generate filenames, so you should stick to letters, digits,
1606underscores, and hyphens.
1607
1608Since \option{py\_modules} is a list, you can of course specify multiple
1609modules, eg. if you're distributing modules \module{foo} and
1610\module{bar}, your setup might look like this:
1611\begin{verbatim}
1612<root>/
1613 setup.py
1614 foo.py
1615 bar.py
1616\end{verbatim}
1617and the setup script might be
1618\begin{verbatim}
1619from distutils.core import setup
1620setup(name = "foobar", version = "1.0",
1621 py_modules = ["foo", "bar"])
1622\end{verbatim}
1623
1624You can put module source files into another directory, but if you have
1625enough modules to do that, it's probably easier to specify modules by
1626package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001627
1628
Greg Ward007c04a2002-05-10 14:45:59 +00001629\subsection{Pure Python distribution (by package)}
1630\label{pure-pkg}
1631
1632If you have more than a couple of modules to distribute, especially if
1633they are in multiple packages, it's probably easier to specify whole
1634packages rather than individual modules. This works even if your
1635modules are not in a package; you can just tell the Distutils to process
1636modules from the root package, and that works the same as any other
1637package (except that you don't have to have an \file{\_\_init\_\_.py}
1638file).
1639
1640The setup script from the last example could also be written as
1641\begin{verbatim}
1642from distutils.core import setup
1643setup(name = "foobar", version = "1.0",
1644 packages = [""])
1645\end{verbatim}
1646(The empty string stands for the root package.)
1647
1648If those two files are moved into a subdirectory, but remain in the root
1649package, e.g.:
1650\begin{verbatim}
1651<root>/
1652 setup.py
1653 src/ foo.py
1654 bar.py
1655\end{verbatim}
1656then you would still specify the root package, but you have to tell the
1657Distutils where source files in the root package live:
1658\begin{verbatim}
1659from distutils.core import setup
1660setup(name = "foobar", version = "1.0",
1661 package_dir = {"": "src"},
1662 packages = [""])
1663\end{verbatim}
1664
1665More typically, though, you will want to distribute multiple modules in
1666the same package (or in sub-packages). For example, if the \module{foo}
1667and \module{bar} modules belong in package \module{foobar}, one way to
1668layout your source tree is
1669\begin{verbatim}
1670<root>/
1671 setup.py
1672 foobar/
1673 __init__.py
1674 foo.py
1675 bar.py
1676\end{verbatim}
1677This is in fact the default layout expected by the Distutils, and the
1678one that requires the least work to describe in your setup script:
1679\begin{verbatim}
1680from distutils.core import setup
1681setup(name = "foobar", version = "1.0",
1682 packages = ["foobar"])
1683\end{verbatim}
1684
1685If you want to put modules in directories not named for their package,
1686then you need to use the \option{package\_dir} option again. For
1687example, if the \file{src} directory holds modules in the
1688\module{foobar} package:
1689\begin{verbatim}
1690<root>/
1691 setup.py
1692 src/
1693 __init__.py
1694 foo.py
1695 bar.py
1696\end{verbatim}
1697an appropriate setup script would be
1698\begin{verbatim}
1699from distutils.core import setup
1700setup(name = "foobar", version = "1.0",
1701 package_dir = {"foobar" : "src"},
1702 packages = ["foobar"])
1703\end{verbatim}
1704
1705Or, you might put modules from your main package right in the
1706distribution root:
1707\begin{verbatim}
1708<root>/
1709 setup.py
1710 __init__.py
1711 foo.py
1712 bar.py
1713\end{verbatim}
1714in which case your setup script would be
1715\begin{verbatim}
1716from distutils.core import setup
1717setup(name = "foobar", version = "1.0",
1718 package_dir = {"foobar" : ""},
1719 packages = ["foobar"])
1720\end{verbatim}
1721(The empty string also stands for the current directory.)
1722
1723If you have sub-packages, they must be explicitly listed in
1724\option{packages}, but any entries in \option{package\_dir}
1725automatically extend to sub-packages. (In other words, the Distutils
1726does \emph{not} scan your source tree, trying to figure out which
1727directories correspond to Python packages by looking for
1728\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1729sub-package:
1730\begin{verbatim}
1731<root>/
1732 setup.py
1733 foobar/
1734 __init__.py
1735 foo.py
1736 bar.py
1737 subfoo/
1738 __init__.py
1739 blah.py
1740\end{verbatim}
1741then the corresponding setup script would be
1742\begin{verbatim}
1743from distutils.core import setup
1744setup(name = "foobar", version = "1.0",
1745 packages = ["foobar", "foobar.subfoo"])
1746\end{verbatim}
1747(Again, the empty string in \option{package\_dir} stands for the current
1748directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001749
1750
Greg Ward007c04a2002-05-10 14:45:59 +00001751\subsection{Single extension module}
1752\label{single-ext}
1753
1754Extension modules are specified using the \option{ext\_modules} option.
1755\option{package\_dir} has no effect on where extension source files are
1756found; it only affects the source for pure Python modules. The simplest
1757case, a single extension module in a single C source file, is:
1758\begin{verbatim}
1759<root>/
1760 setup.py
1761 foo.c
1762\end{verbatim}
1763If the \module{foo} extension belongs in the root package, the setup
1764script for this could be
1765\begin{verbatim}
1766from distutils.core import setup
1767setup(name = "foobar", version = "1.0",
1768 ext_modules = [Extension("foo", ["foo.c"])])
1769\end{verbatim}
1770
1771If the extension actually belongs in a package, say \module{foopkg},
1772then
1773
1774With exactly the same source tree layout, this extension can be put in
1775the \module{foopkg} package simply by changing the name of the
1776extension:
1777\begin{verbatim}
1778from distutils.core import setup
1779setup(name = "foobar", version = "1.0",
1780 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1781\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001782
1783
Fred Drakea09262e2001-03-01 18:35:43 +00001784%\subsection{Multiple extension modules}
1785%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001786
1787
Fred Drakea09262e2001-03-01 18:35:43 +00001788%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001789
1790
Fred Drakea09262e2001-03-01 18:35:43 +00001791%\section{Extending the Distutils}
1792%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001793
1794
Fred Drakea09262e2001-03-01 18:35:43 +00001795%\subsection{Extending existing commands}
1796%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001797
1798
Fred Drakea09262e2001-03-01 18:35:43 +00001799%\subsection{Writing new commands}
1800%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001801
Fred Drakea09262e2001-03-01 18:35:43 +00001802%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001803
Greg Ward4a9e7222000-04-25 02:57:36 +00001804
1805
Greg Ward16aafcd2000-04-09 04:06:44 +00001806\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001807\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001808
1809
Fred Drakea09262e2001-03-01 18:35:43 +00001810%\subsection{Building modules: the \protect\command{build} command family}
1811%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001812
Fred Drakea09262e2001-03-01 18:35:43 +00001813%\subsubsection{\protect\command{build}}
1814%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001815
Fred Drakea09262e2001-03-01 18:35:43 +00001816%\subsubsection{\protect\command{build\_py}}
1817%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001818
Fred Drakea09262e2001-03-01 18:35:43 +00001819%\subsubsection{\protect\command{build\_ext}}
1820%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001821
Fred Drakea09262e2001-03-01 18:35:43 +00001822%\subsubsection{\protect\command{build\_clib}}
1823%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001824
1825
Greg Wardfacb8db2000-04-09 04:32:40 +00001826\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001827\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001828
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001829The install command ensures that the build commands have been run and then
1830runs the subcommands \command{install\_lib},
1831\command{install\_data} and
1832\command{install\_scripts}.
1833
Fred Drakea09262e2001-03-01 18:35:43 +00001834%\subsubsection{\protect\command{install\_lib}}
1835%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001836
1837\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001838\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001839This command installs all data files provided with the distribution.
1840
1841\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001842\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001843This command installs all (Python) scripts in the distribution.
1844
Greg Ward16aafcd2000-04-09 04:06:44 +00001845
Fred Drakea09262e2001-03-01 18:35:43 +00001846%\subsection{Cleaning up: the \protect\command{clean} command}
1847%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001848
1849
Fred Drakeeff9a872000-10-26 16:41:03 +00001850\subsection{Creating a source distribution: the
1851 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001852\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001853
1854
1855\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001856
Greg Ward16aafcd2000-04-09 04:06:44 +00001857The manifest template commands are:
1858\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001859 \lineii{include \var{pat1} \var{pat2} ... }
1860 {include all files matching any of the listed patterns}
1861 \lineii{exclude \var{pat1} \var{pat2} ... }
1862 {exclude all files matching any of the listed patterns}
1863 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1864 {include all files under \var{dir} matching any of the listed patterns}
1865 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1866 {exclude all files under \var{dir} matching any of the listed patterns}
1867 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001868 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001869 any of the listed patterns}
1870 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001871 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001872 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001873 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1874 \lineii{graft \var{dir}}{include all files under \var{dir}}
1875\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001876The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001877sequence of regular filename characters, \code{?} matches any single
1878regular filename character, and \code{[\var{range}]} matches any of the
1879characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001880\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001881platform-specific: on \UNIX{} it is anything except slash; on Windows
1882anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001883
Fred Drakeeff9a872000-10-26 16:41:03 +00001884\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001885
1886
Fred Drakea09262e2001-03-01 18:35:43 +00001887%\subsection{Creating a built distribution: the
1888% \protect\command{bdist} command family}
1889%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001890
1891
Fred Drakeab70b382001-08-02 15:13:15 +00001892%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001893
Fred Drakeab70b382001-08-02 15:13:15 +00001894%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001895
Fred Drakeab70b382001-08-02 15:13:15 +00001896%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001897
Fred Drakeab70b382001-08-02 15:13:15 +00001898%\subsubsection{\protect\command{bdist\_wininst}}
1899
1900
1901\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001902
1903
Greg Wardabc52162000-02-26 00:52:48 +00001904\end{document}