blob: 83e49da404bcef6ad2003535d9ec4f00910162a2 [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
Greg Wardabc52162000-02-26 00:52:48 +000013\author{Greg Ward}
Fred Drake17f690f2001-07-14 02:14:42 +000014\authoraddress{Email: \email{gward@python.net}}
Greg Wardabc52162000-02-26 00:52:48 +000015
Greg Warde3cca262000-08-31 16:36:31 +000016\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000017
Greg Wardabc52162000-02-26 00:52:48 +000018\begin{document}
19
Greg Wardfacb8db2000-04-09 04:32:40 +000020\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000021\begin{abstract}
22 \noindent
23 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000024 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000025 how to use the Distutils to make Python modules and extensions easily
26 available to a wider audience with very little overhead for
27 build/release/install mechanics.
28\end{abstract}
29
Fred Drakea09262e2001-03-01 18:35:43 +000030% The ugly "%begin{latexonly}" pseudo-environment supresses the table
31% of contents for HTML generation.
32%
33%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000034\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000035%end{latexonly}
36
Greg Ward16aafcd2000-04-09 04:06:44 +000037
38\section{Introduction}
Greg Warde78298a2000-04-28 17:12:24 +000039\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000040
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000041This document covers using the Distutils to distribute your Python
42modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000043you're looking for information on installing Python modules, you
44should refer to the \citetitle[../inst/inst.html]{Installing Python
45Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000046
47
Greg Wardfacb8db2000-04-09 04:32:40 +000048\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000049\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000050
51Using the Distutils is quite simple, both for module developers and for
52users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000053your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000054well-tested code, of course!) are:
55\begin{itemize}
56\item write a setup script (\file{setup.py} by convention)
57\item (optional) write a setup configuration file
58\item create a source distribution
59\item (optional) create one or more built (binary) distributions
60\end{itemize}
61Each of these tasks is covered in this document.
62
63Not all module developers have access to a multitude of platforms, so
64it's not always feasible to expect them to create a multitude of built
65distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000066\emph{packagers}, will arise to address this need. Packagers will take
67source distributions released by module developers, build them on one or
68more platforms, and release the resulting built distributions. Thus,
69users on the most popular platforms will be able to install most popular
70Python module distributions in the most natural way for their platform,
71without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000072
73
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000074\subsection{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000075\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000076
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000077The setup script is usually quite simple, although since it's written
78in Python, there are no arbitrary limits to what you can do with it,
79though you should be careful about putting arbitrarily expensive
80operations in your setup script. Unlike, say, Autoconf-style configure
81scripts, the setup script may be run multiple times in the course of
82building and installing your module distribution. If you need to
83insert potentially expensive processing steps into the Distutils
84chain, see section~\ref{extending} on extending the Distutils.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000085
86If all you want to do is distribute a module called \module{foo},
87contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000088simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000089
Greg Ward16aafcd2000-04-09 04:06:44 +000090\begin{verbatim}
91from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000092setup(name="foo",
93 version="1.0",
94 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000095\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000096
Greg Ward16aafcd2000-04-09 04:06:44 +000097Some observations:
98\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +000099\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000100 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000101\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000102 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000103 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000104\item modules are specified by module name, not filename (the same will
105 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000106\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000107 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000108 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000109\end{itemize}
110
Greg Ward370248d2000-06-24 01:45:47 +0000111To create a source distribution for this module, you would create a
112setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000113
Greg Ward16aafcd2000-04-09 04:06:44 +0000114\begin{verbatim}
115python setup.py sdist
116\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000117
Fred Drakeeff9a872000-10-26 16:41:03 +0000118which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000119Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000120\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
121\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000122
123If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000124to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
125and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000126
Greg Ward16aafcd2000-04-09 04:06:44 +0000127\begin{verbatim}
128python setup.py install
129\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000130
Greg Ward16aafcd2000-04-09 04:06:44 +0000131which will ultimately copy \file{foo.py} to the appropriate directory
132for third-party modules in their Python installation.
133
134This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000135Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000136user interface, i.e. the setup script. The difference is which
137Distutils \emph{commands} they use: the \command{sdist} command is
138almost exclusively for module developers, while \command{install} is
139more often for installers (although most developers will want to install
140their own code occasionally).
141
Greg Ward16aafcd2000-04-09 04:06:44 +0000142If you want to make things really easy for your users, you can create
143one or more built distributions for them. For instance, if you are
144running on a Windows machine, and want to make things easy for other
145Windows users, you can create an executable installer (the most
146appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000147\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000148
Greg Ward16aafcd2000-04-09 04:06:44 +0000149\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000150python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000151\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000152
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000153will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000154current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000155
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000156Other useful built distribution formats are RPM, implemented by the
157\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000158(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
159(\command{bdist_sdux}). For example, the following command will
160create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000161
Greg Ward1d8f57a2000-08-05 00:43:11 +0000162\begin{verbatim}
163python setup.py bdist_rpm
164\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000165
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000166(The \command{bdist\_rpm} command uses the \command{rpm} executable,
167therefore this has to be run on an RPM-based system such as Red Hat
168Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000169
170You can find out what distribution formats are available at any time by
171running
Fred Drakea09262e2001-03-01 18:35:43 +0000172
Greg Ward1d8f57a2000-08-05 00:43:11 +0000173\begin{verbatim}
174python setup.py bdist --help-formats
175\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000176
177
178\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000179\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000180
181If you're reading this document, you probably have a good idea of what
182modules, extensions, and so forth are. Nevertheless, just to be sure
183that everyone is operating from a common starting point, we offer the
184following glossary of common Python terms:
185\begin{description}
186\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000187 code imported by some other code. Three types of modules concern us
188 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000189
Greg Ward16aafcd2000-04-09 04:06:44 +0000190\item[pure Python module] a module written in Python and contained in a
191 single \file{.py} file (and possibly associated \file{.pyc} and/or
192 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000193
Greg Ward16aafcd2000-04-09 04:06:44 +0000194\item[extension module] a module written in the low-level language of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000195 the Python implementation: C/C++ for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000196 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000197 file, e.g. a shared object (\file{.so}) file for Python extensions on
198 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000199 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drakeeff9a872000-10-26 16:41:03 +0000200 currently, the Distutils only handles C/C++ extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000201
Greg Ward16aafcd2000-04-09 04:06:44 +0000202\item[package] a module that contains other modules; typically contained
203 in a directory in the filesystem and distinguished from other
204 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000205
Greg Ward6153fa12000-05-26 02:24:28 +0000206\item[root package] the root of the hierarchy of packages. (This isn't
207 really a package, since it doesn't have an \file{\_\_init\_\_.py}
208 file. But we have to call it something.) The vast majority of the
209 standard library is in the root package, as are many small, standalone
210 third-party modules that don't belong to a larger module collection.
211 Unlike regular packages, modules in the root package can be found in
212 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000213 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000214\end{description}
215
216
217\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000218\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000219
220The following terms apply more specifically to the domain of
221distributing Python modules using the Distutils:
222\begin{description}
223\item[module distribution] a collection of Python modules distributed
224 together as a single downloadable resource and meant to be installed
225 \emph{en masse}. Examples of some well-known module distributions are
226 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000227 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000228 is already taken in the Python context: a single module distribution
229 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000230
Greg Ward16aafcd2000-04-09 04:06:44 +0000231\item[pure module distribution] a module distribution that contains only
232 pure Python modules and packages. Sometimes referred to as a ``pure
233 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000234
Greg Ward16aafcd2000-04-09 04:06:44 +0000235\item[non-pure module distribution] a module distribution that contains
236 at least one extension module. Sometimes referred to as a ``non-pure
237 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000238
Greg Ward16aafcd2000-04-09 04:06:44 +0000239\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000240 source distribution); the directory where \file{setup.py} exists. Generally
241 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000242\end{description}
243
244
245\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000246\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000247
248The setup script is the centre of all activity in building,
249distributing, and installing modules using the Distutils. The main
250purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000251the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000252do the right thing. As we saw in section~\ref{simple-example} above,
253the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000254most information supplied to the Distutils by the module developer is
255supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000256
257Here's a slightly more involved example, which we'll follow for the next
258couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000259although the Distutils are included with Python 1.6 and later, they also
260have an independent existence so that Python 1.5.2 users can use them to
261install other module distributions. The Distutils' own setup script,
262shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000263
264\begin{verbatim}
265#!/usr/bin/env python
266
267from distutils.core import setup
268
Fred Drakea09262e2001-03-01 18:35:43 +0000269setup(name="Distutils",
270 version="1.0",
271 description="Python Distribution Utilities",
272 author="Greg Ward",
273 author_email="gward@python.net",
274 url="http://www.python.org/sigs/distutils-sig/",
275 packages=['distutils', 'distutils.command'],
276 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000277\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000278
Greg Ward16aafcd2000-04-09 04:06:44 +0000279There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000280distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000281metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000282rather than by module. This is important since the Distutils consist of
283a couple of dozen modules split into (so far) two packages; an explicit
284list of every module would be tedious to generate and difficult to
285maintain.
286
Greg Ward46b98e32000-04-14 01:53:36 +0000287Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000288script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000289slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000290platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000291current platform before actually using the pathname. This makes your
292setup script portable across operating systems, which of course is one
293of the major goals of the Distutils. In this spirit, all pathnames in
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000294this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000295mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000296path, the opposite of the MacOS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000297
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000298This, of course, only applies to pathnames given to Distutils
299functions. If you, for example, use standard python functions such as
300\function{glob.glob} or \function{os.listdir} to specify files, you
301should be careful to write portable code instead of hardcoding path
302separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000303
Thomas Heller5f52f722001-02-19 17:48:03 +0000304\begin{verbatim}
305 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
306 os.listdir(os.path.join('mydir', 'subdir'))
307\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000308
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000309
Greg Ward2afffd42000-08-06 20:37:24 +0000310\subsection{Listing whole packages}
311\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000312
313The \option{packages} option tells the Distutils to process (build,
314distribute, install, etc.) all pure Python modules found in each package
315mentioned in the \option{packages} list. In order to do this, of
316course, there has to be a correspondence between package names and
317directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000318obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000319\file{distutils} relative to the distribution root. Thus, when you say
320\code{packages = ['foo']} in your setup script, you are promising that
321the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
322be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000323the directory where your setup script lives. If you break this
324promise, the Distutils will issue a warning but still process the broken
325package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000326
327If you use a different convention to lay out your source directory,
328that's no problem: you just have to supply the \option{package\_dir}
329option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000330you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000331``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000332\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
333and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000334
Greg Ward16aafcd2000-04-09 04:06:44 +0000335\begin{verbatim}
336package_dir = {'': 'lib'}
337\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000338
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000339in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000340and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000341directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000342you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000343\file{lib/foo/\_\_init\_\_.py} exists.
344
Greg Ward1ecc2512000-04-19 22:36:24 +0000345Another possible convention is to put the \module{foo} package right in
346\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000347would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000348
Greg Ward16aafcd2000-04-09 04:06:44 +0000349\begin{verbatim}
350package_dir = {'foo': 'lib'}
351\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000352
Greg Ward59d382e2000-05-26 01:04:47 +0000353A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
354dictionary implicitly applies to all packages below \var{package}, so
355the \module{foo.bar} case is automatically handled here. In this
356example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
357to look for \file{lib/\_\_init\_\_.py} and
358\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
359\option{package\_dir} applies recursively, you must explicitly list all
360packages in \option{packages}: the Distutils will \emph{not} recursively
361scan your source tree looking for any directory with an
362\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000363
364
365\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000366\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000367
368For a small module distribution, you might prefer to list all modules
369rather than listing packages---especially the case of a single module
370that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000371simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000372slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000373
Greg Ward16aafcd2000-04-09 04:06:44 +0000374\begin{verbatim}
375py_modules = ['mod1', 'pkg.mod2']
376\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000377
Greg Ward16aafcd2000-04-09 04:06:44 +0000378This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000379other in the \module{pkg} package. Again, the default package/directory
380layout implies that these two modules can be found in \file{mod1.py} and
381\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000382And again, you can override the package/directory correspondence using
383the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000384
385
386\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000387\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000388
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000389% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000390Just as writing Python extension modules is a bit more complicated than
391writing pure Python modules, describing them to the Distutils is a bit
392more complicated. Unlike pure modules, it's not enough just to list
393modules or packages and expect the Distutils to go out and find the
394right files; you have to specify the extension name, source file(s), and
395any compile/link requirements (include directories, libraries to link
396with, etc.).
397
398All of this is done through another keyword argument to
399\function{setup()}, the \option{extensions} option. \option{extensions}
400is just a list of \class{Extension} instances, each of which describes a
401single extension module. Suppose your distribution includes a single
402extension, called \module{foo} and implemented by \file{foo.c}. If no
403additional instructions to the compiler/linker are needed, describing
404this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000405
Greg Ward2afffd42000-08-06 20:37:24 +0000406\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000407uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000408\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000409
Greg Ward2afffd42000-08-06 20:37:24 +0000410The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000411\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000412script for a module distribution that contains only this one extension
413and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000414
Greg Ward2afffd42000-08-06 20:37:24 +0000415\begin{verbatim}
416from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000417setup(name="foo", version="1.0",
418 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000419\end{verbatim}
420
421The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000422machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000423great deal of flexibility in describing Python extensions, which is
424explained in the following sections.
425
426
427\subsubsection{Extension names and packages}
428
429The first argument to the \class{Extension} constructor is always the
430name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000431
Greg Ward2afffd42000-08-06 20:37:24 +0000432\begin{verbatim}
433Extension("foo", ["src/foo1.c", "src/foo2.c"])
434\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000435
Greg Ward2afffd42000-08-06 20:37:24 +0000436describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000437
Greg Ward2afffd42000-08-06 20:37:24 +0000438\begin{verbatim}
439Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
440\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000441
Greg Ward2afffd42000-08-06 20:37:24 +0000442describes the same extension in the \module{pkg} package. The source
443files and resulting object code are identical in both cases; the only
444difference is where in the filesystem (and therefore where in Python's
445namespace hierarchy) the resulting extension lives.
446
447If you have a number of extensions all in the same package (or all under
448the same base package), use the \option{ext\_package} keyword argument
449to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000450
Greg Ward2afffd42000-08-06 20:37:24 +0000451\begin{verbatim}
452setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000453 ext_package="pkg",
454 ext_modules=[Extension("foo", ["foo.c"]),
455 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000456 )
457\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000458
Greg Ward2afffd42000-08-06 20:37:24 +0000459will compile \file{foo.c} to the extension \module{pkg.foo}, and
460\file{bar.c} to \module{pkg.subpkg.bar}.
461
462
463\subsubsection{Extension source files}
464
465The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000466source files. Since the Distutils currently only support C, \Cpp, and
467Objective-C extensions, these are normally C/\Cpp/Objective-C source
468files. (Be sure to use appropriate extensions to distinguish \Cpp\
469source files: \file{.cc} and \file{.cpp} seem to be recognized by both
470\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000471
472However, you can also include SWIG interface (\file{.i}) files in the
473list; the \command{build\_ext} command knows how to deal with SWIG
474extensions: it will run SWIG on the interface file and compile the
475resulting C/C++ file into your extension.
476
477\XXX{SWIG support is rough around the edges and largely untested;
478 especially SWIG support of C++ extensions! Explain in more detail
479 here when the interface firms up.}
480
481On some platforms, you can include non-source files that are processed
482by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000483means Windows message text (\file{.mc}) files and resource definition
484(\file{.rc}) files for Visual C++. These will be compiled to binary resource
485(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000486
487
488\subsubsection{Preprocessor options}
489
490Three optional arguments to \class{Extension} will help if you need to
491specify include directories to search or preprocessor macros to
492define/undefine: \code{include\_dirs}, \code{define\_macros}, and
493\code{undef\_macros}.
494
495For example, if your extension requires header files in the
496\file{include} directory under your distribution root, use the
497\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000498
Greg Ward2afffd42000-08-06 20:37:24 +0000499\begin{verbatim}
500Extension("foo", ["foo.c"], include_dirs=["include"])
501\end{verbatim}
502
503You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000504extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000505\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000506
Greg Ward2afffd42000-08-06 20:37:24 +0000507\begin{verbatim}
508Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
509\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000510
Greg Ward2afffd42000-08-06 20:37:24 +0000511You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000512distribute your code: it's probably better to write C code like
513\begin{verbatim}
514#include <X11/Xlib.h>
515\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000516
517If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000518you can take advantage of the fact that header files are installed in a
519consistent way by the Distutils \command{install\_header} command. For
520example, the Numerical Python header files are installed (on a standard
521Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
522(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000523installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000524directory---\file{/usr/local/include/python1.5} in this case---is always
525included in the search path when building Python extensions, the best
526approach is to write C code like
527\begin{verbatim}
528#include <Numerical/arrayobject.h>
529\end{verbatim}
530If you must put the \file{Numerical} include directory right into your
531header search path, though, you can find that directory using the
532Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000533
Greg Ward2afffd42000-08-06 20:37:24 +0000534\begin{verbatim}
535from distutils.sysconfig import get_python_inc
536incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
537setup(...,
538 Extension(..., include_dirs=[incdir]))
539\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000540
Greg Ward2afffd42000-08-06 20:37:24 +0000541Even though this is quite portable---it will work on any Python
542installation, regardless of platform---it's probably easier to just
543write your C code in the sensible way.
544
545You can define and undefine pre-processor macros with the
546\code{define\_macros} and \code{undef\_macros} options.
547\code{define\_macros} takes a list of \code{(name, value)} tuples, where
548\code{name} is the name of the macro to define (a string) and
549\code{value} is its value: either a string or \code{None}. (Defining a
550macro \code{FOO} to \code{None} is the equivalent of a bare
551\code{\#define FOO} in your C source: with most compilers, this sets
552\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
553a list of macros to undefine.
554
555For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000556
Greg Ward2afffd42000-08-06 20:37:24 +0000557\begin{verbatim}
558Extension(...,
559 define_macros=[('NDEBUG', '1')],
560 ('HAVE_STRFTIME', None),
561 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
562\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000563
Greg Ward2afffd42000-08-06 20:37:24 +0000564is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000565
Greg Ward2afffd42000-08-06 20:37:24 +0000566\begin{verbatim}
567#define NDEBUG 1
568#define HAVE_STRFTIME
569#undef HAVE_FOO
570#undef HAVE_BAR
571\end{verbatim}
572
573
574\subsubsection{Library options}
575
576You can also specify the libraries to link against when building your
577extension, and the directories to search for those libraries. The
578\code{libraries} option is a list of libraries to link against,
579\code{library\_dirs} is a list of directories to search for libraries at
580link-time, and \code{runtime\_library\_dirs} is a list of directories to
581search for shared (dynamically loaded) libraries at run-time.
582
583For example, if you need to link against libraries known to be in the
584standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000585
Greg Ward2afffd42000-08-06 20:37:24 +0000586\begin{verbatim}
587Extension(...,
588 libraries=["gdbm", "readline"])
589\end{verbatim}
590
591If you need to link with libraries in a non-standard location, you'll
592have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000593
Greg Ward2afffd42000-08-06 20:37:24 +0000594\begin{verbatim}
595Extension(...,
596 library_dirs=["/usr/X11R6/lib"],
597 libraries=["X11", "Xt"])
598\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000599
Greg Ward2afffd42000-08-06 20:37:24 +0000600(Again, this sort of non-portable construct should be avoided if you
601intend to distribute your code.)
602
Thomas Heller5f52f722001-02-19 17:48:03 +0000603\XXX{Should mention clib libraries here or somewhere else!}
604
605\subsubsection{Other options}
606
607There are still some other options which can be used to handle special
608cases.
609
610The \option{extra\_objects} option is a list of object files to be passed
611to the linker. These files must not have extensions, as the default
612extension for the compiler is used.
613
614\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000615to specify additional command line options for the respective compiler and
616linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000617
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000618\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000619of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000620is not needed when building compiled extensions: Distutils
621will automatically add \code{initmodule}
622to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000623
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000624\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000625So far we have been dealing with pure and non-pure Python modules,
626which are usually not run by themselves but imported by scripts.
627
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000628Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000629started from the command line. Scripts don't require Distutils to do
630anything very complicated. The only clever feature is that if the
631first line of the script starts with \code{\#!} and contains the word
632``python'', the Distutils will adjust the first line to refer to the
633current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000634
635The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000636in this way. From the PyXML setup script:
637
638\begin{verbatim}
639setup (...
640 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
641 )
642\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000643
644
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000645\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000646
Thomas Heller5f52f722001-02-19 17:48:03 +0000647The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000648files needed by the module distribution: configuration files, message
649catalogs, data files, anything which doesn't fit in the previous
650categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000651
Fred Drake632bda32002-03-08 22:02:06 +0000652\option{data\_files} specifies a sequence of (\var{directory},
653\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000654
Thomas Heller5f52f722001-02-19 17:48:03 +0000655\begin{verbatim}
656setup(...
657 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000658 ('config', ['cfg/data.cfg']),
659 ('/etc/init.d', ['init-script'])]
660 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000661\end{verbatim}
662
663Note that you can specify the directory names where the data files
664will be installed, but you cannot rename the data files themselves.
665
Fred Drake632bda32002-03-08 22:02:06 +0000666Each (\var{directory}, \var{files}) pair in the sequence specifies the
667installation directory and the files to install there. If
668\var{directory} is a relative path, it is interpreted relative to the
669installation prefix (Python's \code{sys.prefix} for pure-Python
670packages, \code{sys.exec_prefix} for packages that contain extension
671modules). Each file name in \var{files} is interpreted relative to
672the \file{setup.py} script at the top of the package source
673distribution. No directory information from \var{files} is used to
674determine the final location of the installed file; only the name of
675the file is used.
676
Thomas Heller5f52f722001-02-19 17:48:03 +0000677You can specify the \option{data\_files} options as a simple sequence
678of files without specifying a target directory, but this is not recommended,
679and the \command{install} command will print a warning in this case.
680To install data files directly in the target directory, an empty
681string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000682
683
684\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000685\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000686
Greg Ward16aafcd2000-04-09 04:06:44 +0000687Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000688distribution \emph{a priori}: you may need to get some information from
689the user, or from the user's system, in order to proceed. As long as
690that information is fairly simple---a list of directories to search for
691C header files or libraries, for example---then providing a
692configuration file, \file{setup.cfg}, for users to edit is a cheap and
693easy way to solicit it. Configuration files also let you provide
694default values for any command option, which the installer can then
695override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000696
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000697% (If you have more advanced needs, such as determining which extensions
698% to build based on what capabilities are present on the target system,
699% then you need the Distutils ``auto-configuration'' facility. This
700% started to appear in Distutils 0.9 but, as of this writing, isn't mature
701% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000702
Greg Ward47f99a62000-09-04 20:07:15 +0000703The setup configuration file is a useful middle-ground between the setup
704script---which, ideally, would be opaque to installers\footnote{This
705 ideal probably won't be achieved until auto-configuration is fully
706 supported by the Distutils.}---and the command-line to the setup
707script, which is outside of your control and entirely up to the
708installer. In fact, \file{setup.cfg} (and any other Distutils
709configuration files present on the target system) are processed after
710the contents of the setup script, but before the command-line. This has
711several useful consequences:
712\begin{itemize}
713\item installers can override some of what you put in \file{setup.py} by
714 editing \file{setup.cfg}
715\item you can provide non-standard defaults for options that are not
716 easily set in \file{setup.py}
717\item installers can override anything in \file{setup.cfg} using the
718 command-line options to \file{setup.py}
719\end{itemize}
720
721The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000722
Greg Ward47f99a62000-09-04 20:07:15 +0000723\begin{verbatim}
724[command]
725option=value
726...
727\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000728
Greg Ward47f99a62000-09-04 20:07:15 +0000729where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000730\command{build\_py}, \command{install}), and \var{option} is one of
731the options that command supports. Any number of options can be
732supplied for each command, and any number of command sections can be
733included in the file. Blank lines are ignored, as are comments, which
734run from a \character{\#} character until the end of the line. Long
735option values can be split across multiple lines simply by indenting
736the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000737
738You can find out the list of options supported by a particular command
739with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000740
Greg Ward47f99a62000-09-04 20:07:15 +0000741\begin{verbatim}
742> python setup.py --help build_ext
743[...]
744Options for 'build_ext' command:
745 --build-lib (-b) directory for compiled extension modules
746 --build-temp (-t) directory for temporary files (build by-products)
747 --inplace (-i) ignore build-lib and put compiled extensions into the
748 source directory alongside your pure Python modules
749 --include-dirs (-I) list of directories to search for header files
750 --define (-D) C preprocessor macros to define
751 --undef (-U) C preprocessor macros to undefine
752[...]
753\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000754
Greg Ward47f99a62000-09-04 20:07:15 +0000755Note that an option spelled \longprogramopt{foo-bar} on the command-line
756is spelled \option{foo\_bar} in configuration files.
757
758For example, say you want your extensions to be built
759``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000760want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000761in the same source directory as your pure Python modules
762\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
763\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000764
Greg Ward47f99a62000-09-04 20:07:15 +0000765\begin{verbatim}
766python setup.py build_ext --inplace
767\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000768
Greg Ward47f99a62000-09-04 20:07:15 +0000769But this requires that you always specify the \command{build\_ext}
770command explicitly, and remember to provide \longprogramopt{inplace}.
771An easier way is to ``set and forget'' this option, by encoding it in
772\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000773
Greg Ward47f99a62000-09-04 20:07:15 +0000774\begin{verbatim}
775[build_ext]
776inplace=1
777\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000778
Greg Ward47f99a62000-09-04 20:07:15 +0000779This will affect all builds of this module distribution, whether or not
780you explcitly specify \command{build\_ext}. If you include
781\file{setup.cfg} in your source distribution, it will also affect
782end-user builds---which is probably a bad idea for this option, since
783always building extensions in-place would break installation of the
784module distribution. In certain peculiar cases, though, modules are
785built right in their installation directory, so this is conceivably a
786useful ability. (Distributing extensions that expect to be built in
787their installation directory is almost always a bad idea, though.)
788
789Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000790change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000791everything required to generate a ``spec'' file for creating an RPM
792distribution. Some of this information comes from the setup script, and
793some is automatically generated by the Distutils (such as the list of
794files installed). But some of it has to be supplied as options to
795\command{bdist\_rpm}, which would be very tedious to do on the
796command-line for every run. Hence, here is a snippet from the
797Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000798
Greg Ward47f99a62000-09-04 20:07:15 +0000799\begin{verbatim}
800[bdist_rpm]
801release = 1
802packager = Greg Ward <gward@python.net>
803doc_files = CHANGES.txt
804 README.txt
805 USAGE.txt
806 doc/
807 examples/
808\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000809
Greg Ward47f99a62000-09-04 20:07:15 +0000810Note that the \option{doc\_files} option is simply a
811whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000812
813
Fred Drakea09262e2001-03-01 18:35:43 +0000814\begin{seealso}
815 \seetitle[../inst/config-syntax.html]{Installing Python
816 Modules}{More information on the configuration files is
817 available in the manual for system administrators.}
818\end{seealso}
819
820
Greg Ward16aafcd2000-04-09 04:06:44 +0000821\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000822\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000823
Greg Warde78298a2000-04-28 17:12:24 +0000824As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000825\command{sdist} command to create a source distribution. In the
826simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000827
Greg Ward16aafcd2000-04-09 04:06:44 +0000828\begin{verbatim}
829python setup.py sdist
830\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000831
Greg Ward19c67f82000-06-24 01:33:16 +0000832(assuming you haven't specified any \command{sdist} options in the setup
833script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000834default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000835tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
836\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000837
Greg Wardd5767a52000-04-19 22:48:09 +0000838You can specify as many formats as you like using the
839\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000840
Greg Ward16aafcd2000-04-09 04:06:44 +0000841\begin{verbatim}
842python setup.py sdist --formats=gztar,zip
843\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000844
Greg Ward16aafcd2000-04-09 04:06:44 +0000845to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000846\begin{tableiii}{l|l|c}{code}%
847 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000848 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
849 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000850 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000851 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000852 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000853\end{tableiii}
854
855\noindent Notes:
856\begin{description}
857\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000858\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000859\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000860 \module{zipfile} module (part of the standard Python library since
861 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000862\item[(4)] requires external utilities: \program{tar} and possibly one
863 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000864\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000865
866
Greg Ward54589d42000-09-06 01:37:35 +0000867
868\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000869\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000870
Greg Ward54589d42000-09-06 01:37:35 +0000871If you don't supply an explicit list of files (or instructions on how to
872generate one), the \command{sdist} command puts a minimal default set
873into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000874\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000875\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000876 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000877\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000878 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000879 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000880\item anything that looks like a test script: \file{test/test*.py}
881 (currently, the Distutils don't do anything with test scripts except
882 include them in source distributions, but in the future there will be
883 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000884\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
885 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000886\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000887
Greg Ward16aafcd2000-04-09 04:06:44 +0000888Sometimes this is enough, but usually you will want to specify
889additional files to distribute. The typical way to do this is to write
890a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000891manifest template is just a list of instructions for how to generate
892your manifest file, \file{MANIFEST}, which is the exact list of files to
893include in your source distribution. The \command{sdist} command
894processes this template and generates a manifest based on its
895instructions and what it finds in the filesystem.
896
897If you prefer to roll your own manifest file, the format is simple: one
898filename per line, regular files (or symlinks to them) only. If you do
899supply your own \file{MANIFEST}, you must specify everything: the
900default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000901
902The manifest template has one command per line, where each command
903specifies a set of files to include or exclude from the source
904distribution. For an example, again we turn to the Distutils' own
905manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +0000906
Greg Ward16aafcd2000-04-09 04:06:44 +0000907\begin{verbatim}
908include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000909recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000910prune examples/sample?/build
911\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000912
Greg Ward16aafcd2000-04-09 04:06:44 +0000913The meanings should be fairly clear: include all files in the
914distribution root matching \code{*.txt}, all files anywhere under the
915\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000916exclude all directories matching \code{examples/sample?/build}. All of
917this is done \emph{after} the standard include set, so you can exclude
918files from the standard set with explicit instructions in the manifest
919template. (Or, you can use the \longprogramopt{no-defaults} option to
920disable the standard set entirely.) There are several other commands
921available in the manifest template mini-language; see
922section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000923
Greg Ward54589d42000-09-06 01:37:35 +0000924The order of commands in the manifest template matters: initially, we
925have the list of default files as described above, and each command in
926the template adds to or removes from that list of files. Once we have
927fully processed the manifest template, we remove files that should not
928be included in the source distribution:
929\begin{itemize}
930\item all files in the Distutils ``build'' tree (default \file{build/})
931\item all files in directories named \file{RCS} or \file{CVS}
932\end{itemize}
933Now we have our complete list of files, which is written to the manifest
934for future reference, and then used to build the source distribution
935archive(s).
936
937You can disable the default set of included files with the
938\longprogramopt{no-defaults} option, and you can disable the standard
939exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000940
Greg Ward46b98e32000-04-14 01:53:36 +0000941Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000942\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000943Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000944\begin{enumerate}
945\item include all Python source files in the \file{distutils} and
946 \file{distutils/command} subdirectories (because packages
947 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000948 \option{packages} option in the setup script---see
949 section~\ref{setup-script})
950\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
951 (standard files)
952\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000953\item include \file{*.txt} in the distribution root (this will find
954 \file{README.txt} a second time, but such redundancies are weeded out
955 later)
Greg Ward54589d42000-09-06 01:37:35 +0000956\item include anything matching \file{*.txt} or \file{*.py} in the
957 sub-tree under \file{examples},
958\item exclude all files in the sub-trees starting at directories
959 matching \file{examples/sample?/build}---this may exclude files
960 included by the previous two steps, so it's important that the
961 \code{prune} command in the manifest template comes after the
962 \code{recursive-include} command
963\item exclude the entire \file{build} tree, and any \file{RCS} or
964 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000965\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000966Just like in the setup script, file and directory names in the manifest
967template should always be slash-separated; the Distutils will take care
968of converting them to the standard representation on your platform.
969That way, the manifest template is portable across operating systems.
970
Greg Ward16aafcd2000-04-09 04:06:44 +0000971
972\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000973\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000974
975The normal course of operations for the \command{sdist} command is as
976follows:
977\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000978\item if the manifest file, \file{MANIFEST} doesn't exist, read
979 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000980\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000981 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +0000982\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
983 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
984 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000985\item use the list of files now in \file{MANIFEST} (either just
986 generated or read in) to create the source distribution archive(s)
987\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000988There are a couple of options that modify this behaviour. First, use
989the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000990disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +0000991
Greg Ward54589d42000-09-06 01:37:35 +0000992Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000993example, if you have added or removed files or directories that match an
994existing pattern in the manifest template, you should regenerate the
995manifest:
Fred Drakea09262e2001-03-01 18:35:43 +0000996
Greg Ward16aafcd2000-04-09 04:06:44 +0000997\begin{verbatim}
998python setup.py sdist --force-manifest
999\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001000
1001Or, you might just want to (re)generate the manifest, but not create a
1002source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001003
Greg Ward16aafcd2000-04-09 04:06:44 +00001004\begin{verbatim}
1005python setup.py sdist --manifest-only
1006\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001007
Greg Ward54589d42000-09-06 01:37:35 +00001008\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1009\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1010\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001011
1012
1013\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001014\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001015
Greg Ward46b98e32000-04-14 01:53:36 +00001016A ``built distribution'' is what you're probably used to thinking of
1017either as a ``binary package'' or an ``installer'' (depending on your
1018background). It's not necessarily binary, though, because it might
1019contain only Python source code and/or byte-code; and we don't call it a
1020package, because that word is already spoken for in Python. (And
1021``installer'' is a term specific to the Windows world. \XXX{do Mac
1022 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001023
Greg Ward46b98e32000-04-14 01:53:36 +00001024A built distribution is how you make life as easy as possible for
1025installers of your module distribution: for users of RPM-based Linux
1026systems, it's a binary RPM; for Windows users, it's an executable
1027installer; for Debian-based Linux users, it's a Debian package; and so
1028forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001029distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001030designed to enable module developers to concentrate on their
1031specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001032intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001033distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001034are packagers.
1035
1036Of course, the module developer could be his own packager; or the
1037packager could be a volunteer ``out there'' somewhere who has access to
1038a platform which the original developer does not; or it could be
1039software periodically grabbing new source distributions and turning them
1040into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001041access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001042setup script and the \command{bdist} command family to generate built
1043distributions.
1044
1045As a simple example, if I run the following command in the Distutils
1046source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001047
Greg Ward46b98e32000-04-14 01:53:36 +00001048\begin{verbatim}
1049python setup.py bdist
1050\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001051
Greg Ward46b98e32000-04-14 01:53:36 +00001052then the Distutils builds my module distribution (the Distutils itself
1053in this case), does a ``fake'' installation (also in the \file{build}
1054directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001055platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001056file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001057file is considered ``dumb'' because it has to be unpacked in a specific
1058location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001059
Fred Drakeeff9a872000-10-26 16:41:03 +00001060Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001061\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001062from the right place installs the Distutils just as though you had
1063downloaded the source distribution and run \code{python setup.py
1064 install}. (The ``right place'' is either the root of the filesystem or
1065Python's \filevar{prefix} directory, depending on the options given to
1066the \command{bdist\_dumb} command; the default is to make dumb
1067distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001068
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001069Obviously, for pure Python distributions, this isn't any simpler than
1070just running \code{python setup.py install}---but for non-pure
1071distributions, which include extensions that would need to be
1072compiled, it can mean the difference between someone being able to use
1073your extensions or not. And creating ``smart'' built distributions,
1074such as an RPM package or an executable installer for Windows, is far
1075more convenient for users even if your distribution doesn't include
1076any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001077
Greg Wardb6528972000-09-07 02:40:37 +00001078The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001079similar to the \command{sdist} command, which you can use to select the
1080types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001081
Greg Ward46b98e32000-04-14 01:53:36 +00001082\begin{verbatim}
1083python setup.py bdist --format=zip
1084\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001085
Fred Drakeeff9a872000-10-26 16:41:03 +00001086would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001087\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001088unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001089
1090The available formats for built distributions are:
1091\begin{tableiii}{l|l|c}{code}%
1092 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001093 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1094 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1095 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1096 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1097 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001098 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1099 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1100 \lineiii{rpm}{RPM}{(5)}
1101% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001102 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001103\end{tableiii}
1104
1105\noindent Notes:
1106\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001107\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001108\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001109\item[(3)] requires external utilities: \program{tar} and possibly one
1110 of \program{gzip}, \program{bzip2}, or \program{compress}
1111\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001112 \module{zipfile} module (part of the standard Python library since
1113 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001114\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1115 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001116\end{description}
1117
1118You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001119\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001120directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001121\command{bdist} ``sub-commands'' actually generate several similar
1122formats; for instance, the \command{bdist\_dumb} command generates all
1123the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1124\code{zip}), and \command{bdist\_rpm} generates both binary and source
1125RPMs. The \command{bdist} sub-commands, and the formats generated by
1126each, are:
1127\begin{tableii}{l|l}{command}%
1128 {Command}{Formats}
1129 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1130 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001131 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001132\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001133
Greg Wardb6528972000-09-07 02:40:37 +00001134The following sections give details on the individual \command{bdist\_*}
1135commands.
1136
1137
1138\subsection{Creating dumb built distributions}
1139\label{creating-dumb}
1140
1141\XXX{Need to document absolute vs. prefix-relative packages here, but
1142 first I have to implement it!}
1143
1144
1145\subsection{Creating RPM packages}
1146\label{creating-rpms}
1147
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001148The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001149Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1150RPM-based Linux distributions) is your usual environment, creating RPM
1151packages for other users of that same distribution is trivial.
1152Depending on the complexity of your module distribution and differences
1153between Linux distributions, you may also be able to create RPMs that
1154work on different RPM-based distributions.
1155
1156The usual way to create an RPM of your module distribution is to run the
1157\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001158
Greg Wardb6528972000-09-07 02:40:37 +00001159\begin{verbatim}
1160python setup.py bdist_rpm
1161\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001162
Greg Wardb6528972000-09-07 02:40:37 +00001163or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001164
Greg Wardb6528972000-09-07 02:40:37 +00001165\begin{verbatim}
1166python setup.py bdist --formats=rpm
1167\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001168
Greg Wardb6528972000-09-07 02:40:37 +00001169The former allows you to specify RPM-specific options; the latter allows
1170you to easily specify multiple formats in one run. If you need to do
1171both, you can explicitly specify multiple \command{bdist\_*} commands
1172and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001173
Greg Wardb6528972000-09-07 02:40:37 +00001174\begin{verbatim}
1175python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1176 bdist_wininst --target_version="2.0"
1177\end{verbatim}
1178
1179Creating RPM packages is driven by a \file{.spec} file, much as using
1180the Distutils is driven by the setup script. To make your life easier,
1181the \command{bdist\_rpm} command normally creates a \file{.spec} file
1182based on the information you supply in the setup script, on the command
1183line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001184sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001185script as follows:
1186\begin{tableii}{l|l}{textrm}%
1187 {RPM \file{.spec} file option or section}{Distutils setup script option}
1188 \lineii{Name}{\option{name}}
1189 \lineii{Summary (in preamble)}{\option{description}}
1190 \lineii{Version}{\option{version}}
1191 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1192 \option{maintainer} and \option{maintainer\_email}}
1193 \lineii{Copyright}{\option{licence}}
1194 \lineii{Url}{\option{url}}
1195 \lineii{\%description (section)}{\option{long\_description}}
1196\end{tableii}
1197
1198Additionally, there many options in \file{.spec} files that don't have
1199corresponding options in the setup script. Most of these are handled
1200through options to the \command{bdist\_rpm} command as follows:
1201\begin{tableiii}{l|l|l}{textrm}%
1202 {RPM \file{.spec} file option or section}%
1203 {\command{bdist\_rpm} option}%
1204 {default value}
1205 \lineiii{Release}{\option{release}}{``1''}
1206 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1207 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001208 \lineiii{Packager}{\option{packager}}{(none)}
1209 \lineiii{Provides}{\option{provides}}{(none)}
1210 \lineiii{Requires}{\option{requires}}{(none)}
1211 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1212 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001213 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1214 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1215 \lineiii{Icon}{\option{icon}}{(none)}
1216\end{tableiii}
1217Obviously, supplying even a few of these options on the command-line
1218would be tedious and error-prone, so it's usually best to put them in
1219the setup configuration file, \file{setup.cfg}---see
1220section~\ref{setup-config}. If you distribute or package many Python
1221module distributions, you might want to put options that apply to all of
1222them in your personal Distutils configuration file
1223(\file{\textasciitilde/.pydistutils.cfg}).
1224
1225There are three steps to building a binary RPM package, all of which are
1226handled automatically by the Distutils:
1227\begin{enumerate}
1228\item create a \file{.spec} file, which describes the package (analogous
1229 to the Distutils setup script; in fact, much of the information in the
1230 setup script winds up in the \file{.spec} file)
1231\item create the source RPM
1232\item create the ``binary'' RPM (which may or may not contain binary
1233 code, depending on whether your module distribution contains Python
1234 extensions)
1235\end{enumerate}
1236Normally, RPM bundles the last two steps together; when you use the
1237Distutils, all three steps are typically bundled together.
1238
1239If you wish, you can separate these three steps. You can use the
1240\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1241create the \file{.spec} file and exit; in this case, the \file{.spec}
1242file will be written to the ``distribution directory''---normally
1243\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1244option. (Normally, the \file{.spec} file winds up deep in the ``build
1245tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1246
1247\XXX{this isn't implemented yet---is it needed?!}
1248You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001249\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001250\longprogramopt{spec-only}, this gives you an opportunity to customize
1251the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001252
Greg Wardb6528972000-09-07 02:40:37 +00001253\begin{verbatim}
1254> python setup.py bdist_rpm --spec-only
1255# ...edit dist/FooBar-1.0.spec
1256> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1257\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001258
Greg Wardb6528972000-09-07 02:40:37 +00001259(Although a better way to do this is probably to override the standard
1260\command{bdist\_rpm} command with one that writes whatever else you want
1261to the \file{.spec} file; see section~\ref{extending} for information on
1262extending the Distutils.)
1263
1264
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001265\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001266\label{creating-wininst}
1267
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001268Executable installers are the natural format for binary
Fred Drake17f690f2001-07-14 02:14:42 +00001269distributions on Windows. They display a nice graphical user interface,
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001270display some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001271from the metadata in the setup script, let the user select a few
Thomas Heller5f52f722001-02-19 17:48:03 +00001272(currently maybe too few) options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001273
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001274Since the metadata is taken from the setup script, creating
Thomas Heller5f52f722001-02-19 17:48:03 +00001275Windows installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001276
Thomas Heller5f52f722001-02-19 17:48:03 +00001277\begin{verbatim}
1278python setup.py bdist_wininst
1279\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001280
Thomas Heller5f52f722001-02-19 17:48:03 +00001281or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001282
Thomas Heller5f52f722001-02-19 17:48:03 +00001283\begin{verbatim}
1284python setup.py bdist --formats=wininst
1285\end{verbatim}
1286
1287If you have a pure module distribution (only containing pure
1288Python modules and packages), the resulting installer will be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001289version independent and have a name like \file{foo-1.0.win32.exe}.
Thomas Heller5f52f722001-02-19 17:48:03 +00001290These installers can even be created on \UNIX{} or MacOS platforms.
1291
1292If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001293created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001294The installer filename will reflect this and now has the form
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001295\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001296for every Python version you want to support.
1297
1298The installer will try to compile pure modules into bytecode after
1299installation on the target system in normal and optimizing mode.
1300If you don't want this to happen for some reason, you can run
1301the bdist_wininst command with the \longprogramopt{no-target-compile} and/or
1302the \longprogramopt{no-target-optimize} option.
Greg Wardb6528972000-09-07 02:40:37 +00001303
Greg Ward007c04a2002-05-10 14:45:59 +00001304\section{Examples}
1305\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001306
1307
Greg Ward007c04a2002-05-10 14:45:59 +00001308\subsection{Pure Python distribution (by module)}
1309\label{pure-mod}
1310
1311If you're just distributing a couple of modules, especially if they
1312don't live in a particular package, you can specify them individually
1313using the \option{py\_modules} option in the setup script.
1314
1315In the simplest case, you'll have two files to worry about: a setup
1316script and the single module you're distributing, \file{foo.py} in this
1317example:
1318\begin{verbatim}
1319<root>/
1320 setup.py
1321 foo.py
1322\end{verbatim}
1323(In all diagrams in this section, \verb|<root>| will refer to the
1324distribution root directory.) A minimal setup script to describe this
1325situation would be:
1326\begin{verbatim}
1327from distutils.core import setup
1328setup(name = "foo", version = "1.0",
1329 py_modules = ["foo"])
1330\end{verbatim}
1331Note that the name of the distribution is specified independently with
1332the \option{name} option, and there's no rule that says it has to be the
1333same as the name of the sole module in the distribution (although that's
1334probably a good convention to follow). However, the distribution name
1335is used to generate filenames, so you should stick to letters, digits,
1336underscores, and hyphens.
1337
1338Since \option{py\_modules} is a list, you can of course specify multiple
1339modules, eg. if you're distributing modules \module{foo} and
1340\module{bar}, your setup might look like this:
1341\begin{verbatim}
1342<root>/
1343 setup.py
1344 foo.py
1345 bar.py
1346\end{verbatim}
1347and the setup script might be
1348\begin{verbatim}
1349from distutils.core import setup
1350setup(name = "foobar", version = "1.0",
1351 py_modules = ["foo", "bar"])
1352\end{verbatim}
1353
1354You can put module source files into another directory, but if you have
1355enough modules to do that, it's probably easier to specify modules by
1356package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001357
1358
Greg Ward007c04a2002-05-10 14:45:59 +00001359\subsection{Pure Python distribution (by package)}
1360\label{pure-pkg}
1361
1362If you have more than a couple of modules to distribute, especially if
1363they are in multiple packages, it's probably easier to specify whole
1364packages rather than individual modules. This works even if your
1365modules are not in a package; you can just tell the Distutils to process
1366modules from the root package, and that works the same as any other
1367package (except that you don't have to have an \file{\_\_init\_\_.py}
1368file).
1369
1370The setup script from the last example could also be written as
1371\begin{verbatim}
1372from distutils.core import setup
1373setup(name = "foobar", version = "1.0",
1374 packages = [""])
1375\end{verbatim}
1376(The empty string stands for the root package.)
1377
1378If those two files are moved into a subdirectory, but remain in the root
1379package, e.g.:
1380\begin{verbatim}
1381<root>/
1382 setup.py
1383 src/ foo.py
1384 bar.py
1385\end{verbatim}
1386then you would still specify the root package, but you have to tell the
1387Distutils where source files in the root package live:
1388\begin{verbatim}
1389from distutils.core import setup
1390setup(name = "foobar", version = "1.0",
1391 package_dir = {"": "src"},
1392 packages = [""])
1393\end{verbatim}
1394
1395More typically, though, you will want to distribute multiple modules in
1396the same package (or in sub-packages). For example, if the \module{foo}
1397and \module{bar} modules belong in package \module{foobar}, one way to
1398layout your source tree is
1399\begin{verbatim}
1400<root>/
1401 setup.py
1402 foobar/
1403 __init__.py
1404 foo.py
1405 bar.py
1406\end{verbatim}
1407This is in fact the default layout expected by the Distutils, and the
1408one that requires the least work to describe in your setup script:
1409\begin{verbatim}
1410from distutils.core import setup
1411setup(name = "foobar", version = "1.0",
1412 packages = ["foobar"])
1413\end{verbatim}
1414
1415If you want to put modules in directories not named for their package,
1416then you need to use the \option{package\_dir} option again. For
1417example, if the \file{src} directory holds modules in the
1418\module{foobar} package:
1419\begin{verbatim}
1420<root>/
1421 setup.py
1422 src/
1423 __init__.py
1424 foo.py
1425 bar.py
1426\end{verbatim}
1427an appropriate setup script would be
1428\begin{verbatim}
1429from distutils.core import setup
1430setup(name = "foobar", version = "1.0",
1431 package_dir = {"foobar" : "src"},
1432 packages = ["foobar"])
1433\end{verbatim}
1434
1435Or, you might put modules from your main package right in the
1436distribution root:
1437\begin{verbatim}
1438<root>/
1439 setup.py
1440 __init__.py
1441 foo.py
1442 bar.py
1443\end{verbatim}
1444in which case your setup script would be
1445\begin{verbatim}
1446from distutils.core import setup
1447setup(name = "foobar", version = "1.0",
1448 package_dir = {"foobar" : ""},
1449 packages = ["foobar"])
1450\end{verbatim}
1451(The empty string also stands for the current directory.)
1452
1453If you have sub-packages, they must be explicitly listed in
1454\option{packages}, but any entries in \option{package\_dir}
1455automatically extend to sub-packages. (In other words, the Distutils
1456does \emph{not} scan your source tree, trying to figure out which
1457directories correspond to Python packages by looking for
1458\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1459sub-package:
1460\begin{verbatim}
1461<root>/
1462 setup.py
1463 foobar/
1464 __init__.py
1465 foo.py
1466 bar.py
1467 subfoo/
1468 __init__.py
1469 blah.py
1470\end{verbatim}
1471then the corresponding setup script would be
1472\begin{verbatim}
1473from distutils.core import setup
1474setup(name = "foobar", version = "1.0",
1475 packages = ["foobar", "foobar.subfoo"])
1476\end{verbatim}
1477(Again, the empty string in \option{package\_dir} stands for the current
1478directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001479
1480
Greg Ward007c04a2002-05-10 14:45:59 +00001481\subsection{Single extension module}
1482\label{single-ext}
1483
1484Extension modules are specified using the \option{ext\_modules} option.
1485\option{package\_dir} has no effect on where extension source files are
1486found; it only affects the source for pure Python modules. The simplest
1487case, a single extension module in a single C source file, is:
1488\begin{verbatim}
1489<root>/
1490 setup.py
1491 foo.c
1492\end{verbatim}
1493If the \module{foo} extension belongs in the root package, the setup
1494script for this could be
1495\begin{verbatim}
1496from distutils.core import setup
1497setup(name = "foobar", version = "1.0",
1498 ext_modules = [Extension("foo", ["foo.c"])])
1499\end{verbatim}
1500
1501If the extension actually belongs in a package, say \module{foopkg},
1502then
1503
1504With exactly the same source tree layout, this extension can be put in
1505the \module{foopkg} package simply by changing the name of the
1506extension:
1507\begin{verbatim}
1508from distutils.core import setup
1509setup(name = "foobar", version = "1.0",
1510 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1511\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001512
1513
Fred Drakea09262e2001-03-01 18:35:43 +00001514%\subsection{Multiple extension modules}
1515%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001516
1517
Fred Drakea09262e2001-03-01 18:35:43 +00001518%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001519
1520
Fred Drakea09262e2001-03-01 18:35:43 +00001521%\section{Extending the Distutils}
1522%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001523
1524
Fred Drakea09262e2001-03-01 18:35:43 +00001525%\subsection{Extending existing commands}
1526%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001527
1528
Fred Drakea09262e2001-03-01 18:35:43 +00001529%\subsection{Writing new commands}
1530%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001531
Fred Drakea09262e2001-03-01 18:35:43 +00001532%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001533
Greg Ward4a9e7222000-04-25 02:57:36 +00001534
1535
Greg Ward16aafcd2000-04-09 04:06:44 +00001536\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001537\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001538
1539
Fred Drakea09262e2001-03-01 18:35:43 +00001540%\subsection{Building modules: the \protect\command{build} command family}
1541%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001542
Fred Drakea09262e2001-03-01 18:35:43 +00001543%\subsubsection{\protect\command{build}}
1544%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001545
Fred Drakea09262e2001-03-01 18:35:43 +00001546%\subsubsection{\protect\command{build\_py}}
1547%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001548
Fred Drakea09262e2001-03-01 18:35:43 +00001549%\subsubsection{\protect\command{build\_ext}}
1550%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001551
Fred Drakea09262e2001-03-01 18:35:43 +00001552%\subsubsection{\protect\command{build\_clib}}
1553%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001554
1555
Greg Wardfacb8db2000-04-09 04:32:40 +00001556\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001557\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001558
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001559The install command ensures that the build commands have been run and then
1560runs the subcommands \command{install\_lib},
1561\command{install\_data} and
1562\command{install\_scripts}.
1563
Fred Drakea09262e2001-03-01 18:35:43 +00001564%\subsubsection{\protect\command{install\_lib}}
1565%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001566
1567\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001568\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001569This command installs all data files provided with the distribution.
1570
1571\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001572\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001573This command installs all (Python) scripts in the distribution.
1574
Greg Ward16aafcd2000-04-09 04:06:44 +00001575
Fred Drakea09262e2001-03-01 18:35:43 +00001576%\subsection{Cleaning up: the \protect\command{clean} command}
1577%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001578
1579
Fred Drakeeff9a872000-10-26 16:41:03 +00001580\subsection{Creating a source distribution: the
1581 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001582\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001583
1584
1585\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001586
Greg Ward16aafcd2000-04-09 04:06:44 +00001587The manifest template commands are:
1588\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001589 \lineii{include \var{pat1} \var{pat2} ... }
1590 {include all files matching any of the listed patterns}
1591 \lineii{exclude \var{pat1} \var{pat2} ... }
1592 {exclude all files matching any of the listed patterns}
1593 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1594 {include all files under \var{dir} matching any of the listed patterns}
1595 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1596 {exclude all files under \var{dir} matching any of the listed patterns}
1597 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001598 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001599 any of the listed patterns}
1600 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001601 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001602 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001603 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1604 \lineii{graft \var{dir}}{include all files under \var{dir}}
1605\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001606The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001607sequence of regular filename characters, \code{?} matches any single
1608regular filename character, and \code{[\var{range}]} matches any of the
1609characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001610\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001611platform-specific: on \UNIX{} it is anything except slash; on Windows
1612anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001613
Fred Drakeeff9a872000-10-26 16:41:03 +00001614\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001615
1616
Fred Drakea09262e2001-03-01 18:35:43 +00001617%\subsection{Creating a built distribution: the
1618% \protect\command{bdist} command family}
1619%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001620
1621
Fred Drakeab70b382001-08-02 15:13:15 +00001622%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001623
Fred Drakeab70b382001-08-02 15:13:15 +00001624%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001625
Fred Drakeab70b382001-08-02 15:13:15 +00001626%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001627
Fred Drakeab70b382001-08-02 15:13:15 +00001628%\subsubsection{\protect\command{bdist\_wininst}}
1629
1630
1631\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001632
1633
Greg Wardabc52162000-02-26 00:52:48 +00001634\end{document}