blob: 679d9fd3649da2782fd2973bbc51bfbe20419ff9 [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
2\usepackage{ltxmarkup}
Greg Wardfacb8db2000-04-09 04:32:40 +00003\usepackage{times}
Greg Ward16aafcd2000-04-09 04:06:44 +00004\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00005
Greg Wardb6528972000-09-07 02:40:37 +00006% $Id$
7
Greg Ward16aafcd2000-04-09 04:06:44 +00008\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +00009
Greg Wardabc52162000-02-26 00:52:48 +000010\author{Greg Ward}
11\authoraddress{E-mail: \email{gward@python.net}}
12
Greg Warde3cca262000-08-31 16:36:31 +000013\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000014
Greg Wardabc52162000-02-26 00:52:48 +000015\begin{document}
16
Greg Wardfacb8db2000-04-09 04:32:40 +000017\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000018\begin{abstract}
19 \noindent
20 This document describes the Python Distribution Utilities
21 (``Distutils'') from the module developer's point-of-view, describing
22 how to use the Distutils to make Python modules and extensions easily
23 available to a wider audience with very little overhead for
24 build/release/install mechanics.
25\end{abstract}
26
Greg Wardfacb8db2000-04-09 04:32:40 +000027\tableofcontents
Greg Ward16aafcd2000-04-09 04:06:44 +000028
29\section{Introduction}
Greg Warde78298a2000-04-28 17:12:24 +000030\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000031
32In the past, Python module developers have not had much infrastructure
33support for distributing modules, nor have Python users had much support
34for installing and maintaining third-party modules. With the
35introduction of the Python Distribution Utilities (Distutils for short)
Greg Ward1d8f57a2000-08-05 00:43:11 +000036in Python 1.6, this situation should start to improve.
Greg Ward16aafcd2000-04-09 04:06:44 +000037
38This document only covers using the Distutils to distribute your Python
Greg Ward1d8f57a2000-08-05 00:43:11 +000039modules. Using the Distutils does not tie you to Python 1.6, though:
40the Distutils work just fine with Python 1.5.2, and it is reasonable
41(and expected to become commonplace) to expect users of Python 1.5.2 to
Greg Ward16aafcd2000-04-09 04:06:44 +000042download and install the Distutils separately before they can install
Greg Ward1d8f57a2000-08-05 00:43:11 +000043your modules. Python 1.6 (or later) users, of course, won't have to add
44anything to their Python installation in order to use the Distutils to
45install third-party modules.
Greg Ward16aafcd2000-04-09 04:06:44 +000046
47This document concentrates on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000048you're looking for information on installing Python modules, you
49should refer to the \citetitle[../inst/inst.html]{Installing Python
50Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000051
52
Greg Wardfacb8db2000-04-09 04:32:40 +000053\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000054\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000055
56Using the Distutils is quite simple, both for module developers and for
57users/administrators installing third-party modules. As a developer,
58your responsibilites (apart from writing solid, well-documented and
59well-tested code, of course!) are:
60\begin{itemize}
61\item write a setup script (\file{setup.py} by convention)
62\item (optional) write a setup configuration file
63\item create a source distribution
64\item (optional) create one or more built (binary) distributions
65\end{itemize}
66Each of these tasks is covered in this document.
67
68Not all module developers have access to a multitude of platforms, so
69it's not always feasible to expect them to create a multitude of built
70distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000071\emph{packagers}, will arise to address this need. Packagers will take
72source distributions released by module developers, build them on one or
73more platforms, and release the resulting built distributions. Thus,
74users on the most popular platforms will be able to install most popular
75Python module distributions in the most natural way for their platform,
76without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000077
78
79\subsection{A simple example}
Greg Warde78298a2000-04-28 17:12:24 +000080\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000081
82The setup script is usually quite simple, although since it's written in
Greg Ward47f99a62000-09-04 20:07:15 +000083Python, there are no arbitrary limits to what you can do with
84it.\footnote{But be careful about putting arbitrarily expensive
85 operations in your setup script; unlike, say, Autoconf-style configure
86 scripts, the setup script may be run multiple times in the course of
87 building and installing your module distribution. If you need to
88 insert potentially expensive processing steps into the Distutils
89 chain, see section~\ref{extending} on extending the Distutils.} If
Greg Ward1d8f57a2000-08-05 00:43:11 +000090all you want to do is distribute a module called \module{foo}, contained
91in a file \file{foo.py}, then your setup script can be as little as
92this:
Greg Ward16aafcd2000-04-09 04:06:44 +000093\begin{verbatim}
94from distutils.core import setup
95setup (name = "foo",
96 version = "1.0",
97 py_modules = ["foo"])
98\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000099
Greg Ward16aafcd2000-04-09 04:06:44 +0000100Some observations:
101\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000102\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000103 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000104\item those keyword arguments fall into two categories: package
105 meta-data (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000106 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000107\item modules are specified by module name, not filename (the same will
108 hold true for packages and extensions)
109\item it's recommended that you supply a little more meta-data, in
110 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000111 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000112\end{itemize}
113
Greg Ward370248d2000-06-24 01:45:47 +0000114To create a source distribution for this module, you would create a
115setup script, \file{setup.py}, containing the above code, and run:
Greg Ward16aafcd2000-04-09 04:06:44 +0000116\begin{verbatim}
117python setup.py sdist
118\end{verbatim}
119which will create an archive file (e.g., tarball on Unix, zip file on
120Windows) containing your setup script, \file{setup.py}, and your module,
121\file{foo.py}. The archive file will be named \file{Foo-1.0.tar.gz} (or
122\file{.zip}), and will unpack into a directory \file{Foo-1.0}.
123
124If an end-user wishes to install your \module{foo} module, all she has
Greg Ward59d382e2000-05-26 01:04:47 +0000125to do is download \file{Foo-1.0.tar.gz} (or \file{.zip}), unpack it,
Greg Ward16aafcd2000-04-09 04:06:44 +0000126and---from the \file{Foo-1.0} directory---run
127\begin{verbatim}
128python setup.py install
129\end{verbatim}
130which will ultimately copy \file{foo.py} to the appropriate directory
131for third-party modules in their Python installation.
132
133This simple example demonstrates some fundamental concepts of the
134Distutils: first, both developers and installers have the same basic
135user interface, i.e. the setup script. The difference is which
136Distutils \emph{commands} they use: the \command{sdist} command is
137almost exclusively for module developers, while \command{install} is
138more often for installers (although most developers will want to install
139their own code occasionally).
140
Greg Ward16aafcd2000-04-09 04:06:44 +0000141If you want to make things really easy for your users, you can create
142one or more built distributions for them. For instance, if you are
143running on a Windows machine, and want to make things easy for other
144Windows users, you can create an executable installer (the most
145appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000146\command{bdist\_wininst} command. For example:
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}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000150will create an executable installer, \file{Foo-1.0.win32.exe}, in the
151current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000152
Greg Ward1d8f57a2000-08-05 00:43:11 +0000153\XXX{not implemented yet}
Greg Ward59d382e2000-05-26 01:04:47 +0000154(Another way to create executable installers for Windows is with the
155\command{bdist\_wise} command, which uses Wise---the commercial
156installer-generator used to create Python's own installer---to create
157the installer. Wise-based installers are more appropriate for large,
158industrial-strength applications that need the full capabilities of a
159``real'' installer. \command{bdist\_wininst} creates a self-extracting
160zip file with a minimal user interface, which is enough for small- to
161medium-sized module collections. You'll need to have version XXX of
Greg Ward370248d2000-06-24 01:45:47 +0000162Wise installed on your system for the \command{bdist\_wise} command to
163work; it's available from \url{http://foo/bar/baz}.)
Greg Ward59d382e2000-05-26 01:04:47 +0000164
Greg Ward47f99a62000-09-04 20:07:15 +0000165Currently (Distutils 0.9.2), the are only other useful built
Greg Ward1d8f57a2000-08-05 00:43:11 +0000166distribution format is RPM, implemented by the \command{bdist\_rpm}
167command. For example, the following command will create an RPM file
168called \file{Foo-1.0.noarch.rpm}:
169\begin{verbatim}
170python setup.py bdist_rpm
171\end{verbatim}
172(This uses the \command{rpm} command, so has to be run on an RPM-based
173system such as Red Hat Linux, SuSE Linux, or Mandrake Linux.)
174
175You can find out what distribution formats are available at any time by
176running
177\begin{verbatim}
178python setup.py bdist --help-formats
179\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000180
181
182\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000183\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000184
185If you're reading this document, you probably have a good idea of what
186modules, extensions, and so forth are. Nevertheless, just to be sure
187that everyone is operating from a common starting point, we offer the
188following glossary of common Python terms:
189\begin{description}
190\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000191 code imported by some other code. Three types of modules concern us
192 here: pure Python modules, extension modules, and packages.
Greg Ward16aafcd2000-04-09 04:06:44 +0000193\item[pure Python module] a module written in Python and contained in a
194 single \file{.py} file (and possibly associated \file{.pyc} and/or
195 \file{.pyo} files). Sometimes referred to as a ``pure module.''
196\item[extension module] a module written in the low-level language of
197 the Python implemention: C/C++ for CPython, Java for JPython.
198 Typically contained in a single dynamically loadable pre-compiled
199 file, e.g. a shared object (\file{.so}) file for CPython extensions on
200 Unix, a DLL (given the \file{.pyd} extension) for CPython extensions
Greg Ward1bbe3292000-06-25 03:14:13 +0000201 on Windows, or a Java class file for JPython extensions. (Note that
202 currently, the Distutils only handles C/C++ extensions for CPython.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000203\item[package] a module that contains other modules; typically contained
204 in a directory in the filesystem and distinguished from other
205 directories by the presence of a file \file{\_\_init\_\_.py}.
Greg Ward6153fa12000-05-26 02:24:28 +0000206\item[root package] the root of the hierarchy of packages. (This isn't
207 really a package, since it doesn't have an \file{\_\_init\_\_.py}
208 file. But we have to call it something.) The vast majority of the
209 standard library is in the root package, as are many small, standalone
210 third-party modules that don't belong to a larger module collection.
211 Unlike regular packages, modules in the root package can be found in
212 many directories: in fact, every directory listed in \code{sys.path}
213 can contribute modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000214\end{description}
215
216
217\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000218\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000219
220The following terms apply more specifically to the domain of
221distributing Python modules using the Distutils:
222\begin{description}
223\item[module distribution] a collection of Python modules distributed
224 together as a single downloadable resource and meant to be installed
225 \emph{en masse}. Examples of some well-known module distributions are
226 Numeric Python, PyXML, PIL (the Python Imaging Library), or
227 mxDateTime. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000228 is already taken in the Python context: a single module distribution
229 may contain zero, one, or many Python packages.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000230\item[pure module distribution] a module distribution that contains only
231 pure Python modules and packages. Sometimes referred to as a ``pure
232 distribution.''
233\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.''
236\item[distribution root] the top-level directory of your source tree (or
237 source distribution); the directory where \file{setup.py} exists and
238 is run from
239\end{description}
240
241
242\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000243\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000244
245The setup script is the centre of all activity in building,
246distributing, and installing modules using the Distutils. The main
247purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000248the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000249do the right thing. As we saw in section~\ref{simple-example} above,
250the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000251most information supplied to the Distutils by the module developer is
252supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000253
254Here's a slightly more involved example, which we'll follow for the next
255couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000256although the Distutils are included with Python 1.6 and later, they also
257have an independent existence so that Python 1.5.2 users can use them to
258install other module distributions. The Distutils' own setup script,
259shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000260
261\begin{verbatim}
262#!/usr/bin/env python
263
264from distutils.core import setup
265
266setup (name = "Distutils",
267 version = "1.0",
Greg Ward1d8f57a2000-08-05 00:43:11 +0000268 description = "Python Distribution Utilities",
Greg Ward16aafcd2000-04-09 04:06:44 +0000269 author = "Greg Ward",
270 author_email = "gward@python.net",
271 url = "http://www.python.org/sigs/distutils-sig/",
272
273 packages = ['distutils', 'distutils.command'],
274 )
275\end{verbatim}
276There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000277distribution presented in section~\ref{simple-example}: more
Greg Ward16aafcd2000-04-09 04:06:44 +0000278meta-data, and the specification of pure Python modules by package,
279rather than by module. This is important since the Distutils consist of
280a couple of dozen modules split into (so far) two packages; an explicit
281list of every module would be tedious to generate and difficult to
282maintain.
283
Greg Ward46b98e32000-04-14 01:53:36 +0000284Note that any pathnames (files or directories) supplied in the setup
285script should be written using the Unix convention, i.e.
286slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000287platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000288current platform before actually using the pathname. This makes your
289setup script portable across operating systems, which of course is one
290of the major goals of the Distutils. In this spirit, all pathnames in
Greg Ward59d382e2000-05-26 01:04:47 +0000291this document are slash-separated (Mac OS programmers should keep in
292mind that the \emph{absence} of a leading slash indicates a relative
293path, the opposite of the Mac OS convention with colons).
Greg Ward46b98e32000-04-14 01:53:36 +0000294
Greg Ward16aafcd2000-04-09 04:06:44 +0000295
Greg Ward2afffd42000-08-06 20:37:24 +0000296\subsection{Listing whole packages}
297\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000298
299The \option{packages} option tells the Distutils to process (build,
300distribute, install, etc.) all pure Python modules found in each package
301mentioned in the \option{packages} list. In order to do this, of
302course, there has to be a correspondence between package names and
303directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000304obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000305\file{distutils} relative to the distribution root. Thus, when you say
306\code{packages = ['foo']} in your setup script, you are promising that
307the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
308be spelled differently on your system, but you get the idea) relative to
309the directory where your setup script lives. (If you break this
310promise, the Distutils will issue a warning but process the broken
311package anyways.)
312
313If you use a different convention to lay out your source directory,
314that's no problem: you just have to supply the \option{package\_dir}
315option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000316you keep all Python source under \file{lib}, so that modules in the
317``root package'' (i.e., not in any package at all) are right in
318\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
319and so forth. Then you would put
Greg Ward16aafcd2000-04-09 04:06:44 +0000320\begin{verbatim}
321package_dir = {'': 'lib'}
322\end{verbatim}
323in your setup script. (The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000324and an empty package name stands for the root package. The values are
325directory names relative to your distribution root.) In this case, when
326you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000327\file{lib/foo/\_\_init\_\_.py} exists.
328
Greg Ward1ecc2512000-04-19 22:36:24 +0000329Another possible convention is to put the \module{foo} package right in
330\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000331would be written in the setup script as
332\begin{verbatim}
333package_dir = {'foo': 'lib'}
334\end{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000335A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
336dictionary implicitly applies to all packages below \var{package}, so
337the \module{foo.bar} case is automatically handled here. In this
338example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
339to look for \file{lib/\_\_init\_\_.py} and
340\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
341\option{package\_dir} applies recursively, you must explicitly list all
342packages in \option{packages}: the Distutils will \emph{not} recursively
343scan your source tree looking for any directory with an
344\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000345
346
347\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000348\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000349
350For a small module distribution, you might prefer to list all modules
351rather than listing packages---especially the case of a single module
352that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000353simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000354slightly more involved example:
355\begin{verbatim}
356py_modules = ['mod1', 'pkg.mod2']
357\end{verbatim}
358This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000359other in the \module{pkg} package. Again, the default package/directory
360layout implies that these two modules can be found in \file{mod1.py} and
361\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000362And again, you can override the package/directory correspondence using
363the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000364
365
366\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000367\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000368
Greg Ward2afffd42000-08-06 20:37:24 +0000369Just as writing Python extension modules is a bit more complicated than
370writing pure Python modules, describing them to the Distutils is a bit
371more complicated. Unlike pure modules, it's not enough just to list
372modules or packages and expect the Distutils to go out and find the
373right files; you have to specify the extension name, source file(s), and
374any compile/link requirements (include directories, libraries to link
375with, etc.).
376
377All of this is done through another keyword argument to
378\function{setup()}, the \option{extensions} option. \option{extensions}
379is just a list of \class{Extension} instances, each of which describes a
380single extension module. Suppose your distribution includes a single
381extension, called \module{foo} and implemented by \file{foo.c}. If no
382additional instructions to the compiler/linker are needed, describing
383this extension is quite simple:
384\begin{verbatim}
385Extension("foo", ["foo.c"])
386\end{verbatim}
387The \class{Extension} class can be imported from
388\module{distutils.core}, along with \function{setup()}. Thus, the setup
389script for a module distribution that contains only this one extension
390and nothing else might be:
391\begin{verbatim}
392from distutils.core import setup, Extension
393setup(name = "foo", version = "1.0",
394 extensions = [Extension("foo", ["foo.c"])])
395\end{verbatim}
396
397The \class{Extension} class (actually, the underlying extension-building
398machinery implemented by the \command{built\_ext} command) supports a
399great deal of flexibility in describing Python extensions, which is
400explained in the following sections.
401
402
403\subsubsection{Extension names and packages}
404
405The first argument to the \class{Extension} constructor is always the
406name of the extension, including any package names. For example,
407\begin{verbatim}
408Extension("foo", ["src/foo1.c", "src/foo2.c"])
409\end{verbatim}
410describes an extension that lives in the root package, while
411\begin{verbatim}
412Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
413\end{verbatim}
414describes the same extension in the \module{pkg} package. The source
415files and resulting object code are identical in both cases; the only
416difference is where in the filesystem (and therefore where in Python's
417namespace hierarchy) the resulting extension lives.
418
419If you have a number of extensions all in the same package (or all under
420the same base package), use the \option{ext\_package} keyword argument
421to \function{setup()}. For example,
422\begin{verbatim}
423setup(...
424 ext_package = "pkg",
425 extensions = [Extension("foo", ["foo.c"]),
426 Extension("subpkg.bar", ["bar.c"])]
427 )
428\end{verbatim}
429will compile \file{foo.c} to the extension \module{pkg.foo}, and
430\file{bar.c} to \module{pkg.subpkg.bar}.
431
432
433\subsubsection{Extension source files}
434
435The second argument to the \class{Extension} constructor is a list of
436source files. Since the Distutils currently only support C/C++
437extensions, these are normally C/C++ source files. (Be sure to use
438appropriate extensions to distinguish C++ source files: \file{.cc} and
439\file{.cpp} seem to be recognized by both Unix and Windows compilers.)
440
441However, you can also include SWIG interface (\file{.i}) files in the
442list; the \command{build\_ext} command knows how to deal with SWIG
443extensions: it will run SWIG on the interface file and compile the
444resulting C/C++ file into your extension.
445
446\XXX{SWIG support is rough around the edges and largely untested;
447 especially SWIG support of C++ extensions! Explain in more detail
448 here when the interface firms up.}
449
450On some platforms, you can include non-source files that are processed
451by the compiler and included in your extension. Currently, this just
452means Windows resource files for Visual C++. \XXX{get more detail on
453 this feature from Thomas Heller!}
454
455
456\subsubsection{Preprocessor options}
457
458Three optional arguments to \class{Extension} will help if you need to
459specify include directories to search or preprocessor macros to
460define/undefine: \code{include\_dirs}, \code{define\_macros}, and
461\code{undef\_macros}.
462
463For example, if your extension requires header files in the
464\file{include} directory under your distribution root, use the
465\code{include\_dirs} option:
466\begin{verbatim}
467Extension("foo", ["foo.c"], include_dirs=["include"])
468\end{verbatim}
469
470You can specify absolute directories there; if you know that your
471extension will only be built on Unix systems with X11R6 installed to
472\file{/usr}, you can get away with
473\begin{verbatim}
474Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
475\end{verbatim}
476You should avoid this sort of non-portable usage if you plan to
477distribute your code: it's probably better to write your code to include
478(e.g.) \code{<X11/Xlib.h>}.
479
480If you need to include header files from some other Python extension,
481you can take advantage of the fact that the Distutils install extension
482header files in a consistent way. For example, the Numerical Python
483header files are installed (on a standard Unix installation) to
484\file{/usr/local/include/python1.5/Numerical}. (The exact location will
485differ according to your platform and Python installation.) Since the
486Python include directory---\file{/usr/local/include/python1.5} in this
487case---is always included in the search path when building Python
488extensions, the best approach is to include (e.g.)
489\code{<Numerical/arrayobject.h>}. If you insist on putting the
490\file{Numerical} include directory right into your header search path,
491though, you can find that directory using the Distutils
492\module{sysconfig} module:
493\begin{verbatim}
494from distutils.sysconfig import get_python_inc
495incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
496setup(...,
497 Extension(..., include_dirs=[incdir]))
498\end{verbatim}
499Even though this is quite portable---it will work on any Python
500installation, regardless of platform---it's probably easier to just
501write your C code in the sensible way.
502
503You can define and undefine pre-processor macros with the
504\code{define\_macros} and \code{undef\_macros} options.
505\code{define\_macros} takes a list of \code{(name, value)} tuples, where
506\code{name} is the name of the macro to define (a string) and
507\code{value} is its value: either a string or \code{None}. (Defining a
508macro \code{FOO} to \code{None} is the equivalent of a bare
509\code{\#define FOO} in your C source: with most compilers, this sets
510\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
511a list of macros to undefine.
512
513For example:
514\begin{verbatim}
515Extension(...,
516 define_macros=[('NDEBUG', '1')],
517 ('HAVE_STRFTIME', None),
518 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
519\end{verbatim}
520is the equivalent of having this at the top of every C source file:
521\begin{verbatim}
522#define NDEBUG 1
523#define HAVE_STRFTIME
524#undef HAVE_FOO
525#undef HAVE_BAR
526\end{verbatim}
527
528
529\subsubsection{Library options}
530
531You can also specify the libraries to link against when building your
532extension, and the directories to search for those libraries. The
533\code{libraries} option is a list of libraries to link against,
534\code{library\_dirs} is a list of directories to search for libraries at
535link-time, and \code{runtime\_library\_dirs} is a list of directories to
536search for shared (dynamically loaded) libraries at run-time.
537
538For example, if you need to link against libraries known to be in the
539standard library search path on target systems
540\begin{verbatim}
541Extension(...,
542 libraries=["gdbm", "readline"])
543\end{verbatim}
544
545If you need to link with libraries in a non-standard location, you'll
546have to include the location in \code{library\_dirs}:
547\begin{verbatim}
548Extension(...,
549 library_dirs=["/usr/X11R6/lib"],
550 libraries=["X11", "Xt"])
551\end{verbatim}
552(Again, this sort of non-portable construct should be avoided if you
553intend to distribute your code.)
554
555\XXX{still undocumented: extra\_objects, extra\_compile\_args,
556 extra\_link\_args, export\_symbols---none of which are frequently
557 needed, some of which might be completely unnecessary!}
Greg Ward16aafcd2000-04-09 04:06:44 +0000558
559
560\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000561\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000562
Greg Ward16aafcd2000-04-09 04:06:44 +0000563Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000564distribution \emph{a priori}: you may need to get some information from
565the user, or from the user's system, in order to proceed. As long as
566that information is fairly simple---a list of directories to search for
567C header files or libraries, for example---then providing a
568configuration file, \file{setup.cfg}, for users to edit is a cheap and
569easy way to solicit it. Configuration files also let you provide
570default values for any command option, which the installer can then
571override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000572
Greg Ward47f99a62000-09-04 20:07:15 +0000573(If you have more advanced needs, such as determining which extensions
574to build based on what capabilities are present on the target system,
575then you need the Distutils ``auto-configuration'' facility. This
576started to appear in Distutils 0.9 but, as of this writing, isn't mature
577or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000578
Greg Ward47f99a62000-09-04 20:07:15 +0000579\XXX{should reference description of distutils config files in
580 ``Installing'' manual here}
581
582The setup configuration file is a useful middle-ground between the setup
583script---which, ideally, would be opaque to installers\footnote{This
584 ideal probably won't be achieved until auto-configuration is fully
585 supported by the Distutils.}---and the command-line to the setup
586script, which is outside of your control and entirely up to the
587installer. In fact, \file{setup.cfg} (and any other Distutils
588configuration files present on the target system) are processed after
589the contents of the setup script, but before the command-line. This has
590several useful consequences:
591\begin{itemize}
592\item installers can override some of what you put in \file{setup.py} by
593 editing \file{setup.cfg}
594\item you can provide non-standard defaults for options that are not
595 easily set in \file{setup.py}
596\item installers can override anything in \file{setup.cfg} using the
597 command-line options to \file{setup.py}
598\end{itemize}
599
600The basic syntax of the configuration file is simple:
601\begin{verbatim}
602[command]
603option=value
604...
605\end{verbatim}
606where \var{command} is one of the Distutils commands (e.g.
607\command{build\_py}, \command{install}), and \var{option} is one of the
608options that command supports. Any number of options can be supplied
609for each command, and any number of command sections can be included in
610the file. Blank lines are ignored, as are comments (from a \verb+#+
611character to end-of-line). Long option values can be split across
612multiple lines simply by indenting the continuation lines.
613
614You can find out the list of options supported by a particular command
615with the universal \longprogramopt{help} option, e.g.
616\begin{verbatim}
617> python setup.py --help build_ext
618[...]
619Options for 'build_ext' command:
620 --build-lib (-b) directory for compiled extension modules
621 --build-temp (-t) directory for temporary files (build by-products)
622 --inplace (-i) ignore build-lib and put compiled extensions into the
623 source directory alongside your pure Python modules
624 --include-dirs (-I) list of directories to search for header files
625 --define (-D) C preprocessor macros to define
626 --undef (-U) C preprocessor macros to undefine
627[...]
628\end{verbatim}
629Or consult section \ref{reference} of this document (the command
630reference).
631
632Note that an option spelled \longprogramopt{foo-bar} on the command-line
633is spelled \option{foo\_bar} in configuration files.
634
635For example, say you want your extensions to be built
636``in-place''---that is, you have an extension \module{pkg.ext}, and you
637want the compiled extension file (\file{ext.so} on Unix, say) to be put
638in the same source directory as your pure Python modules
639\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
640\longprogramopt{inplace} option on the command-line to ensure this:
641\begin{verbatim}
642python setup.py build_ext --inplace
643\end{verbatim}
644But this requires that you always specify the \command{build\_ext}
645command explicitly, and remember to provide \longprogramopt{inplace}.
646An easier way is to ``set and forget'' this option, by encoding it in
647\file{setup.cfg}, the configuration file for this distribution:
648\begin{verbatim}
649[build_ext]
650inplace=1
651\end{verbatim}
652This will affect all builds of this module distribution, whether or not
653you explcitly specify \command{build\_ext}. If you include
654\file{setup.cfg} in your source distribution, it will also affect
655end-user builds---which is probably a bad idea for this option, since
656always building extensions in-place would break installation of the
657module distribution. In certain peculiar cases, though, modules are
658built right in their installation directory, so this is conceivably a
659useful ability. (Distributing extensions that expect to be built in
660their installation directory is almost always a bad idea, though.)
661
662Another example: certain commands take a lot of options that don't
663change from run-to-run; for example, \command{bdist\_rpm} needs to know
664everything required to generate a ``spec'' file for creating an RPM
665distribution. Some of this information comes from the setup script, and
666some is automatically generated by the Distutils (such as the list of
667files installed). But some of it has to be supplied as options to
668\command{bdist\_rpm}, which would be very tedious to do on the
669command-line for every run. Hence, here is a snippet from the
670Distutils' own \file{setup.cfg}:
671\begin{verbatim}
672[bdist_rpm]
673release = 1
674packager = Greg Ward <gward@python.net>
675doc_files = CHANGES.txt
676 README.txt
677 USAGE.txt
678 doc/
679 examples/
680\end{verbatim}
681Note that the \option{doc\_files} option is simply a
682whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000683
684
685\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000686\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000687
Greg Warde78298a2000-04-28 17:12:24 +0000688As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000689\command{sdist} command to create a source distribution. In the
690simplest case,
691\begin{verbatim}
692python setup.py sdist
693\end{verbatim}
Greg Ward19c67f82000-06-24 01:33:16 +0000694(assuming you haven't specified any \command{sdist} options in the setup
695script or config file), \command{sdist} creates the archive of the
Greg Ward54589d42000-09-06 01:37:35 +0000696default format for the current platform. The default format is gzip'ed
697tar file (\file{.tar.gz}) on Unix, and ZIP file on Windows. \XXX{no Mac
698 OS support here}
699
Greg Wardd5767a52000-04-19 22:48:09 +0000700You can specify as many formats as you like using the
701\longprogramopt{formats} option, for example:
Greg Ward16aafcd2000-04-09 04:06:44 +0000702\begin{verbatim}
703python setup.py sdist --formats=gztar,zip
704\end{verbatim}
705to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000706\begin{tableiii}{l|l|c}{code}%
707 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000708 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
709 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000710 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.gz})}{(4)}
711 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000712 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000713\end{tableiii}
714
715\noindent Notes:
716\begin{description}
717\item[(1)] default on Windows
Greg Ward54589d42000-09-06 01:37:35 +0000718\item[(2)] default on Unix
Greg Wardb6528972000-09-07 02:40:37 +0000719\item[(3)] requires either external \program{zip} utility or
720 \module{zipfile} module (not part of the standard Python library)
Greg Ward47f99a62000-09-04 20:07:15 +0000721\item[(4)] requires external utilities: \program{tar} and possibly one
722 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000723\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000724
725
Greg Ward54589d42000-09-06 01:37:35 +0000726
727\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000728\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000729
Greg Ward54589d42000-09-06 01:37:35 +0000730If you don't supply an explicit list of files (or instructions on how to
731generate one), the \command{sdist} command puts a minimal default set
732into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000733\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000734\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000735 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000736\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000737 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000738 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000739\item anything that looks like a test script: \file{test/test*.py}
740 (currently, the Distutils don't do anything with test scripts except
741 include them in source distributions, but in the future there will be
742 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000743\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
744 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000745\end{itemize}
746Sometimes this is enough, but usually you will want to specify
747additional files to distribute. The typical way to do this is to write
748a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000749manifest template is just a list of instructions for how to generate
750your manifest file, \file{MANIFEST}, which is the exact list of files to
751include in your source distribution. The \command{sdist} command
752processes this template and generates a manifest based on its
753instructions and what it finds in the filesystem.
754
755If you prefer to roll your own manifest file, the format is simple: one
756filename per line, regular files (or symlinks to them) only. If you do
757supply your own \file{MANIFEST}, you must specify everything: the
758default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000759
760The manifest template has one command per line, where each command
761specifies a set of files to include or exclude from the source
762distribution. For an example, again we turn to the Distutils' own
763manifest template:
764\begin{verbatim}
765include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000766recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000767prune examples/sample?/build
768\end{verbatim}
769The meanings should be fairly clear: include all files in the
770distribution root matching \code{*.txt}, all files anywhere under the
771\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000772exclude all directories matching \code{examples/sample?/build}. All of
773this is done \emph{after} the standard include set, so you can exclude
774files from the standard set with explicit instructions in the manifest
775template. (Or, you can use the \longprogramopt{no-defaults} option to
776disable the standard set entirely.) There are several other commands
777available in the manifest template mini-language; see
778section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000779
Greg Ward54589d42000-09-06 01:37:35 +0000780The order of commands in the manifest template matters: initially, we
781have the list of default files as described above, and each command in
782the template adds to or removes from that list of files. Once we have
783fully processed the manifest template, we remove files that should not
784be included in the source distribution:
785\begin{itemize}
786\item all files in the Distutils ``build'' tree (default \file{build/})
787\item all files in directories named \file{RCS} or \file{CVS}
788\end{itemize}
789Now we have our complete list of files, which is written to the manifest
790for future reference, and then used to build the source distribution
791archive(s).
792
793You can disable the default set of included files with the
794\longprogramopt{no-defaults} option, and you can disable the standard
795exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000796
Greg Ward46b98e32000-04-14 01:53:36 +0000797Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000798\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000799Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000800\begin{enumerate}
801\item include all Python source files in the \file{distutils} and
802 \file{distutils/command} subdirectories (because packages
803 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000804 \option{packages} option in the setup script---see
805 section~\ref{setup-script})
806\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
807 (standard files)
808\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000809\item include \file{*.txt} in the distribution root (this will find
810 \file{README.txt} a second time, but such redundancies are weeded out
811 later)
Greg Ward54589d42000-09-06 01:37:35 +0000812\item include anything matching \file{*.txt} or \file{*.py} in the
813 sub-tree under \file{examples},
814\item exclude all files in the sub-trees starting at directories
815 matching \file{examples/sample?/build}---this may exclude files
816 included by the previous two steps, so it's important that the
817 \code{prune} command in the manifest template comes after the
818 \code{recursive-include} command
819\item exclude the entire \file{build} tree, and any \file{RCS} or
820 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000821\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000822Just like in the setup script, file and directory names in the manifest
823template should always be slash-separated; the Distutils will take care
824of converting them to the standard representation on your platform.
825That way, the manifest template is portable across operating systems.
826
Greg Ward16aafcd2000-04-09 04:06:44 +0000827
828\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000829\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000830
831The normal course of operations for the \command{sdist} command is as
832follows:
833\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000834\item if the manifest file, \file{MANIFEST} doesn't exist, read
835 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000836\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
837 manifest with just the default file set\footnote{In versions of the
838 Distutils up to and including 0.9.2 (Python 2.0b1), this feature was
839 broken; use the \programopt{-f} (\longprogramopt{force-manifest})
840 option to work around the bug.}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000841\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
842 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
843 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000844\item use the list of files now in \file{MANIFEST} (either just
845 generated or read in) to create the source distribution archive(s)
846\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000847There are a couple of options that modify this behaviour. First, use
848the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
849disable the standard ``include'' and ``exclude'' sets.\footnote{Note
850 that if you have no manifest template, no manifest, and use the
851 \longprogramopt{no-defaults}, you will get an empty manifest. Another
852 bug in Distutils 0.9.2 and earlier causes an uncaught exception in
853 this case. The workaround is: Don't Do That.}
Greg Ward16aafcd2000-04-09 04:06:44 +0000854
Greg Ward54589d42000-09-06 01:37:35 +0000855Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000856example, if you have added or removed files or directories that match an
857existing pattern in the manifest template, you should regenerate the
858manifest:
859\begin{verbatim}
860python setup.py sdist --force-manifest
861\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000862
863Or, you might just want to (re)generate the manifest, but not create a
864source distribution:
865\begin{verbatim}
866python setup.py sdist --manifest-only
867\end{verbatim}
Greg Ward54589d42000-09-06 01:37:35 +0000868\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
869\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
870\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000871
872
873\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +0000874\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000875
Greg Ward46b98e32000-04-14 01:53:36 +0000876A ``built distribution'' is what you're probably used to thinking of
877either as a ``binary package'' or an ``installer'' (depending on your
878background). It's not necessarily binary, though, because it might
879contain only Python source code and/or byte-code; and we don't call it a
880package, because that word is already spoken for in Python. (And
881``installer'' is a term specific to the Windows world. \XXX{do Mac
882 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +0000883
Greg Ward46b98e32000-04-14 01:53:36 +0000884A built distribution is how you make life as easy as possible for
885installers of your module distribution: for users of RPM-based Linux
886systems, it's a binary RPM; for Windows users, it's an executable
887installer; for Debian-based Linux users, it's a Debian package; and so
888forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +0000889distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +0000890designed to enable module developers to concentrate on their
891specialty---writing code and creating source distributions---while an
892intermediary species of \emph{packager} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +0000893distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +0000894are packagers.
895
896Of course, the module developer could be his own packager; or the
897packager could be a volunteer ``out there'' somewhere who has access to
898a platform which the original developer does not; or it could be
899software periodically grabbing new source distributions and turning them
900into built distributions for as many platforms as the software has
901access to. Regardless of the nature of the beast, a packager uses the
902setup script and the \command{bdist} command family to generate built
903distributions.
904
905As a simple example, if I run the following command in the Distutils
906source tree:
907\begin{verbatim}
908python setup.py bdist
909\end{verbatim}
910then the Distutils builds my module distribution (the Distutils itself
911in this case), does a ``fake'' installation (also in the \file{build}
912directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +0000913platform. The default format for built distributions is a ``dumb'' tar
914file on Unix, and an simple executable installer on Windows. (That tar
915file is considered ``dumb'' because it has to be unpacked in a specific
916location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000917
918Thus, the above command on a Unix system creates
919\file{Distutils-0.9.1.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +0000920from the right place installs the Distutils just as though you had
921downloaded the source distribution and run \code{python setup.py
922 install}. (The ``right place'' is either the root of the filesystem or
923Python's \filevar{prefix} directory, depending on the options given to
924the \command{bdist\_dumb} command; the default is to make dumb
925distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +0000926
Greg Wardb6528972000-09-07 02:40:37 +0000927Obviously, for pure Python distributions, this isn't a huge win---but
928for non-pure distributions, which include extensions that would need to
929be compiled, it can mean the difference between someone being able to
930use your extensions or not. And creating ``smart'' built distributions,
931such as an RPM package or an executable installer for Windows, is a big
932win for users even if your distribution doesn't include any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +0000933
Greg Wardb6528972000-09-07 02:40:37 +0000934The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000935similar to the \command{sdist} command, which you can use to select the
936types of built distribution to generate: for example,
Greg Ward46b98e32000-04-14 01:53:36 +0000937\begin{verbatim}
938python setup.py bdist --format=zip
939\end{verbatim}
940would, when run on a Unix system, create
Greg Ward1d8f57a2000-08-05 00:43:11 +0000941\file{Distutils-0.8.\filevar{plat}.zip}---again, this archive would be
942unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +0000943
944The available formats for built distributions are:
945\begin{tableiii}{l|l|c}{code}%
946 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +0000947 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
948 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
949 \lineiii{tar}{tar file (\file{.tar})}{(3)}
950 \lineiii{zip}{zip file (\file{.zip})}{(4)}
951 \lineiii{rpm}{RPM}{(5)}
952 \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
953 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(6)}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000954 %\lineiii{wise}{Wise installer for Windows}{(3)}
Greg Ward46b98e32000-04-14 01:53:36 +0000955\end{tableiii}
956
957\noindent Notes:
958\begin{description}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000959\item[(1)] default on Unix
960\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +0000961\item[(3)] requires external utilities: \program{tar} and possibly one
962 of \program{gzip}, \program{bzip2}, or \program{compress}
963\item[(4)] requires either external \program{zip} utility or
964 \module{zipfile} module (not part of the standard Python library)
965\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
966 better (use \code{rpm --version} to find out which version you have)
967\item[(6)] \XXX{requirements for \command{bdist\_wininst}?}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000968%\item[(3)] not implemented yet
Greg Ward46b98e32000-04-14 01:53:36 +0000969\end{description}
970
971You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +0000972\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000973directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +0000974\command{bdist} ``sub-commands'' actually generate several similar
975formats; for instance, the \command{bdist\_dumb} command generates all
976the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
977\code{zip}), and \command{bdist\_rpm} generates both binary and source
978RPMs. The \command{bdist} sub-commands, and the formats generated by
979each, are:
980\begin{tableii}{l|l}{command}%
981 {Command}{Formats}
982 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
983 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +0000984 \lineii{bdist\_wininst}{wininst}
985 %\lineii{bdist\_wise}{wise}
Greg Ward46b98e32000-04-14 01:53:36 +0000986\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +0000987
Greg Wardb6528972000-09-07 02:40:37 +0000988The following sections give details on the individual \command{bdist\_*}
989commands.
990
991
992\subsection{Creating dumb built distributions}
993\label{creating-dumb}
994
995\XXX{Need to document absolute vs. prefix-relative packages here, but
996 first I have to implement it!}
997
998
999\subsection{Creating RPM packages}
1000\label{creating-rpms}
1001
1002The RPM format is used by many of popular Linux distributions, including
1003Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1004RPM-based Linux distributions) is your usual environment, creating RPM
1005packages for other users of that same distribution is trivial.
1006Depending on the complexity of your module distribution and differences
1007between Linux distributions, you may also be able to create RPMs that
1008work on different RPM-based distributions.
1009
1010The usual way to create an RPM of your module distribution is to run the
1011\command{bdist\_rpm} command:
1012\begin{verbatim}
1013python setup.py bdist_rpm
1014\end{verbatim}
1015or the \command{bdist} command with the \longprogramopt{format} option:
1016\begin{verbatim}
1017python setup.py bdist --formats=rpm
1018\end{verbatim}
1019The former allows you to specify RPM-specific options; the latter allows
1020you to easily specify multiple formats in one run. If you need to do
1021both, you can explicitly specify multiple \command{bdist\_*} commands
1022and their options:
1023\begin{verbatim}
1024python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1025 bdist_wininst --target_version="2.0"
1026\end{verbatim}
1027
1028Creating RPM packages is driven by a \file{.spec} file, much as using
1029the Distutils is driven by the setup script. To make your life easier,
1030the \command{bdist\_rpm} command normally creates a \file{.spec} file
1031based on the information you supply in the setup script, on the command
1032line, and in any Distutils configuration files. Various options and
1033section in the \file{.spec} file are derived from options in the setup
1034script as follows:
1035\begin{tableii}{l|l}{textrm}%
1036 {RPM \file{.spec} file option or section}{Distutils setup script option}
1037 \lineii{Name}{\option{name}}
1038 \lineii{Summary (in preamble)}{\option{description}}
1039 \lineii{Version}{\option{version}}
1040 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1041 \option{maintainer} and \option{maintainer\_email}}
1042 \lineii{Copyright}{\option{licence}}
1043 \lineii{Url}{\option{url}}
1044 \lineii{\%description (section)}{\option{long\_description}}
1045\end{tableii}
1046
1047Additionally, there many options in \file{.spec} files that don't have
1048corresponding options in the setup script. Most of these are handled
1049through options to the \command{bdist\_rpm} command as follows:
1050\begin{tableiii}{l|l|l}{textrm}%
1051 {RPM \file{.spec} file option or section}%
1052 {\command{bdist\_rpm} option}%
1053 {default value}
1054 \lineiii{Release}{\option{release}}{``1''}
1055 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1056 \lineiii{Vendor}{\option{vendor}}{(see above)}
1057 \lineiii{Packager}{packager}{(none)}
1058 \lineiii{Provides}{provides}{(none)}
1059 \lineiii{Requires}{requires}{(none)}
1060 \lineiii{Conflicts}{conflicts}{(none)}
1061 \lineiii{Obsoletes}{obsoletes}{(none)}
1062 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1063 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1064 \lineiii{Icon}{\option{icon}}{(none)}
1065\end{tableiii}
1066Obviously, supplying even a few of these options on the command-line
1067would be tedious and error-prone, so it's usually best to put them in
1068the setup configuration file, \file{setup.cfg}---see
1069section~\ref{setup-config}. If you distribute or package many Python
1070module distributions, you might want to put options that apply to all of
1071them in your personal Distutils configuration file
1072(\file{\textasciitilde/.pydistutils.cfg}).
1073
1074There are three steps to building a binary RPM package, all of which are
1075handled automatically by the Distutils:
1076\begin{enumerate}
1077\item create a \file{.spec} file, which describes the package (analogous
1078 to the Distutils setup script; in fact, much of the information in the
1079 setup script winds up in the \file{.spec} file)
1080\item create the source RPM
1081\item create the ``binary'' RPM (which may or may not contain binary
1082 code, depending on whether your module distribution contains Python
1083 extensions)
1084\end{enumerate}
1085Normally, RPM bundles the last two steps together; when you use the
1086Distutils, all three steps are typically bundled together.
1087
1088If you wish, you can separate these three steps. You can use the
1089\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1090create the \file{.spec} file and exit; in this case, the \file{.spec}
1091file will be written to the ``distribution directory''---normally
1092\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1093option. (Normally, the \file{.spec} file winds up deep in the ``build
1094tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1095
1096\XXX{this isn't implemented yet---is it needed?!}
1097You can also specify a custom \file{.spec} file with the
1098\longprogramopt{spec-file} option; used in conjunctin with
1099\longprogramopt{spec-only}, this gives you an opportunity to customize
1100the \file{.spec} file manually:
1101\begin{verbatim}
1102> python setup.py bdist_rpm --spec-only
1103# ...edit dist/FooBar-1.0.spec
1104> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1105\end{verbatim}
1106(Although a better way to do this is probably to override the standard
1107\command{bdist\_rpm} command with one that writes whatever else you want
1108to the \file{.spec} file; see section~\ref{extending} for information on
1109extending the Distutils.)
1110
1111
1112\subsection{Creating Windows installers}
1113\label{creating-wininst}
1114
1115
1116
Greg Ward16aafcd2000-04-09 04:06:44 +00001117\section{Examples}
Greg Warde78298a2000-04-28 17:12:24 +00001118\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001119
1120
1121\subsection{Pure Python distribution (by module)}
Greg Warde78298a2000-04-28 17:12:24 +00001122\label{pure-mod}
Greg Ward16aafcd2000-04-09 04:06:44 +00001123
1124
1125\subsection{Pure Python distribution (by package)}
Greg Warde78298a2000-04-28 17:12:24 +00001126\label{pure-pkg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001127
1128
1129\subsection{Single extension module}
Greg Warde78298a2000-04-28 17:12:24 +00001130\label{single-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001131
1132
1133\subsection{Multiple extension modules}
Greg Warde78298a2000-04-28 17:12:24 +00001134\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001135
1136
1137\subsection{Putting it all together}
1138
1139
Greg Ward4a9e7222000-04-25 02:57:36 +00001140
1141\section{Extending the Distutils}
Greg Warde78298a2000-04-28 17:12:24 +00001142\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001143
1144
1145\subsection{Extending existing commands}
Greg Warde78298a2000-04-28 17:12:24 +00001146\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001147
1148
1149\subsection{Writing new commands}
Greg Warde78298a2000-04-28 17:12:24 +00001150\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001151
1152
1153
Greg Ward16aafcd2000-04-09 04:06:44 +00001154\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001155\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001156
1157
Greg Wardfacb8db2000-04-09 04:32:40 +00001158\subsection{Building modules: the \protect\command{build} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001159\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001160
Greg Wardfacb8db2000-04-09 04:32:40 +00001161\subsubsection{\protect\command{build}}
Greg Warde78298a2000-04-28 17:12:24 +00001162\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001163
Greg Wardfacb8db2000-04-09 04:32:40 +00001164\subsubsection{\protect\command{build\_py}}
Greg Warde78298a2000-04-28 17:12:24 +00001165\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001166
Greg Wardfacb8db2000-04-09 04:32:40 +00001167\subsubsection{\protect\command{build\_ext}}
Greg Warde78298a2000-04-28 17:12:24 +00001168\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001169
Greg Wardfacb8db2000-04-09 04:32:40 +00001170\subsubsection{\protect\command{build\_clib}}
Greg Warde78298a2000-04-28 17:12:24 +00001171\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001172
1173
Greg Wardfacb8db2000-04-09 04:32:40 +00001174\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001175\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001176
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001177The install command ensures that the build commands have been run and then
1178runs the subcommands \command{install\_lib},
1179\command{install\_data} and
1180\command{install\_scripts}.
1181
1182\subsubsection{\protect\command{install\_lib}}
Greg Ward1365a302000-08-31 14:47:05 +00001183\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001184
1185\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001186\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001187This command installs all data files provided with the distribution.
1188
1189\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001190\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001191This command installs all (Python) scripts in the distribution.
1192
Greg Ward16aafcd2000-04-09 04:06:44 +00001193
Greg Wardfacb8db2000-04-09 04:32:40 +00001194\subsection{Cleaning up: the \protect\command{clean} command}
Greg Warde78298a2000-04-28 17:12:24 +00001195\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001196
1197
Greg Wardfacb8db2000-04-09 04:32:40 +00001198\subsection{Creating a source distribution: the \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001199\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001200
1201
1202\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001203
Greg Ward16aafcd2000-04-09 04:06:44 +00001204The manifest template commands are:
1205\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001206 \lineii{include \var{pat1} \var{pat2} ... }
1207 {include all files matching any of the listed patterns}
1208 \lineii{exclude \var{pat1} \var{pat2} ... }
1209 {exclude all files matching any of the listed patterns}
1210 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1211 {include all files under \var{dir} matching any of the listed patterns}
1212 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1213 {exclude all files under \var{dir} matching any of the listed patterns}
1214 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001215 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001216 any of the listed patterns}
1217 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001218 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001219 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001220 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1221 \lineii{graft \var{dir}}{include all files under \var{dir}}
1222\end{tableii}
1223The patterns here are Unix-style ``glob'' patterns: \code{*} matches any
1224sequence of regular filename characters, \code{?} matches any single
1225regular filename character, and \code{[\var{range}]} matches any of the
1226characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001227\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Greg Ward16aafcd2000-04-09 04:06:44 +00001228platform-specific: on Unix it is anything except slash; on Windows
1229anything except backslash or colon; on Mac OS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001230
Greg Ward16aafcd2000-04-09 04:06:44 +00001231\XXX{Windows and Mac OS support not there yet}
1232
1233
Greg Wardd5767a52000-04-19 22:48:09 +00001234\subsection{Creating a ``built'' distribution: the
1235 \protect\command{bdist} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001236\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001237
1238
Greg Wardfacb8db2000-04-09 04:32:40 +00001239\subsubsection{\protect\command{blib}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001240
Greg Wardfacb8db2000-04-09 04:32:40 +00001241\subsubsection{\protect\command{blib\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001242
Greg Wardfacb8db2000-04-09 04:32:40 +00001243\subsubsection{\protect\command{blib\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001244
Greg Wardfacb8db2000-04-09 04:32:40 +00001245\subsubsection{\protect\command{blib\_wise}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001246
1247
1248
1249
1250
1251
1252
1253
Greg Wardabc52162000-02-26 00:52:48 +00001254\end{document}