blob: dd2b971d486a78dbd81af7dee50ee7a33b34b558 [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
Greg Ward16aafcd2000-04-09 04:06:44 +00006\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +00007
Greg Wardabc52162000-02-26 00:52:48 +00008\author{Greg Ward}
9\authoraddress{E-mail: \email{gward@python.net}}
10
Greg Warde3cca262000-08-31 16:36:31 +000011\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000012
Greg Wardabc52162000-02-26 00:52:48 +000013\begin{document}
14
Greg Wardfacb8db2000-04-09 04:32:40 +000015\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000016\begin{abstract}
17 \noindent
18 This document describes the Python Distribution Utilities
19 (``Distutils'') from the module developer's point-of-view, describing
20 how to use the Distutils to make Python modules and extensions easily
21 available to a wider audience with very little overhead for
22 build/release/install mechanics.
23\end{abstract}
24
Greg Wardfacb8db2000-04-09 04:32:40 +000025\tableofcontents
Greg Ward16aafcd2000-04-09 04:06:44 +000026
27\section{Introduction}
Greg Warde78298a2000-04-28 17:12:24 +000028\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000029
30In the past, Python module developers have not had much infrastructure
31support for distributing modules, nor have Python users had much support
32for installing and maintaining third-party modules. With the
33introduction of the Python Distribution Utilities (Distutils for short)
Greg Ward1d8f57a2000-08-05 00:43:11 +000034in Python 1.6, this situation should start to improve.
Greg Ward16aafcd2000-04-09 04:06:44 +000035
36This document only covers using the Distutils to distribute your Python
Greg Ward1d8f57a2000-08-05 00:43:11 +000037modules. Using the Distutils does not tie you to Python 1.6, though:
38the Distutils work just fine with Python 1.5.2, and it is reasonable
39(and expected to become commonplace) to expect users of Python 1.5.2 to
Greg Ward16aafcd2000-04-09 04:06:44 +000040download and install the Distutils separately before they can install
Greg Ward1d8f57a2000-08-05 00:43:11 +000041your modules. Python 1.6 (or later) users, of course, won't have to add
42anything to their Python installation in order to use the Distutils to
43install third-party modules.
Greg Ward16aafcd2000-04-09 04:06:44 +000044
45This document concentrates on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000046you're looking for information on installing Python modules, you
47should refer to the \citetitle[../inst/inst.html]{Installing Python
48Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000049
50
Greg Wardfacb8db2000-04-09 04:32:40 +000051\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000052\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000053
54Using the Distutils is quite simple, both for module developers and for
55users/administrators installing third-party modules. As a developer,
56your responsibilites (apart from writing solid, well-documented and
57well-tested code, of course!) are:
58\begin{itemize}
59\item write a setup script (\file{setup.py} by convention)
60\item (optional) write a setup configuration file
61\item create a source distribution
62\item (optional) create one or more built (binary) distributions
63\end{itemize}
64Each of these tasks is covered in this document.
65
66Not all module developers have access to a multitude of platforms, so
67it's not always feasible to expect them to create a multitude of built
68distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000069\emph{packagers}, will arise to address this need. Packagers will take
70source distributions released by module developers, build them on one or
71more platforms, and release the resulting built distributions. Thus,
72users on the most popular platforms will be able to install most popular
73Python module distributions in the most natural way for their platform,
74without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000075
76
77\subsection{A simple example}
Greg Warde78298a2000-04-28 17:12:24 +000078\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000079
80The setup script is usually quite simple, although since it's written in
Greg Ward47f99a62000-09-04 20:07:15 +000081Python, there are no arbitrary limits to what you can do with
82it.\footnote{But be careful about putting arbitrarily expensive
83 operations in your setup script; unlike, say, Autoconf-style configure
84 scripts, the setup script may be run multiple times in the course of
85 building and installing your module distribution. If you need to
86 insert potentially expensive processing steps into the Distutils
87 chain, see section~\ref{extending} on extending the Distutils.} If
Greg Ward1d8f57a2000-08-05 00:43:11 +000088all you want to do is distribute a module called \module{foo}, contained
89in a file \file{foo.py}, then your setup script can be as little as
90this:
Greg Ward16aafcd2000-04-09 04:06:44 +000091\begin{verbatim}
92from distutils.core import setup
93setup (name = "foo",
94 version = "1.0",
95 py_modules = ["foo"])
96\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000097
Greg Ward16aafcd2000-04-09 04:06:44 +000098Some observations:
99\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000100\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000101 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000102\item those keyword arguments fall into two categories: package
103 meta-data (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000104 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000105\item modules are specified by module name, not filename (the same will
106 hold true for packages and extensions)
107\item it's recommended that you supply a little more meta-data, in
108 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000109 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000110\end{itemize}
111
Greg Ward370248d2000-06-24 01:45:47 +0000112To create a source distribution for this module, you would create a
113setup script, \file{setup.py}, containing the above code, and run:
Greg Ward16aafcd2000-04-09 04:06:44 +0000114\begin{verbatim}
115python setup.py sdist
116\end{verbatim}
Fred Drakeeff9a872000-10-26 16:41:03 +0000117which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Greg Ward16aafcd2000-04-09 04:06:44 +0000118Windows) containing your setup script, \file{setup.py}, and your module,
119\file{foo.py}. The archive file will be named \file{Foo-1.0.tar.gz} (or
120\file{.zip}), and will unpack into a directory \file{Foo-1.0}.
121
122If an end-user wishes to install your \module{foo} module, all she has
Greg Ward59d382e2000-05-26 01:04:47 +0000123to do is download \file{Foo-1.0.tar.gz} (or \file{.zip}), unpack it,
Greg Ward16aafcd2000-04-09 04:06:44 +0000124and---from the \file{Foo-1.0} directory---run
125\begin{verbatim}
126python setup.py install
127\end{verbatim}
128which will ultimately copy \file{foo.py} to the appropriate directory
129for third-party modules in their Python installation.
130
131This simple example demonstrates some fundamental concepts of the
132Distutils: first, both developers and installers have the same basic
133user interface, i.e. the setup script. The difference is which
134Distutils \emph{commands} they use: the \command{sdist} command is
135almost exclusively for module developers, while \command{install} is
136more often for installers (although most developers will want to install
137their own code occasionally).
138
Greg Ward16aafcd2000-04-09 04:06:44 +0000139If you want to make things really easy for your users, you can create
140one or more built distributions for them. For instance, if you are
141running on a Windows machine, and want to make things easy for other
142Windows users, you can create an executable installer (the most
143appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000144\command{bdist\_wininst} command. For example:
Greg Ward16aafcd2000-04-09 04:06:44 +0000145\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000146python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000147\end{verbatim}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000148will create an executable installer, \file{Foo-1.0.win32.exe}, in the
149current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000150
Greg Ward1d8f57a2000-08-05 00:43:11 +0000151\XXX{not implemented yet}
Greg Ward59d382e2000-05-26 01:04:47 +0000152(Another way to create executable installers for Windows is with the
153\command{bdist\_wise} command, which uses Wise---the commercial
154installer-generator used to create Python's own installer---to create
155the installer. Wise-based installers are more appropriate for large,
156industrial-strength applications that need the full capabilities of a
157``real'' installer. \command{bdist\_wininst} creates a self-extracting
158zip file with a minimal user interface, which is enough for small- to
159medium-sized module collections. You'll need to have version XXX of
Greg Ward370248d2000-06-24 01:45:47 +0000160Wise installed on your system for the \command{bdist\_wise} command to
161work; it's available from \url{http://foo/bar/baz}.)
Greg Ward59d382e2000-05-26 01:04:47 +0000162
Greg Ward47f99a62000-09-04 20:07:15 +0000163Currently (Distutils 0.9.2), the are only other useful built
Greg Ward1d8f57a2000-08-05 00:43:11 +0000164distribution format is RPM, implemented by the \command{bdist\_rpm}
165command. For example, the following command will create an RPM file
166called \file{Foo-1.0.noarch.rpm}:
167\begin{verbatim}
168python setup.py bdist_rpm
169\end{verbatim}
170(This uses the \command{rpm} command, so has to be run on an RPM-based
171system such as Red Hat Linux, SuSE Linux, or Mandrake Linux.)
172
173You can find out what distribution formats are available at any time by
174running
175\begin{verbatim}
176python setup.py bdist --help-formats
177\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000178
179
180\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000181\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000182
183If you're reading this document, you probably have a good idea of what
184modules, extensions, and so forth are. Nevertheless, just to be sure
185that everyone is operating from a common starting point, we offer the
186following glossary of common Python terms:
187\begin{description}
188\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000189 code imported by some other code. Three types of modules concern us
190 here: pure Python modules, extension modules, and packages.
Greg Ward16aafcd2000-04-09 04:06:44 +0000191\item[pure Python module] a module written in Python and contained in a
192 single \file{.py} file (and possibly associated \file{.pyc} and/or
193 \file{.pyo} files). Sometimes referred to as a ``pure module.''
194\item[extension module] a module written in the low-level language of
Fred Drakeeff9a872000-10-26 16:41:03 +0000195 the Python implemention: C/C++ for Python, Java for JPython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000196 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000197 file, e.g. a shared object (\file{.so}) file for Python extensions on
198 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Greg Ward1bbe3292000-06-25 03:14:13 +0000199 on Windows, or a Java class file for JPython extensions. (Note that
Fred Drakeeff9a872000-10-26 16:41:03 +0000200 currently, the Distutils only handles C/C++ extensions for Python.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000201\item[package] a module that contains other modules; typically contained
202 in a directory in the filesystem and distinguished from other
203 directories by the presence of a file \file{\_\_init\_\_.py}.
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}
211 can contribute 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
225 mxDateTime. (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.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000228\item[pure module distribution] a module distribution that contains only
229 pure Python modules and packages. Sometimes referred to as a ``pure
230 distribution.''
231\item[non-pure module distribution] a module distribution that contains
232 at least one extension module. Sometimes referred to as a ``non-pure
233 distribution.''
234\item[distribution root] the top-level directory of your source tree (or
235 source distribution); the directory where \file{setup.py} exists and
236 is run from
237\end{description}
238
239
240\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000241\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000242
243The setup script is the centre of all activity in building,
244distributing, and installing modules using the Distutils. The main
245purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000246the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000247do the right thing. As we saw in section~\ref{simple-example} above,
248the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000249most information supplied to the Distutils by the module developer is
250supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000251
252Here's a slightly more involved example, which we'll follow for the next
253couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000254although the Distutils are included with Python 1.6 and later, they also
255have an independent existence so that Python 1.5.2 users can use them to
256install other module distributions. The Distutils' own setup script,
257shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000258
259\begin{verbatim}
260#!/usr/bin/env python
261
262from distutils.core import setup
263
264setup (name = "Distutils",
265 version = "1.0",
Greg Ward1d8f57a2000-08-05 00:43:11 +0000266 description = "Python Distribution Utilities",
Greg Ward16aafcd2000-04-09 04:06:44 +0000267 author = "Greg Ward",
268 author_email = "gward@python.net",
269 url = "http://www.python.org/sigs/distutils-sig/",
270
271 packages = ['distutils', 'distutils.command'],
272 )
273\end{verbatim}
274There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000275distribution presented in section~\ref{simple-example}: more
Greg Ward16aafcd2000-04-09 04:06:44 +0000276meta-data, and the specification of pure Python modules by package,
277rather than by module. This is important since the Distutils consist of
278a couple of dozen modules split into (so far) two packages; an explicit
279list of every module would be tedious to generate and difficult to
280maintain.
281
Greg Ward46b98e32000-04-14 01:53:36 +0000282Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000283script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000284slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000285platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000286current platform before actually using the pathname. This makes your
287setup script portable across operating systems, which of course is one
288of the major goals of the Distutils. In this spirit, all pathnames in
Fred Drakeeff9a872000-10-26 16:41:03 +0000289this document are slash-separated (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000290mind that the \emph{absence} of a leading slash indicates a relative
Fred Drakeeff9a872000-10-26 16:41:03 +0000291path, the opposite of the MacOS convention with colons).
Greg Ward46b98e32000-04-14 01:53:36 +0000292
Greg Ward16aafcd2000-04-09 04:06:44 +0000293
Greg Ward2afffd42000-08-06 20:37:24 +0000294\subsection{Listing whole packages}
295\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000296
297The \option{packages} option tells the Distutils to process (build,
298distribute, install, etc.) all pure Python modules found in each package
299mentioned in the \option{packages} list. In order to do this, of
300course, there has to be a correspondence between package names and
301directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000302obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000303\file{distutils} relative to the distribution root. Thus, when you say
304\code{packages = ['foo']} in your setup script, you are promising that
305the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
306be spelled differently on your system, but you get the idea) relative to
307the directory where your setup script lives. (If you break this
308promise, the Distutils will issue a warning but process the broken
309package anyways.)
310
311If you use a different convention to lay out your source directory,
312that's no problem: you just have to supply the \option{package\_dir}
313option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000314you keep all Python source under \file{lib}, so that modules in the
315``root package'' (i.e., not in any package at all) are right in
316\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
317and so forth. Then you would put
Greg Ward16aafcd2000-04-09 04:06:44 +0000318\begin{verbatim}
319package_dir = {'': 'lib'}
320\end{verbatim}
321in your setup script. (The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000322and an empty package name stands for the root package. The values are
323directory names relative to your distribution root.) In this case, when
324you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000325\file{lib/foo/\_\_init\_\_.py} exists.
326
Greg Ward1ecc2512000-04-19 22:36:24 +0000327Another possible convention is to put the \module{foo} package right in
328\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000329would be written in the setup script as
330\begin{verbatim}
331package_dir = {'foo': 'lib'}
332\end{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000333A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
334dictionary implicitly applies to all packages below \var{package}, so
335the \module{foo.bar} case is automatically handled here. In this
336example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
337to look for \file{lib/\_\_init\_\_.py} and
338\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
339\option{package\_dir} applies recursively, you must explicitly list all
340packages in \option{packages}: the Distutils will \emph{not} recursively
341scan your source tree looking for any directory with an
342\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000343
344
345\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000346\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000347
348For a small module distribution, you might prefer to list all modules
349rather than listing packages---especially the case of a single module
350that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000351simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000352slightly more involved example:
353\begin{verbatim}
354py_modules = ['mod1', 'pkg.mod2']
355\end{verbatim}
356This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000357other in the \module{pkg} package. Again, the default package/directory
358layout implies that these two modules can be found in \file{mod1.py} and
359\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000360And again, you can override the package/directory correspondence using
361the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000362
363
364\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000365\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000366
Greg Ward2afffd42000-08-06 20:37:24 +0000367Just as writing Python extension modules is a bit more complicated than
368writing pure Python modules, describing them to the Distutils is a bit
369more complicated. Unlike pure modules, it's not enough just to list
370modules or packages and expect the Distutils to go out and find the
371right files; you have to specify the extension name, source file(s), and
372any compile/link requirements (include directories, libraries to link
373with, etc.).
374
375All of this is done through another keyword argument to
376\function{setup()}, the \option{extensions} option. \option{extensions}
377is just a list of \class{Extension} instances, each of which describes a
378single extension module. Suppose your distribution includes a single
379extension, called \module{foo} and implemented by \file{foo.c}. If no
380additional instructions to the compiler/linker are needed, describing
381this extension is quite simple:
382\begin{verbatim}
383Extension("foo", ["foo.c"])
384\end{verbatim}
385The \class{Extension} class can be imported from
386\module{distutils.core}, along with \function{setup()}. Thus, the setup
387script for a module distribution that contains only this one extension
388and nothing else might be:
389\begin{verbatim}
390from distutils.core import setup, Extension
391setup(name = "foo", version = "1.0",
Greg Ward078fc082000-09-12 23:08:53 +0000392 ext_modules = [Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000393\end{verbatim}
394
395The \class{Extension} class (actually, the underlying extension-building
396machinery implemented by the \command{built\_ext} command) supports a
397great deal of flexibility in describing Python extensions, which is
398explained in the following sections.
399
400
401\subsubsection{Extension names and packages}
402
403The first argument to the \class{Extension} constructor is always the
404name of the extension, including any package names. For example,
405\begin{verbatim}
406Extension("foo", ["src/foo1.c", "src/foo2.c"])
407\end{verbatim}
408describes an extension that lives in the root package, while
409\begin{verbatim}
410Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
411\end{verbatim}
412describes the same extension in the \module{pkg} package. The source
413files and resulting object code are identical in both cases; the only
414difference is where in the filesystem (and therefore where in Python's
415namespace hierarchy) the resulting extension lives.
416
417If you have a number of extensions all in the same package (or all under
418the same base package), use the \option{ext\_package} keyword argument
419to \function{setup()}. For example,
420\begin{verbatim}
421setup(...
422 ext_package = "pkg",
Greg Ward078fc082000-09-12 23:08:53 +0000423 ext_modules = [Extension("foo", ["foo.c"]),
424 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000425 )
426\end{verbatim}
427will compile \file{foo.c} to the extension \module{pkg.foo}, and
428\file{bar.c} to \module{pkg.subpkg.bar}.
429
430
431\subsubsection{Extension source files}
432
433The second argument to the \class{Extension} constructor is a list of
434source files. Since the Distutils currently only support C/C++
435extensions, these are normally C/C++ source files. (Be sure to use
436appropriate extensions to distinguish C++ source files: \file{.cc} and
Fred Drakeeff9a872000-10-26 16:41:03 +0000437\file{.cpp} seem to be recognized by both \UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000438
439However, you can also include SWIG interface (\file{.i}) files in the
440list; the \command{build\_ext} command knows how to deal with SWIG
441extensions: it will run SWIG on the interface file and compile the
442resulting C/C++ file into your extension.
443
444\XXX{SWIG support is rough around the edges and largely untested;
445 especially SWIG support of C++ extensions! Explain in more detail
446 here when the interface firms up.}
447
448On some platforms, you can include non-source files that are processed
449by the compiler and included in your extension. Currently, this just
450means Windows resource files for Visual C++. \XXX{get more detail on
451 this feature from Thomas Heller!}
452
453
454\subsubsection{Preprocessor options}
455
456Three optional arguments to \class{Extension} will help if you need to
457specify include directories to search or preprocessor macros to
458define/undefine: \code{include\_dirs}, \code{define\_macros}, and
459\code{undef\_macros}.
460
461For example, if your extension requires header files in the
462\file{include} directory under your distribution root, use the
463\code{include\_dirs} option:
464\begin{verbatim}
465Extension("foo", ["foo.c"], include_dirs=["include"])
466\end{verbatim}
467
468You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000469extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000470\file{/usr}, you can get away with
471\begin{verbatim}
472Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
473\end{verbatim}
474You should avoid this sort of non-portable usage if you plan to
475distribute your code: it's probably better to write your code to include
476(e.g.) \code{<X11/Xlib.h>}.
477
478If you need to include header files from some other Python extension,
479you can take advantage of the fact that the Distutils install extension
480header files in a consistent way. For example, the Numerical Python
Fred Drakeeff9a872000-10-26 16:41:03 +0000481header files are installed (on a standard \UNIX{} installation) to
Greg Ward2afffd42000-08-06 20:37:24 +0000482\file{/usr/local/include/python1.5/Numerical}. (The exact location will
483differ according to your platform and Python installation.) Since the
484Python include directory---\file{/usr/local/include/python1.5} in this
485case---is always included in the search path when building Python
486extensions, the best approach is to include (e.g.)
487\code{<Numerical/arrayobject.h>}. If you insist on putting the
488\file{Numerical} include directory right into your header search path,
489though, you can find that directory using the Distutils
490\module{sysconfig} module:
491\begin{verbatim}
492from distutils.sysconfig import get_python_inc
493incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
494setup(...,
495 Extension(..., include_dirs=[incdir]))
496\end{verbatim}
497Even though this is quite portable---it will work on any Python
498installation, regardless of platform---it's probably easier to just
499write your C code in the sensible way.
500
501You can define and undefine pre-processor macros with the
502\code{define\_macros} and \code{undef\_macros} options.
503\code{define\_macros} takes a list of \code{(name, value)} tuples, where
504\code{name} is the name of the macro to define (a string) and
505\code{value} is its value: either a string or \code{None}. (Defining a
506macro \code{FOO} to \code{None} is the equivalent of a bare
507\code{\#define FOO} in your C source: with most compilers, this sets
508\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
509a list of macros to undefine.
510
511For example:
512\begin{verbatim}
513Extension(...,
514 define_macros=[('NDEBUG', '1')],
515 ('HAVE_STRFTIME', None),
516 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
517\end{verbatim}
518is the equivalent of having this at the top of every C source file:
519\begin{verbatim}
520#define NDEBUG 1
521#define HAVE_STRFTIME
522#undef HAVE_FOO
523#undef HAVE_BAR
524\end{verbatim}
525
526
527\subsubsection{Library options}
528
529You can also specify the libraries to link against when building your
530extension, and the directories to search for those libraries. The
531\code{libraries} option is a list of libraries to link against,
532\code{library\_dirs} is a list of directories to search for libraries at
533link-time, and \code{runtime\_library\_dirs} is a list of directories to
534search for shared (dynamically loaded) libraries at run-time.
535
536For example, if you need to link against libraries known to be in the
537standard library search path on target systems
538\begin{verbatim}
539Extension(...,
540 libraries=["gdbm", "readline"])
541\end{verbatim}
542
543If you need to link with libraries in a non-standard location, you'll
544have to include the location in \code{library\_dirs}:
545\begin{verbatim}
546Extension(...,
547 library_dirs=["/usr/X11R6/lib"],
548 libraries=["X11", "Xt"])
549\end{verbatim}
550(Again, this sort of non-portable construct should be avoided if you
551intend to distribute your code.)
552
553\XXX{still undocumented: extra\_objects, extra\_compile\_args,
554 extra\_link\_args, export\_symbols---none of which are frequently
555 needed, some of which might be completely unnecessary!}
Greg Ward16aafcd2000-04-09 04:06:44 +0000556
557
558\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000559\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000560
Greg Ward16aafcd2000-04-09 04:06:44 +0000561Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000562distribution \emph{a priori}: you may need to get some information from
563the user, or from the user's system, in order to proceed. As long as
564that information is fairly simple---a list of directories to search for
565C header files or libraries, for example---then providing a
566configuration file, \file{setup.cfg}, for users to edit is a cheap and
567easy way to solicit it. Configuration files also let you provide
568default values for any command option, which the installer can then
569override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000570
Greg Ward47f99a62000-09-04 20:07:15 +0000571(If you have more advanced needs, such as determining which extensions
572to build based on what capabilities are present on the target system,
573then you need the Distutils ``auto-configuration'' facility. This
574started to appear in Distutils 0.9 but, as of this writing, isn't mature
575or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000576
Greg Ward47f99a62000-09-04 20:07:15 +0000577\XXX{should reference description of distutils config files in
578 ``Installing'' manual here}
579
580The setup configuration file is a useful middle-ground between the setup
581script---which, ideally, would be opaque to installers\footnote{This
582 ideal probably won't be achieved until auto-configuration is fully
583 supported by the Distutils.}---and the command-line to the setup
584script, which is outside of your control and entirely up to the
585installer. In fact, \file{setup.cfg} (and any other Distutils
586configuration files present on the target system) are processed after
587the contents of the setup script, but before the command-line. This has
588several useful consequences:
589\begin{itemize}
590\item installers can override some of what you put in \file{setup.py} by
591 editing \file{setup.cfg}
592\item you can provide non-standard defaults for options that are not
593 easily set in \file{setup.py}
594\item installers can override anything in \file{setup.cfg} using the
595 command-line options to \file{setup.py}
596\end{itemize}
597
598The basic syntax of the configuration file is simple:
599\begin{verbatim}
600[command]
601option=value
602...
603\end{verbatim}
604where \var{command} is one of the Distutils commands (e.g.
605\command{build\_py}, \command{install}), and \var{option} is one of the
606options that command supports. Any number of options can be supplied
607for each command, and any number of command sections can be included in
Fred Drake78489a32000-11-22 16:06:16 +0000608the file. Blank lines are ignored, as are comments (from a
609\character{\#} character to end-of-line). Long option values can be
610split across multiple lines simply by indenting the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000611
612You can find out the list of options supported by a particular command
613with the universal \longprogramopt{help} option, e.g.
614\begin{verbatim}
615> python setup.py --help build_ext
616[...]
617Options for 'build_ext' command:
618 --build-lib (-b) directory for compiled extension modules
619 --build-temp (-t) directory for temporary files (build by-products)
620 --inplace (-i) ignore build-lib and put compiled extensions into the
621 source directory alongside your pure Python modules
622 --include-dirs (-I) list of directories to search for header files
623 --define (-D) C preprocessor macros to define
624 --undef (-U) C preprocessor macros to undefine
625[...]
626\end{verbatim}
627Or consult section \ref{reference} of this document (the command
628reference).
629
630Note that an option spelled \longprogramopt{foo-bar} on the command-line
631is spelled \option{foo\_bar} in configuration files.
632
633For example, say you want your extensions to be built
634``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000635want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000636in the same source directory as your pure Python modules
637\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
638\longprogramopt{inplace} option on the command-line to ensure this:
639\begin{verbatim}
640python setup.py build_ext --inplace
641\end{verbatim}
642But this requires that you always specify the \command{build\_ext}
643command explicitly, and remember to provide \longprogramopt{inplace}.
644An easier way is to ``set and forget'' this option, by encoding it in
645\file{setup.cfg}, the configuration file for this distribution:
646\begin{verbatim}
647[build_ext]
648inplace=1
649\end{verbatim}
650This will affect all builds of this module distribution, whether or not
651you explcitly specify \command{build\_ext}. If you include
652\file{setup.cfg} in your source distribution, it will also affect
653end-user builds---which is probably a bad idea for this option, since
654always building extensions in-place would break installation of the
655module distribution. In certain peculiar cases, though, modules are
656built right in their installation directory, so this is conceivably a
657useful ability. (Distributing extensions that expect to be built in
658their installation directory is almost always a bad idea, though.)
659
660Another example: certain commands take a lot of options that don't
661change from run-to-run; for example, \command{bdist\_rpm} needs to know
662everything required to generate a ``spec'' file for creating an RPM
663distribution. Some of this information comes from the setup script, and
664some is automatically generated by the Distutils (such as the list of
665files installed). But some of it has to be supplied as options to
666\command{bdist\_rpm}, which would be very tedious to do on the
667command-line for every run. Hence, here is a snippet from the
668Distutils' own \file{setup.cfg}:
669\begin{verbatim}
670[bdist_rpm]
671release = 1
672packager = Greg Ward <gward@python.net>
673doc_files = CHANGES.txt
674 README.txt
675 USAGE.txt
676 doc/
677 examples/
678\end{verbatim}
679Note that the \option{doc\_files} option is simply a
680whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000681
682
683\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000684\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000685
Greg Warde78298a2000-04-28 17:12:24 +0000686As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000687\command{sdist} command to create a source distribution. In the
688simplest case,
689\begin{verbatim}
690python setup.py sdist
691\end{verbatim}
Greg Ward19c67f82000-06-24 01:33:16 +0000692(assuming you haven't specified any \command{sdist} options in the setup
693script or config file), \command{sdist} creates the archive of the
Greg Ward54589d42000-09-06 01:37:35 +0000694default format for the current platform. The default format is gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000695tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
696\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000697
Greg Wardd5767a52000-04-19 22:48:09 +0000698You can specify as many formats as you like using the
699\longprogramopt{formats} option, for example:
Greg Ward16aafcd2000-04-09 04:06:44 +0000700\begin{verbatim}
701python setup.py sdist --formats=gztar,zip
702\end{verbatim}
703to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000704\begin{tableiii}{l|l|c}{code}%
705 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000706 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
707 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000708 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.gz})}{(4)}
709 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000710 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000711\end{tableiii}
712
713\noindent Notes:
714\begin{description}
715\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000716\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000717\item[(3)] requires either external \program{zip} utility or
718 \module{zipfile} module (not part of the standard Python library)
Greg Ward47f99a62000-09-04 20:07:15 +0000719\item[(4)] requires external utilities: \program{tar} and possibly one
720 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000721\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000722
723
Greg Ward54589d42000-09-06 01:37:35 +0000724
725\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000726\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000727
Greg Ward54589d42000-09-06 01:37:35 +0000728If you don't supply an explicit list of files (or instructions on how to
729generate one), the \command{sdist} command puts a minimal default set
730into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000731\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000732\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000733 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000734\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000735 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000736 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000737\item anything that looks like a test script: \file{test/test*.py}
738 (currently, the Distutils don't do anything with test scripts except
739 include them in source distributions, but in the future there will be
740 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000741\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
742 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000743\end{itemize}
744Sometimes this is enough, but usually you will want to specify
745additional files to distribute. The typical way to do this is to write
746a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000747manifest template is just a list of instructions for how to generate
748your manifest file, \file{MANIFEST}, which is the exact list of files to
749include in your source distribution. The \command{sdist} command
750processes this template and generates a manifest based on its
751instructions and what it finds in the filesystem.
752
753If you prefer to roll your own manifest file, the format is simple: one
754filename per line, regular files (or symlinks to them) only. If you do
755supply your own \file{MANIFEST}, you must specify everything: the
756default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000757
758The manifest template has one command per line, where each command
759specifies a set of files to include or exclude from the source
760distribution. For an example, again we turn to the Distutils' own
761manifest template:
762\begin{verbatim}
763include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000764recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000765prune examples/sample?/build
766\end{verbatim}
767The meanings should be fairly clear: include all files in the
768distribution root matching \code{*.txt}, all files anywhere under the
769\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000770exclude all directories matching \code{examples/sample?/build}. All of
771this is done \emph{after} the standard include set, so you can exclude
772files from the standard set with explicit instructions in the manifest
773template. (Or, you can use the \longprogramopt{no-defaults} option to
774disable the standard set entirely.) There are several other commands
775available in the manifest template mini-language; see
776section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000777
Greg Ward54589d42000-09-06 01:37:35 +0000778The order of commands in the manifest template matters: initially, we
779have the list of default files as described above, and each command in
780the template adds to or removes from that list of files. Once we have
781fully processed the manifest template, we remove files that should not
782be included in the source distribution:
783\begin{itemize}
784\item all files in the Distutils ``build'' tree (default \file{build/})
785\item all files in directories named \file{RCS} or \file{CVS}
786\end{itemize}
787Now we have our complete list of files, which is written to the manifest
788for future reference, and then used to build the source distribution
789archive(s).
790
791You can disable the default set of included files with the
792\longprogramopt{no-defaults} option, and you can disable the standard
793exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000794
Greg Ward46b98e32000-04-14 01:53:36 +0000795Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000796\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000797Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000798\begin{enumerate}
799\item include all Python source files in the \file{distutils} and
800 \file{distutils/command} subdirectories (because packages
801 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000802 \option{packages} option in the setup script---see
803 section~\ref{setup-script})
804\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
805 (standard files)
806\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000807\item include \file{*.txt} in the distribution root (this will find
808 \file{README.txt} a second time, but such redundancies are weeded out
809 later)
Greg Ward54589d42000-09-06 01:37:35 +0000810\item include anything matching \file{*.txt} or \file{*.py} in the
811 sub-tree under \file{examples},
812\item exclude all files in the sub-trees starting at directories
813 matching \file{examples/sample?/build}---this may exclude files
814 included by the previous two steps, so it's important that the
815 \code{prune} command in the manifest template comes after the
816 \code{recursive-include} command
817\item exclude the entire \file{build} tree, and any \file{RCS} or
818 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000819\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000820Just like in the setup script, file and directory names in the manifest
821template should always be slash-separated; the Distutils will take care
822of converting them to the standard representation on your platform.
823That way, the manifest template is portable across operating systems.
824
Greg Ward16aafcd2000-04-09 04:06:44 +0000825
826\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000827\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000828
829The normal course of operations for the \command{sdist} command is as
830follows:
831\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000832\item if the manifest file, \file{MANIFEST} doesn't exist, read
833 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000834\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
835 manifest with just the default file set\footnote{In versions of the
836 Distutils up to and including 0.9.2 (Python 2.0b1), this feature was
837 broken; use the \programopt{-f} (\longprogramopt{force-manifest})
838 option to work around the bug.}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000839\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
840 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
841 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000842\item use the list of files now in \file{MANIFEST} (either just
843 generated or read in) to create the source distribution archive(s)
844\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000845There are a couple of options that modify this behaviour. First, use
846the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
847disable the standard ``include'' and ``exclude'' sets.\footnote{Note
848 that if you have no manifest template, no manifest, and use the
849 \longprogramopt{no-defaults}, you will get an empty manifest. Another
850 bug in Distutils 0.9.2 and earlier causes an uncaught exception in
851 this case. The workaround is: Don't Do That.}
Greg Ward16aafcd2000-04-09 04:06:44 +0000852
Greg Ward54589d42000-09-06 01:37:35 +0000853Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000854example, if you have added or removed files or directories that match an
855existing pattern in the manifest template, you should regenerate the
856manifest:
857\begin{verbatim}
858python setup.py sdist --force-manifest
859\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000860
861Or, you might just want to (re)generate the manifest, but not create a
862source distribution:
863\begin{verbatim}
864python setup.py sdist --manifest-only
865\end{verbatim}
Greg Ward54589d42000-09-06 01:37:35 +0000866\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
867\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
868\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000869
870
871\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +0000872\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000873
Greg Ward46b98e32000-04-14 01:53:36 +0000874A ``built distribution'' is what you're probably used to thinking of
875either as a ``binary package'' or an ``installer'' (depending on your
876background). It's not necessarily binary, though, because it might
877contain only Python source code and/or byte-code; and we don't call it a
878package, because that word is already spoken for in Python. (And
879``installer'' is a term specific to the Windows world. \XXX{do Mac
880 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +0000881
Greg Ward46b98e32000-04-14 01:53:36 +0000882A built distribution is how you make life as easy as possible for
883installers of your module distribution: for users of RPM-based Linux
884systems, it's a binary RPM; for Windows users, it's an executable
885installer; for Debian-based Linux users, it's a Debian package; and so
886forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +0000887distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +0000888designed to enable module developers to concentrate on their
889specialty---writing code and creating source distributions---while an
890intermediary species of \emph{packager} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +0000891distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +0000892are packagers.
893
894Of course, the module developer could be his own packager; or the
895packager could be a volunteer ``out there'' somewhere who has access to
896a platform which the original developer does not; or it could be
897software periodically grabbing new source distributions and turning them
898into built distributions for as many platforms as the software has
899access to. Regardless of the nature of the beast, a packager uses the
900setup script and the \command{bdist} command family to generate built
901distributions.
902
903As a simple example, if I run the following command in the Distutils
904source tree:
905\begin{verbatim}
906python setup.py bdist
907\end{verbatim}
908then the Distutils builds my module distribution (the Distutils itself
909in this case), does a ``fake'' installation (also in the \file{build}
910directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +0000911platform. The default format for built distributions is a ``dumb'' tar
Fred Drakeeff9a872000-10-26 16:41:03 +0000912file on \UNIX, and an simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +0000913file is considered ``dumb'' because it has to be unpacked in a specific
914location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000915
Fred Drakeeff9a872000-10-26 16:41:03 +0000916Thus, the above command on a \UNIX{} system creates
Greg Ward1d8f57a2000-08-05 00:43:11 +0000917\file{Distutils-0.9.1.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +0000918from the right place installs the Distutils just as though you had
919downloaded the source distribution and run \code{python setup.py
920 install}. (The ``right place'' is either the root of the filesystem or
921Python's \filevar{prefix} directory, depending on the options given to
922the \command{bdist\_dumb} command; the default is to make dumb
923distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +0000924
Greg Wardb6528972000-09-07 02:40:37 +0000925Obviously, for pure Python distributions, this isn't a huge win---but
926for non-pure distributions, which include extensions that would need to
927be compiled, it can mean the difference between someone being able to
928use your extensions or not. And creating ``smart'' built distributions,
929such as an RPM package or an executable installer for Windows, is a big
930win for users even if your distribution doesn't include any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +0000931
Greg Wardb6528972000-09-07 02:40:37 +0000932The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000933similar to the \command{sdist} command, which you can use to select the
934types of built distribution to generate: for example,
Greg Ward46b98e32000-04-14 01:53:36 +0000935\begin{verbatim}
936python setup.py bdist --format=zip
937\end{verbatim}
Fred Drakeeff9a872000-10-26 16:41:03 +0000938would, when run on a \UNIX{} system, create
Greg Ward1d8f57a2000-08-05 00:43:11 +0000939\file{Distutils-0.8.\filevar{plat}.zip}---again, this archive would be
940unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +0000941
942The available formats for built distributions are:
943\begin{tableiii}{l|l|c}{code}%
944 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +0000945 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
946 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
947 \lineiii{tar}{tar file (\file{.tar})}{(3)}
948 \lineiii{zip}{zip file (\file{.zip})}{(4)}
949 \lineiii{rpm}{RPM}{(5)}
950 \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
951 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(6)}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000952 %\lineiii{wise}{Wise installer for Windows}{(3)}
Greg Ward46b98e32000-04-14 01:53:36 +0000953\end{tableiii}
954
955\noindent Notes:
956\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +0000957\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +0000958\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +0000959\item[(3)] requires external utilities: \program{tar} and possibly one
960 of \program{gzip}, \program{bzip2}, or \program{compress}
961\item[(4)] requires either external \program{zip} utility or
962 \module{zipfile} module (not part of the standard Python library)
963\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
964 better (use \code{rpm --version} to find out which version you have)
965\item[(6)] \XXX{requirements for \command{bdist\_wininst}?}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000966%\item[(3)] not implemented yet
Greg Ward46b98e32000-04-14 01:53:36 +0000967\end{description}
968
969You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +0000970\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000971directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +0000972\command{bdist} ``sub-commands'' actually generate several similar
973formats; for instance, the \command{bdist\_dumb} command generates all
974the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
975\code{zip}), and \command{bdist\_rpm} generates both binary and source
976RPMs. The \command{bdist} sub-commands, and the formats generated by
977each, are:
978\begin{tableii}{l|l}{command}%
979 {Command}{Formats}
980 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
981 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000982 \lineii{bdist\_wininst}{wininst}
983 %\lineii{bdist\_wise}{wise}
Greg Ward46b98e32000-04-14 01:53:36 +0000984\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +0000985
Greg Wardb6528972000-09-07 02:40:37 +0000986The following sections give details on the individual \command{bdist\_*}
987commands.
988
989
990\subsection{Creating dumb built distributions}
991\label{creating-dumb}
992
993\XXX{Need to document absolute vs. prefix-relative packages here, but
994 first I have to implement it!}
995
996
997\subsection{Creating RPM packages}
998\label{creating-rpms}
999
1000The RPM format is used by many of popular Linux distributions, including
1001Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1002RPM-based Linux distributions) is your usual environment, creating RPM
1003packages for other users of that same distribution is trivial.
1004Depending on the complexity of your module distribution and differences
1005between Linux distributions, you may also be able to create RPMs that
1006work on different RPM-based distributions.
1007
1008The usual way to create an RPM of your module distribution is to run the
1009\command{bdist\_rpm} command:
1010\begin{verbatim}
1011python setup.py bdist_rpm
1012\end{verbatim}
1013or the \command{bdist} command with the \longprogramopt{format} option:
1014\begin{verbatim}
1015python setup.py bdist --formats=rpm
1016\end{verbatim}
1017The former allows you to specify RPM-specific options; the latter allows
1018you to easily specify multiple formats in one run. If you need to do
1019both, you can explicitly specify multiple \command{bdist\_*} commands
1020and their options:
1021\begin{verbatim}
1022python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1023 bdist_wininst --target_version="2.0"
1024\end{verbatim}
1025
1026Creating RPM packages is driven by a \file{.spec} file, much as using
1027the Distutils is driven by the setup script. To make your life easier,
1028the \command{bdist\_rpm} command normally creates a \file{.spec} file
1029based on the information you supply in the setup script, on the command
1030line, and in any Distutils configuration files. Various options and
1031section in the \file{.spec} file are derived from options in the setup
1032script as follows:
1033\begin{tableii}{l|l}{textrm}%
1034 {RPM \file{.spec} file option or section}{Distutils setup script option}
1035 \lineii{Name}{\option{name}}
1036 \lineii{Summary (in preamble)}{\option{description}}
1037 \lineii{Version}{\option{version}}
1038 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1039 \option{maintainer} and \option{maintainer\_email}}
1040 \lineii{Copyright}{\option{licence}}
1041 \lineii{Url}{\option{url}}
1042 \lineii{\%description (section)}{\option{long\_description}}
1043\end{tableii}
1044
1045Additionally, there many options in \file{.spec} files that don't have
1046corresponding options in the setup script. Most of these are handled
1047through options to the \command{bdist\_rpm} command as follows:
1048\begin{tableiii}{l|l|l}{textrm}%
1049 {RPM \file{.spec} file option or section}%
1050 {\command{bdist\_rpm} option}%
1051 {default value}
1052 \lineiii{Release}{\option{release}}{``1''}
1053 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1054 \lineiii{Vendor}{\option{vendor}}{(see above)}
1055 \lineiii{Packager}{packager}{(none)}
1056 \lineiii{Provides}{provides}{(none)}
1057 \lineiii{Requires}{requires}{(none)}
1058 \lineiii{Conflicts}{conflicts}{(none)}
1059 \lineiii{Obsoletes}{obsoletes}{(none)}
1060 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1061 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1062 \lineiii{Icon}{\option{icon}}{(none)}
1063\end{tableiii}
1064Obviously, supplying even a few of these options on the command-line
1065would be tedious and error-prone, so it's usually best to put them in
1066the setup configuration file, \file{setup.cfg}---see
1067section~\ref{setup-config}. If you distribute or package many Python
1068module distributions, you might want to put options that apply to all of
1069them in your personal Distutils configuration file
1070(\file{\textasciitilde/.pydistutils.cfg}).
1071
1072There are three steps to building a binary RPM package, all of which are
1073handled automatically by the Distutils:
1074\begin{enumerate}
1075\item create a \file{.spec} file, which describes the package (analogous
1076 to the Distutils setup script; in fact, much of the information in the
1077 setup script winds up in the \file{.spec} file)
1078\item create the source RPM
1079\item create the ``binary'' RPM (which may or may not contain binary
1080 code, depending on whether your module distribution contains Python
1081 extensions)
1082\end{enumerate}
1083Normally, RPM bundles the last two steps together; when you use the
1084Distutils, all three steps are typically bundled together.
1085
1086If you wish, you can separate these three steps. You can use the
1087\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1088create the \file{.spec} file and exit; in this case, the \file{.spec}
1089file will be written to the ``distribution directory''---normally
1090\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1091option. (Normally, the \file{.spec} file winds up deep in the ``build
1092tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1093
1094\XXX{this isn't implemented yet---is it needed?!}
1095You can also specify a custom \file{.spec} file with the
1096\longprogramopt{spec-file} option; used in conjunctin with
1097\longprogramopt{spec-only}, this gives you an opportunity to customize
1098the \file{.spec} file manually:
1099\begin{verbatim}
1100> python setup.py bdist_rpm --spec-only
1101# ...edit dist/FooBar-1.0.spec
1102> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1103\end{verbatim}
1104(Although a better way to do this is probably to override the standard
1105\command{bdist\_rpm} command with one that writes whatever else you want
1106to the \file{.spec} file; see section~\ref{extending} for information on
1107extending the Distutils.)
1108
1109
1110\subsection{Creating Windows installers}
1111\label{creating-wininst}
1112
1113
1114
Greg Ward16aafcd2000-04-09 04:06:44 +00001115\section{Examples}
Greg Warde78298a2000-04-28 17:12:24 +00001116\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001117
1118
1119\subsection{Pure Python distribution (by module)}
Greg Warde78298a2000-04-28 17:12:24 +00001120\label{pure-mod}
Greg Ward16aafcd2000-04-09 04:06:44 +00001121
1122
1123\subsection{Pure Python distribution (by package)}
Greg Warde78298a2000-04-28 17:12:24 +00001124\label{pure-pkg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001125
1126
1127\subsection{Single extension module}
Greg Warde78298a2000-04-28 17:12:24 +00001128\label{single-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001129
1130
1131\subsection{Multiple extension modules}
Greg Warde78298a2000-04-28 17:12:24 +00001132\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001133
1134
1135\subsection{Putting it all together}
1136
1137
Greg Ward4a9e7222000-04-25 02:57:36 +00001138
1139\section{Extending the Distutils}
Greg Warde78298a2000-04-28 17:12:24 +00001140\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001141
1142
1143\subsection{Extending existing commands}
Greg Warde78298a2000-04-28 17:12:24 +00001144\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001145
1146
1147\subsection{Writing new commands}
Greg Warde78298a2000-04-28 17:12:24 +00001148\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001149
1150
1151
Greg Ward16aafcd2000-04-09 04:06:44 +00001152\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001153\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001154
1155
Greg Wardfacb8db2000-04-09 04:32:40 +00001156\subsection{Building modules: the \protect\command{build} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001157\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001158
Greg Wardfacb8db2000-04-09 04:32:40 +00001159\subsubsection{\protect\command{build}}
Greg Warde78298a2000-04-28 17:12:24 +00001160\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001161
Greg Wardfacb8db2000-04-09 04:32:40 +00001162\subsubsection{\protect\command{build\_py}}
Greg Warde78298a2000-04-28 17:12:24 +00001163\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001164
Greg Wardfacb8db2000-04-09 04:32:40 +00001165\subsubsection{\protect\command{build\_ext}}
Greg Warde78298a2000-04-28 17:12:24 +00001166\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001167
Greg Wardfacb8db2000-04-09 04:32:40 +00001168\subsubsection{\protect\command{build\_clib}}
Greg Warde78298a2000-04-28 17:12:24 +00001169\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001170
1171
Greg Wardfacb8db2000-04-09 04:32:40 +00001172\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001173\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001174
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001175The install command ensures that the build commands have been run and then
1176runs the subcommands \command{install\_lib},
1177\command{install\_data} and
1178\command{install\_scripts}.
1179
1180\subsubsection{\protect\command{install\_lib}}
Greg Ward1365a302000-08-31 14:47:05 +00001181\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001182
1183\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001184\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001185This command installs all data files provided with the distribution.
1186
1187\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001188\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001189This command installs all (Python) scripts in the distribution.
1190
Greg Ward16aafcd2000-04-09 04:06:44 +00001191
Greg Wardfacb8db2000-04-09 04:32:40 +00001192\subsection{Cleaning up: the \protect\command{clean} command}
Greg Warde78298a2000-04-28 17:12:24 +00001193\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001194
1195
Fred Drakeeff9a872000-10-26 16:41:03 +00001196\subsection{Creating a source distribution: the
1197 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001198\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001199
1200
1201\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001202
Greg Ward16aafcd2000-04-09 04:06:44 +00001203The manifest template commands are:
1204\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001205 \lineii{include \var{pat1} \var{pat2} ... }
1206 {include all files matching any of the listed patterns}
1207 \lineii{exclude \var{pat1} \var{pat2} ... }
1208 {exclude all files matching any of the listed patterns}
1209 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1210 {include all files under \var{dir} matching any of the listed patterns}
1211 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1212 {exclude all files under \var{dir} matching any of the listed patterns}
1213 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001214 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001215 any of the listed patterns}
1216 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001217 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001218 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001219 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1220 \lineii{graft \var{dir}}{include all files under \var{dir}}
1221\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001222The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001223sequence of regular filename characters, \code{?} matches any single
1224regular filename character, and \code{[\var{range}]} matches any of the
1225characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001226\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001227platform-specific: on \UNIX{} it is anything except slash; on Windows
1228anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001229
Fred Drakeeff9a872000-10-26 16:41:03 +00001230\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001231
1232
Greg Wardd5767a52000-04-19 22:48:09 +00001233\subsection{Creating a ``built'' distribution: the
1234 \protect\command{bdist} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001235\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001236
1237
Greg Wardfacb8db2000-04-09 04:32:40 +00001238\subsubsection{\protect\command{blib}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001239
Greg Wardfacb8db2000-04-09 04:32:40 +00001240\subsubsection{\protect\command{blib\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001241
Greg Wardfacb8db2000-04-09 04:32:40 +00001242\subsubsection{\protect\command{blib\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001243
Greg Wardfacb8db2000-04-09 04:32:40 +00001244\subsubsection{\protect\command{blib\_wise}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001245
1246
1247
1248
1249
1250
1251
1252
Greg Wardabc52162000-02-26 00:52:48 +00001253\end{document}