blob: 84e9cfce8b4ca8d9ffc4dca28a292bc5e1206dfd [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
24 (``Distutils'') from the module developer's point-of-view, describing
25 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
74\subsection{A simple example}
Greg Warde78298a2000-04-28 17:12:24 +000075\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000076
77The setup script is usually quite simple, although since it's written in
Greg Ward47f99a62000-09-04 20:07:15 +000078Python, there are no arbitrary limits to what you can do with
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000079it, though you should be careful about putting arbitrarily expensive
80 operations in your setup script. Unlike, say, Autoconf-style configure
Greg Ward47f99a62000-09-04 20:07:15 +000081 scripts, the setup script may be run multiple times in the course of
82 building and installing your module distribution. If you need to
83 insert potentially expensive processing steps into the Distutils
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000084 chain, see section~\ref{extending} on extending the Distutils.
85
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
88little 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
102 meta-data (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)
106\item it's recommended that you supply a little more meta-data, in
107 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
Greg Ward16aafcd2000-04-09 04:06:44 +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
135Distutils: first, both developers and installers have the same basic
136user 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}
158(\command{bdist\_pkgtool}, and HP-UX \program{swinstall} (\command{bdist_sdux}).
159For example, the following command will create an RPM file called
160\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
Greg Ward1d8f57a2000-08-05 00:43:11 +0000166(This uses the \command{rpm} command, so has to be run on an RPM-based
167system such as Red Hat Linux, SuSE Linux, or Mandrake Linux.)
168
169You can find out what distribution formats are available at any time by
170running
Fred Drakea09262e2001-03-01 18:35:43 +0000171
Greg Ward1d8f57a2000-08-05 00:43:11 +0000172\begin{verbatim}
173python setup.py bdist --help-formats
174\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000175
176
177\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000178\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000179
180If you're reading this document, you probably have a good idea of what
181modules, extensions, and so forth are. Nevertheless, just to be sure
182that everyone is operating from a common starting point, we offer the
183following glossary of common Python terms:
184\begin{description}
185\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000186 code imported by some other code. Three types of modules concern us
187 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000188
Greg Ward16aafcd2000-04-09 04:06:44 +0000189\item[pure Python module] a module written in Python and contained in a
190 single \file{.py} file (and possibly associated \file{.pyc} and/or
191 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000192
Greg Ward16aafcd2000-04-09 04:06:44 +0000193\item[extension module] a module written in the low-level language of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000194 the Python implementation: C/C++ for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000195 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000196 file, e.g. a shared object (\file{.so}) file for Python extensions on
197 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000198 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drakeeff9a872000-10-26 16:41:03 +0000199 currently, the Distutils only handles C/C++ extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000200
Greg Ward16aafcd2000-04-09 04:06:44 +0000201\item[package] a module that contains other modules; typically contained
202 in a directory in the filesystem and distinguished from other
203 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000204
Greg Ward6153fa12000-05-26 02:24:28 +0000205\item[root package] the root of the hierarchy of packages. (This isn't
206 really a package, since it doesn't have an \file{\_\_init\_\_.py}
207 file. But we have to call it something.) The vast majority of the
208 standard library is in the root package, as are many small, standalone
209 third-party modules that don't belong to a larger module collection.
210 Unlike regular packages, modules in the root package can be found in
211 many directories: in fact, every directory listed in \code{sys.path}
212 can contribute modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000213\end{description}
214
215
216\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000217\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000218
219The following terms apply more specifically to the domain of
220distributing Python modules using the Distutils:
221\begin{description}
222\item[module distribution] a collection of Python modules distributed
223 together as a single downloadable resource and meant to be installed
224 \emph{en masse}. Examples of some well-known module distributions are
225 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000226 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000227 is already taken in the Python context: a single module distribution
228 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000229
Greg Ward16aafcd2000-04-09 04:06:44 +0000230\item[pure module distribution] a module distribution that contains only
231 pure Python modules and packages. Sometimes referred to as a ``pure
232 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000233
Greg Ward16aafcd2000-04-09 04:06:44 +0000234\item[non-pure module distribution] a module distribution that contains
235 at least one extension module. Sometimes referred to as a ``non-pure
236 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000237
Greg Ward16aafcd2000-04-09 04:06:44 +0000238\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000239 source distribution); the directory where \file{setup.py} exists. Generally
240 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000241\end{description}
242
243
244\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000245\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000246
247The setup script is the centre of all activity in building,
248distributing, and installing modules using the Distutils. The main
249purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000250the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000251do the right thing. As we saw in section~\ref{simple-example} above,
252the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000253most information supplied to the Distutils by the module developer is
254supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000255
256Here's a slightly more involved example, which we'll follow for the next
257couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000258although the Distutils are included with Python 1.6 and later, they also
259have an independent existence so that Python 1.5.2 users can use them to
260install other module distributions. The Distutils' own setup script,
261shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000262
263\begin{verbatim}
264#!/usr/bin/env python
265
266from distutils.core import setup
267
Fred Drakea09262e2001-03-01 18:35:43 +0000268setup(name="Distutils",
269 version="1.0",
270 description="Python Distribution Utilities",
271 author="Greg Ward",
272 author_email="gward@python.net",
273 url="http://www.python.org/sigs/distutils-sig/",
274 packages=['distutils', 'distutils.command'],
275 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000276\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000277
Greg Ward16aafcd2000-04-09 04:06:44 +0000278There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000279distribution presented in section~\ref{simple-example}: more
Greg Ward16aafcd2000-04-09 04:06:44 +0000280meta-data, and the specification of pure Python modules by package,
281rather than by module. This is important since the Distutils consist of
282a couple of dozen modules split into (so far) two packages; an explicit
283list of every module would be tedious to generate and difficult to
284maintain.
285
Greg Ward46b98e32000-04-14 01:53:36 +0000286Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000287script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000288slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000289platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000290current platform before actually using the pathname. This makes your
291setup script portable across operating systems, which of course is one
292of the major goals of the Distutils. In this spirit, all pathnames in
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000293this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000294mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000295path, the opposite of the MacOS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000296
Thomas Heller5f52f722001-02-19 17:48:03 +0000297This, of course, only applies to pathnames given to Distutils functions.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000298If you, for example, use standard python functions such as \function{glob.glob}
299or \function{os.listdir} to specify files, you should be careful to write portable
Thomas Heller5f52f722001-02-19 17:48:03 +0000300code instead of hardcoding path separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000301
Thomas Heller5f52f722001-02-19 17:48:03 +0000302\begin{verbatim}
303 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
304 os.listdir(os.path.join('mydir', 'subdir'))
305\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000306
Greg Ward2afffd42000-08-06 20:37:24 +0000307\subsection{Listing whole packages}
308\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000309
310The \option{packages} option tells the Distutils to process (build,
311distribute, install, etc.) all pure Python modules found in each package
312mentioned in the \option{packages} list. In order to do this, of
313course, there has to be a correspondence between package names and
314directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000315obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000316\file{distutils} relative to the distribution root. Thus, when you say
317\code{packages = ['foo']} in your setup script, you are promising that
318the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
319be spelled differently on your system, but you get the idea) relative to
320the directory where your setup script lives. (If you break this
321promise, the Distutils will issue a warning but process the broken
322package anyways.)
323
324If you use a different convention to lay out your source directory,
325that's no problem: you just have to supply the \option{package\_dir}
326option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000327you keep all Python source under \file{lib}, so that modules in the
328``root package'' (i.e., not in any package at all) are right in
329\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
330and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000331
Greg Ward16aafcd2000-04-09 04:06:44 +0000332\begin{verbatim}
333package_dir = {'': 'lib'}
334\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000335
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000336in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000337and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000338directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000339you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000340\file{lib/foo/\_\_init\_\_.py} exists.
341
Greg Ward1ecc2512000-04-19 22:36:24 +0000342Another possible convention is to put the \module{foo} package right in
343\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000344would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000345
Greg Ward16aafcd2000-04-09 04:06:44 +0000346\begin{verbatim}
347package_dir = {'foo': 'lib'}
348\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000349
Greg Ward59d382e2000-05-26 01:04:47 +0000350A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
351dictionary implicitly applies to all packages below \var{package}, so
352the \module{foo.bar} case is automatically handled here. In this
353example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
354to look for \file{lib/\_\_init\_\_.py} and
355\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
356\option{package\_dir} applies recursively, you must explicitly list all
357packages in \option{packages}: the Distutils will \emph{not} recursively
358scan your source tree looking for any directory with an
359\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000360
361
362\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000363\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000364
365For a small module distribution, you might prefer to list all modules
366rather than listing packages---especially the case of a single module
367that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000368simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000369slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000370
Greg Ward16aafcd2000-04-09 04:06:44 +0000371\begin{verbatim}
372py_modules = ['mod1', 'pkg.mod2']
373\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000374
Greg Ward16aafcd2000-04-09 04:06:44 +0000375This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000376other in the \module{pkg} package. Again, the default package/directory
377layout implies that these two modules can be found in \file{mod1.py} and
378\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000379And again, you can override the package/directory correspondence using
380the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000381
382
383\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000384\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000385
Greg Ward2afffd42000-08-06 20:37:24 +0000386Just as writing Python extension modules is a bit more complicated than
387writing pure Python modules, describing them to the Distutils is a bit
388more complicated. Unlike pure modules, it's not enough just to list
389modules or packages and expect the Distutils to go out and find the
390right files; you have to specify the extension name, source file(s), and
391any compile/link requirements (include directories, libraries to link
392with, etc.).
393
394All of this is done through another keyword argument to
395\function{setup()}, the \option{extensions} option. \option{extensions}
396is just a list of \class{Extension} instances, each of which describes a
397single extension module. Suppose your distribution includes a single
398extension, called \module{foo} and implemented by \file{foo.c}. If no
399additional instructions to the compiler/linker are needed, describing
400this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000401
Greg Ward2afffd42000-08-06 20:37:24 +0000402\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000403uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000404\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000405
Greg Ward2afffd42000-08-06 20:37:24 +0000406The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000407\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000408script for a module distribution that contains only this one extension
409and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000410
Greg Ward2afffd42000-08-06 20:37:24 +0000411\begin{verbatim}
412from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000413setup(name="foo", version="1.0",
414 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000415\end{verbatim}
416
417The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000418machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000419great deal of flexibility in describing Python extensions, which is
420explained in the following sections.
421
422
423\subsubsection{Extension names and packages}
424
425The first argument to the \class{Extension} constructor is always the
426name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000427
Greg Ward2afffd42000-08-06 20:37:24 +0000428\begin{verbatim}
429Extension("foo", ["src/foo1.c", "src/foo2.c"])
430\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000431
Greg Ward2afffd42000-08-06 20:37:24 +0000432describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000433
Greg Ward2afffd42000-08-06 20:37:24 +0000434\begin{verbatim}
435Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
436\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000437
Greg Ward2afffd42000-08-06 20:37:24 +0000438describes the same extension in the \module{pkg} package. The source
439files and resulting object code are identical in both cases; the only
440difference is where in the filesystem (and therefore where in Python's
441namespace hierarchy) the resulting extension lives.
442
443If you have a number of extensions all in the same package (or all under
444the same base package), use the \option{ext\_package} keyword argument
445to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000446
Greg Ward2afffd42000-08-06 20:37:24 +0000447\begin{verbatim}
448setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000449 ext_package="pkg",
450 ext_modules=[Extension("foo", ["foo.c"]),
451 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000452 )
453\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000454
Greg Ward2afffd42000-08-06 20:37:24 +0000455will compile \file{foo.c} to the extension \module{pkg.foo}, and
456\file{bar.c} to \module{pkg.subpkg.bar}.
457
458
459\subsubsection{Extension source files}
460
461The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000462source files. Since the Distutils currently only support C, \Cpp, and
463Objective-C extensions, these are normally C/\Cpp/Objective-C source
464files. (Be sure to use appropriate extensions to distinguish \Cpp\
465source files: \file{.cc} and \file{.cpp} seem to be recognized by both
466\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000467
468However, you can also include SWIG interface (\file{.i}) files in the
469list; the \command{build\_ext} command knows how to deal with SWIG
470extensions: it will run SWIG on the interface file and compile the
471resulting C/C++ file into your extension.
472
473\XXX{SWIG support is rough around the edges and largely untested;
474 especially SWIG support of C++ extensions! Explain in more detail
475 here when the interface firms up.}
476
477On some platforms, you can include non-source files that are processed
478by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000479means Windows message text (\file{.mc}) files and resource definition
480(\file{.rc}) files for Visual C++. These will be compiled to binary resource
481(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000482
483
484\subsubsection{Preprocessor options}
485
486Three optional arguments to \class{Extension} will help if you need to
487specify include directories to search or preprocessor macros to
488define/undefine: \code{include\_dirs}, \code{define\_macros}, and
489\code{undef\_macros}.
490
491For example, if your extension requires header files in the
492\file{include} directory under your distribution root, use the
493\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000494
Greg Ward2afffd42000-08-06 20:37:24 +0000495\begin{verbatim}
496Extension("foo", ["foo.c"], include_dirs=["include"])
497\end{verbatim}
498
499You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000500extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000501\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000502
Greg Ward2afffd42000-08-06 20:37:24 +0000503\begin{verbatim}
504Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
505\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000506
Greg Ward2afffd42000-08-06 20:37:24 +0000507You should avoid this sort of non-portable usage if you plan to
508distribute your code: it's probably better to write your code to include
509(e.g.) \code{<X11/Xlib.h>}.
510
511If you need to include header files from some other Python extension,
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000512you can take advantage of the fact that the Distutils installs
513extension header files in a consistent way. For example, on a
514standard \UNIX{} installation the Numerical Python header files are
515installed to \file{/usr/local/include/python1.5/Numerical}. (The
516exact location will differ according to your platform and Python
517installation.) Since the Python include
518directory---\file{/usr/local/include/python1.5} in this case---is
519always included in the search path when building Python extensions,
520the best approach is to include (e.g.)
Greg Ward2afffd42000-08-06 20:37:24 +0000521\code{<Numerical/arrayobject.h>}. If you insist on putting the
522\file{Numerical} include directory right into your header search path,
523though, you can find that directory using the Distutils
524\module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000525
Greg Ward2afffd42000-08-06 20:37:24 +0000526\begin{verbatim}
527from distutils.sysconfig import get_python_inc
528incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
529setup(...,
530 Extension(..., include_dirs=[incdir]))
531\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000532
Greg Ward2afffd42000-08-06 20:37:24 +0000533Even though this is quite portable---it will work on any Python
534installation, regardless of platform---it's probably easier to just
535write your C code in the sensible way.
536
537You can define and undefine pre-processor macros with the
538\code{define\_macros} and \code{undef\_macros} options.
539\code{define\_macros} takes a list of \code{(name, value)} tuples, where
540\code{name} is the name of the macro to define (a string) and
541\code{value} is its value: either a string or \code{None}. (Defining a
542macro \code{FOO} to \code{None} is the equivalent of a bare
543\code{\#define FOO} in your C source: with most compilers, this sets
544\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
545a list of macros to undefine.
546
547For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000548
Greg Ward2afffd42000-08-06 20:37:24 +0000549\begin{verbatim}
550Extension(...,
551 define_macros=[('NDEBUG', '1')],
552 ('HAVE_STRFTIME', None),
553 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
554\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000555
Greg Ward2afffd42000-08-06 20:37:24 +0000556is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000557
Greg Ward2afffd42000-08-06 20:37:24 +0000558\begin{verbatim}
559#define NDEBUG 1
560#define HAVE_STRFTIME
561#undef HAVE_FOO
562#undef HAVE_BAR
563\end{verbatim}
564
565
566\subsubsection{Library options}
567
568You can also specify the libraries to link against when building your
569extension, and the directories to search for those libraries. The
570\code{libraries} option is a list of libraries to link against,
571\code{library\_dirs} is a list of directories to search for libraries at
572link-time, and \code{runtime\_library\_dirs} is a list of directories to
573search for shared (dynamically loaded) libraries at run-time.
574
575For example, if you need to link against libraries known to be in the
576standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000577
Greg Ward2afffd42000-08-06 20:37:24 +0000578\begin{verbatim}
579Extension(...,
580 libraries=["gdbm", "readline"])
581\end{verbatim}
582
583If you need to link with libraries in a non-standard location, you'll
584have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000585
Greg Ward2afffd42000-08-06 20:37:24 +0000586\begin{verbatim}
587Extension(...,
588 library_dirs=["/usr/X11R6/lib"],
589 libraries=["X11", "Xt"])
590\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000591
Greg Ward2afffd42000-08-06 20:37:24 +0000592(Again, this sort of non-portable construct should be avoided if you
593intend to distribute your code.)
594
Thomas Heller5f52f722001-02-19 17:48:03 +0000595\XXX{Should mention clib libraries here or somewhere else!}
596
597\subsubsection{Other options}
598
599There are still some other options which can be used to handle special
600cases.
601
602The \option{extra\_objects} option is a list of object files to be passed
603to the linker. These files must not have extensions, as the default
604extension for the compiler is used.
605
606\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000607to specify additional command line options for the respective compiler and
608linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000609
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000610\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000611of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000612is not needed when building compiled extensions: Distutils
613will automatically add \code{initmodule}
614to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000615
616\subsection{Listing scripts}
617So far we have been dealing with pure and non-pure Python modules,
618which are usually not run by themselves but imported by scripts.
619
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000620Scripts are files containing Python source code, indended to be
621started from the command line. Scripts don't require Distutils to do
622anything very complicated. The only clever feature is that if the
623first line of the script starts with \code{\#!} and contains the word
624``python'', the Distutils will adjust the first line to refer to the
625current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000626
627The \option{scripts} option simply is a list of files to be handled
628in this way.
629
630
631\subsection{Listing additional files}
Fred Drakea09262e2001-03-01 18:35:43 +0000632
Thomas Heller5f52f722001-02-19 17:48:03 +0000633The \option{data\_files} option can be used to specify additional
634files needed by the module distribution: configuration files,
635data files, anything which does not fit in the previous categories.
636
Fred Drake632bda32002-03-08 22:02:06 +0000637\option{data\_files} specifies a sequence of (\var{directory},
638\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000639
Thomas Heller5f52f722001-02-19 17:48:03 +0000640\begin{verbatim}
641setup(...
642 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
643 ('config', ['cfg/data.cfg'])])
644\end{verbatim}
645
646Note that you can specify the directory names where the data files
647will be installed, but you cannot rename the data files themselves.
648
Fred Drake632bda32002-03-08 22:02:06 +0000649Each (\var{directory}, \var{files}) pair in the sequence specifies the
650installation directory and the files to install there. If
651\var{directory} is a relative path, it is interpreted relative to the
652installation prefix (Python's \code{sys.prefix} for pure-Python
653packages, \code{sys.exec_prefix} for packages that contain extension
654modules). Each file name in \var{files} is interpreted relative to
655the \file{setup.py} script at the top of the package source
656distribution. No directory information from \var{files} is used to
657determine the final location of the installed file; only the name of
658the file is used.
659
Thomas Heller5f52f722001-02-19 17:48:03 +0000660You can specify the \option{data\_files} options as a simple sequence
661of files without specifying a target directory, but this is not recommended,
662and the \command{install} command will print a warning in this case.
663To install data files directly in the target directory, an empty
664string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000665
666
667\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000668\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000669
Greg Ward16aafcd2000-04-09 04:06:44 +0000670Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000671distribution \emph{a priori}: you may need to get some information from
672the user, or from the user's system, in order to proceed. As long as
673that information is fairly simple---a list of directories to search for
674C header files or libraries, for example---then providing a
675configuration file, \file{setup.cfg}, for users to edit is a cheap and
676easy way to solicit it. Configuration files also let you provide
677default values for any command option, which the installer can then
678override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000679
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000680% (If you have more advanced needs, such as determining which extensions
681% to build based on what capabilities are present on the target system,
682% then you need the Distutils ``auto-configuration'' facility. This
683% started to appear in Distutils 0.9 but, as of this writing, isn't mature
684% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000685
Greg Ward47f99a62000-09-04 20:07:15 +0000686The setup configuration file is a useful middle-ground between the setup
687script---which, ideally, would be opaque to installers\footnote{This
688 ideal probably won't be achieved until auto-configuration is fully
689 supported by the Distutils.}---and the command-line to the setup
690script, which is outside of your control and entirely up to the
691installer. In fact, \file{setup.cfg} (and any other Distutils
692configuration files present on the target system) are processed after
693the contents of the setup script, but before the command-line. This has
694several useful consequences:
695\begin{itemize}
696\item installers can override some of what you put in \file{setup.py} by
697 editing \file{setup.cfg}
698\item you can provide non-standard defaults for options that are not
699 easily set in \file{setup.py}
700\item installers can override anything in \file{setup.cfg} using the
701 command-line options to \file{setup.py}
702\end{itemize}
703
704The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000705
Greg Ward47f99a62000-09-04 20:07:15 +0000706\begin{verbatim}
707[command]
708option=value
709...
710\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000711
Greg Ward47f99a62000-09-04 20:07:15 +0000712where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000713\command{build\_py}, \command{install}), and \var{option} is one of
714the options that command supports. Any number of options can be
715supplied for each command, and any number of command sections can be
716included in the file. Blank lines are ignored, as are comments, which
717run from a \character{\#} character until the end of the line. Long
718option values can be split across multiple lines simply by indenting
719the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000720
721You can find out the list of options supported by a particular command
722with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000723
Greg Ward47f99a62000-09-04 20:07:15 +0000724\begin{verbatim}
725> python setup.py --help build_ext
726[...]
727Options for 'build_ext' command:
728 --build-lib (-b) directory for compiled extension modules
729 --build-temp (-t) directory for temporary files (build by-products)
730 --inplace (-i) ignore build-lib and put compiled extensions into the
731 source directory alongside your pure Python modules
732 --include-dirs (-I) list of directories to search for header files
733 --define (-D) C preprocessor macros to define
734 --undef (-U) C preprocessor macros to undefine
735[...]
736\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000737
Greg Ward47f99a62000-09-04 20:07:15 +0000738Or consult section \ref{reference} of this document (the command
739reference).
740
741Note that an option spelled \longprogramopt{foo-bar} on the command-line
742is spelled \option{foo\_bar} in configuration files.
743
744For example, say you want your extensions to be built
745``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000746want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000747in the same source directory as your pure Python modules
748\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
749\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000750
Greg Ward47f99a62000-09-04 20:07:15 +0000751\begin{verbatim}
752python setup.py build_ext --inplace
753\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000754
Greg Ward47f99a62000-09-04 20:07:15 +0000755But this requires that you always specify the \command{build\_ext}
756command explicitly, and remember to provide \longprogramopt{inplace}.
757An easier way is to ``set and forget'' this option, by encoding it in
758\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000759
Greg Ward47f99a62000-09-04 20:07:15 +0000760\begin{verbatim}
761[build_ext]
762inplace=1
763\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000764
Greg Ward47f99a62000-09-04 20:07:15 +0000765This will affect all builds of this module distribution, whether or not
766you explcitly specify \command{build\_ext}. If you include
767\file{setup.cfg} in your source distribution, it will also affect
768end-user builds---which is probably a bad idea for this option, since
769always building extensions in-place would break installation of the
770module distribution. In certain peculiar cases, though, modules are
771built right in their installation directory, so this is conceivably a
772useful ability. (Distributing extensions that expect to be built in
773their installation directory is almost always a bad idea, though.)
774
775Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000776change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000777everything required to generate a ``spec'' file for creating an RPM
778distribution. Some of this information comes from the setup script, and
779some is automatically generated by the Distutils (such as the list of
780files installed). But some of it has to be supplied as options to
781\command{bdist\_rpm}, which would be very tedious to do on the
782command-line for every run. Hence, here is a snippet from the
783Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000784
Greg Ward47f99a62000-09-04 20:07:15 +0000785\begin{verbatim}
786[bdist_rpm]
787release = 1
788packager = Greg Ward <gward@python.net>
789doc_files = CHANGES.txt
790 README.txt
791 USAGE.txt
792 doc/
793 examples/
794\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000795
Greg Ward47f99a62000-09-04 20:07:15 +0000796Note that the \option{doc\_files} option is simply a
797whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000798
799
Fred Drakea09262e2001-03-01 18:35:43 +0000800\begin{seealso}
801 \seetitle[../inst/config-syntax.html]{Installing Python
802 Modules}{More information on the configuration files is
803 available in the manual for system administrators.}
804\end{seealso}
805
806
Greg Ward16aafcd2000-04-09 04:06:44 +0000807\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000808\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000809
Greg Warde78298a2000-04-28 17:12:24 +0000810As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000811\command{sdist} command to create a source distribution. In the
812simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000813
Greg Ward16aafcd2000-04-09 04:06:44 +0000814\begin{verbatim}
815python setup.py sdist
816\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000817
Greg Ward19c67f82000-06-24 01:33:16 +0000818(assuming you haven't specified any \command{sdist} options in the setup
819script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000820default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000821tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
822\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000823
Greg Wardd5767a52000-04-19 22:48:09 +0000824You can specify as many formats as you like using the
825\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000826
Greg Ward16aafcd2000-04-09 04:06:44 +0000827\begin{verbatim}
828python setup.py sdist --formats=gztar,zip
829\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000830
Greg Ward16aafcd2000-04-09 04:06:44 +0000831to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000832\begin{tableiii}{l|l|c}{code}%
833 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000834 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
835 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000836 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000837 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000838 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000839\end{tableiii}
840
841\noindent Notes:
842\begin{description}
843\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000844\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000845\item[(3)] requires either external \program{zip} utility or
846 \module{zipfile} module (not part of the standard Python library)
Greg Ward47f99a62000-09-04 20:07:15 +0000847\item[(4)] requires external utilities: \program{tar} and possibly one
848 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000849\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000850
851
Greg Ward54589d42000-09-06 01:37:35 +0000852
853\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000854\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000855
Greg Ward54589d42000-09-06 01:37:35 +0000856If you don't supply an explicit list of files (or instructions on how to
857generate one), the \command{sdist} command puts a minimal default set
858into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000859\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000860\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000861 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000862\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000863 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000864 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000865\item anything that looks like a test script: \file{test/test*.py}
866 (currently, the Distutils don't do anything with test scripts except
867 include them in source distributions, but in the future there will be
868 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000869\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
870 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000871\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000872
Greg Ward16aafcd2000-04-09 04:06:44 +0000873Sometimes this is enough, but usually you will want to specify
874additional files to distribute. The typical way to do this is to write
875a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000876manifest template is just a list of instructions for how to generate
877your manifest file, \file{MANIFEST}, which is the exact list of files to
878include in your source distribution. The \command{sdist} command
879processes this template and generates a manifest based on its
880instructions and what it finds in the filesystem.
881
882If you prefer to roll your own manifest file, the format is simple: one
883filename per line, regular files (or symlinks to them) only. If you do
884supply your own \file{MANIFEST}, you must specify everything: the
885default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000886
887The manifest template has one command per line, where each command
888specifies a set of files to include or exclude from the source
889distribution. For an example, again we turn to the Distutils' own
890manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +0000891
Greg Ward16aafcd2000-04-09 04:06:44 +0000892\begin{verbatim}
893include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000894recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000895prune examples/sample?/build
896\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000897
Greg Ward16aafcd2000-04-09 04:06:44 +0000898The meanings should be fairly clear: include all files in the
899distribution root matching \code{*.txt}, all files anywhere under the
900\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000901exclude all directories matching \code{examples/sample?/build}. All of
902this is done \emph{after} the standard include set, so you can exclude
903files from the standard set with explicit instructions in the manifest
904template. (Or, you can use the \longprogramopt{no-defaults} option to
905disable the standard set entirely.) There are several other commands
906available in the manifest template mini-language; see
907section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000908
Greg Ward54589d42000-09-06 01:37:35 +0000909The order of commands in the manifest template matters: initially, we
910have the list of default files as described above, and each command in
911the template adds to or removes from that list of files. Once we have
912fully processed the manifest template, we remove files that should not
913be included in the source distribution:
914\begin{itemize}
915\item all files in the Distutils ``build'' tree (default \file{build/})
916\item all files in directories named \file{RCS} or \file{CVS}
917\end{itemize}
918Now we have our complete list of files, which is written to the manifest
919for future reference, and then used to build the source distribution
920archive(s).
921
922You can disable the default set of included files with the
923\longprogramopt{no-defaults} option, and you can disable the standard
924exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000925
Greg Ward46b98e32000-04-14 01:53:36 +0000926Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000927\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000928Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000929\begin{enumerate}
930\item include all Python source files in the \file{distutils} and
931 \file{distutils/command} subdirectories (because packages
932 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000933 \option{packages} option in the setup script---see
934 section~\ref{setup-script})
935\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
936 (standard files)
937\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000938\item include \file{*.txt} in the distribution root (this will find
939 \file{README.txt} a second time, but such redundancies are weeded out
940 later)
Greg Ward54589d42000-09-06 01:37:35 +0000941\item include anything matching \file{*.txt} or \file{*.py} in the
942 sub-tree under \file{examples},
943\item exclude all files in the sub-trees starting at directories
944 matching \file{examples/sample?/build}---this may exclude files
945 included by the previous two steps, so it's important that the
946 \code{prune} command in the manifest template comes after the
947 \code{recursive-include} command
948\item exclude the entire \file{build} tree, and any \file{RCS} or
949 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000950\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000951Just like in the setup script, file and directory names in the manifest
952template should always be slash-separated; the Distutils will take care
953of converting them to the standard representation on your platform.
954That way, the manifest template is portable across operating systems.
955
Greg Ward16aafcd2000-04-09 04:06:44 +0000956
957\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000958\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000959
960The normal course of operations for the \command{sdist} command is as
961follows:
962\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000963\item if the manifest file, \file{MANIFEST} doesn't exist, read
964 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000965\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000966 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +0000967\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
968 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
969 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000970\item use the list of files now in \file{MANIFEST} (either just
971 generated or read in) to create the source distribution archive(s)
972\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000973There are a couple of options that modify this behaviour. First, use
974the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000975disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +0000976
Greg Ward54589d42000-09-06 01:37:35 +0000977Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000978example, if you have added or removed files or directories that match an
979existing pattern in the manifest template, you should regenerate the
980manifest:
Fred Drakea09262e2001-03-01 18:35:43 +0000981
Greg Ward16aafcd2000-04-09 04:06:44 +0000982\begin{verbatim}
983python setup.py sdist --force-manifest
984\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000985
986Or, you might just want to (re)generate the manifest, but not create a
987source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000988
Greg Ward16aafcd2000-04-09 04:06:44 +0000989\begin{verbatim}
990python setup.py sdist --manifest-only
991\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000992
Greg Ward54589d42000-09-06 01:37:35 +0000993\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
994\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
995\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000996
997
998\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +0000999\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001000
Greg Ward46b98e32000-04-14 01:53:36 +00001001A ``built distribution'' is what you're probably used to thinking of
1002either as a ``binary package'' or an ``installer'' (depending on your
1003background). It's not necessarily binary, though, because it might
1004contain only Python source code and/or byte-code; and we don't call it a
1005package, because that word is already spoken for in Python. (And
1006``installer'' is a term specific to the Windows world. \XXX{do Mac
1007 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001008
Greg Ward46b98e32000-04-14 01:53:36 +00001009A built distribution is how you make life as easy as possible for
1010installers of your module distribution: for users of RPM-based Linux
1011systems, it's a binary RPM; for Windows users, it's an executable
1012installer; for Debian-based Linux users, it's a Debian package; and so
1013forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001014distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001015designed to enable module developers to concentrate on their
1016specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001017intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001018distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001019are packagers.
1020
1021Of course, the module developer could be his own packager; or the
1022packager could be a volunteer ``out there'' somewhere who has access to
1023a platform which the original developer does not; or it could be
1024software periodically grabbing new source distributions and turning them
1025into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001026access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001027setup script and the \command{bdist} command family to generate built
1028distributions.
1029
1030As a simple example, if I run the following command in the Distutils
1031source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001032
Greg Ward46b98e32000-04-14 01:53:36 +00001033\begin{verbatim}
1034python setup.py bdist
1035\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001036
Greg Ward46b98e32000-04-14 01:53:36 +00001037then the Distutils builds my module distribution (the Distutils itself
1038in this case), does a ``fake'' installation (also in the \file{build}
1039directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001040platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001041file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001042file is considered ``dumb'' because it has to be unpacked in a specific
1043location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001044
Fred Drakeeff9a872000-10-26 16:41:03 +00001045Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001046\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001047from the right place installs the Distutils just as though you had
1048downloaded the source distribution and run \code{python setup.py
1049 install}. (The ``right place'' is either the root of the filesystem or
1050Python's \filevar{prefix} directory, depending on the options given to
1051the \command{bdist\_dumb} command; the default is to make dumb
1052distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001053
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001054Obviously, for pure Python distributions, this isn't any simpler than
1055just running \code{python setup.py install}---but for non-pure
1056distributions, which include extensions that would need to be
1057compiled, it can mean the difference between someone being able to use
1058your extensions or not. And creating ``smart'' built distributions,
1059such as an RPM package or an executable installer for Windows, is far
1060more convenient for users even if your distribution doesn't include
1061any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001062
Greg Wardb6528972000-09-07 02:40:37 +00001063The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001064similar to the \command{sdist} command, which you can use to select the
1065types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001066
Greg Ward46b98e32000-04-14 01:53:36 +00001067\begin{verbatim}
1068python setup.py bdist --format=zip
1069\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001070
Fred Drakeeff9a872000-10-26 16:41:03 +00001071would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001072\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001073unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001074
1075The available formats for built distributions are:
1076\begin{tableiii}{l|l|c}{code}%
1077 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001078 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1079 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1080 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1081 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1082 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001083 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1084 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1085 \lineiii{rpm}{RPM}{(5)}
1086% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001087 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001088\end{tableiii}
1089
1090\noindent Notes:
1091\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001092\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001093\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001094\item[(3)] requires external utilities: \program{tar} and possibly one
1095 of \program{gzip}, \program{bzip2}, or \program{compress}
1096\item[(4)] requires either external \program{zip} utility or
1097 \module{zipfile} module (not part of the standard Python library)
1098\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1099 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001100\end{description}
1101
1102You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001103\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001104directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001105\command{bdist} ``sub-commands'' actually generate several similar
1106formats; for instance, the \command{bdist\_dumb} command generates all
1107the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1108\code{zip}), and \command{bdist\_rpm} generates both binary and source
1109RPMs. The \command{bdist} sub-commands, and the formats generated by
1110each, are:
1111\begin{tableii}{l|l}{command}%
1112 {Command}{Formats}
1113 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1114 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001115 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001116\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001117
Greg Wardb6528972000-09-07 02:40:37 +00001118The following sections give details on the individual \command{bdist\_*}
1119commands.
1120
1121
1122\subsection{Creating dumb built distributions}
1123\label{creating-dumb}
1124
1125\XXX{Need to document absolute vs. prefix-relative packages here, but
1126 first I have to implement it!}
1127
1128
1129\subsection{Creating RPM packages}
1130\label{creating-rpms}
1131
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001132The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001133Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1134RPM-based Linux distributions) is your usual environment, creating RPM
1135packages for other users of that same distribution is trivial.
1136Depending on the complexity of your module distribution and differences
1137between Linux distributions, you may also be able to create RPMs that
1138work on different RPM-based distributions.
1139
1140The usual way to create an RPM of your module distribution is to run the
1141\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001142
Greg Wardb6528972000-09-07 02:40:37 +00001143\begin{verbatim}
1144python setup.py bdist_rpm
1145\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001146
Greg Wardb6528972000-09-07 02:40:37 +00001147or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001148
Greg Wardb6528972000-09-07 02:40:37 +00001149\begin{verbatim}
1150python setup.py bdist --formats=rpm
1151\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001152
Greg Wardb6528972000-09-07 02:40:37 +00001153The former allows you to specify RPM-specific options; the latter allows
1154you to easily specify multiple formats in one run. If you need to do
1155both, you can explicitly specify multiple \command{bdist\_*} commands
1156and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001157
Greg Wardb6528972000-09-07 02:40:37 +00001158\begin{verbatim}
1159python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1160 bdist_wininst --target_version="2.0"
1161\end{verbatim}
1162
1163Creating RPM packages is driven by a \file{.spec} file, much as using
1164the Distutils is driven by the setup script. To make your life easier,
1165the \command{bdist\_rpm} command normally creates a \file{.spec} file
1166based on the information you supply in the setup script, on the command
1167line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001168sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001169script as follows:
1170\begin{tableii}{l|l}{textrm}%
1171 {RPM \file{.spec} file option or section}{Distutils setup script option}
1172 \lineii{Name}{\option{name}}
1173 \lineii{Summary (in preamble)}{\option{description}}
1174 \lineii{Version}{\option{version}}
1175 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1176 \option{maintainer} and \option{maintainer\_email}}
1177 \lineii{Copyright}{\option{licence}}
1178 \lineii{Url}{\option{url}}
1179 \lineii{\%description (section)}{\option{long\_description}}
1180\end{tableii}
1181
1182Additionally, there many options in \file{.spec} files that don't have
1183corresponding options in the setup script. Most of these are handled
1184through options to the \command{bdist\_rpm} command as follows:
1185\begin{tableiii}{l|l|l}{textrm}%
1186 {RPM \file{.spec} file option or section}%
1187 {\command{bdist\_rpm} option}%
1188 {default value}
1189 \lineiii{Release}{\option{release}}{``1''}
1190 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1191 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001192 \lineiii{Packager}{\option{packager}}{(none)}
1193 \lineiii{Provides}{\option{provides}}{(none)}
1194 \lineiii{Requires}{\option{requires}}{(none)}
1195 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1196 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001197 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1198 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1199 \lineiii{Icon}{\option{icon}}{(none)}
1200\end{tableiii}
1201Obviously, supplying even a few of these options on the command-line
1202would be tedious and error-prone, so it's usually best to put them in
1203the setup configuration file, \file{setup.cfg}---see
1204section~\ref{setup-config}. If you distribute or package many Python
1205module distributions, you might want to put options that apply to all of
1206them in your personal Distutils configuration file
1207(\file{\textasciitilde/.pydistutils.cfg}).
1208
1209There are three steps to building a binary RPM package, all of which are
1210handled automatically by the Distutils:
1211\begin{enumerate}
1212\item create a \file{.spec} file, which describes the package (analogous
1213 to the Distutils setup script; in fact, much of the information in the
1214 setup script winds up in the \file{.spec} file)
1215\item create the source RPM
1216\item create the ``binary'' RPM (which may or may not contain binary
1217 code, depending on whether your module distribution contains Python
1218 extensions)
1219\end{enumerate}
1220Normally, RPM bundles the last two steps together; when you use the
1221Distutils, all three steps are typically bundled together.
1222
1223If you wish, you can separate these three steps. You can use the
1224\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1225create the \file{.spec} file and exit; in this case, the \file{.spec}
1226file will be written to the ``distribution directory''---normally
1227\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1228option. (Normally, the \file{.spec} file winds up deep in the ``build
1229tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1230
1231\XXX{this isn't implemented yet---is it needed?!}
1232You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001233\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001234\longprogramopt{spec-only}, this gives you an opportunity to customize
1235the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001236
Greg Wardb6528972000-09-07 02:40:37 +00001237\begin{verbatim}
1238> python setup.py bdist_rpm --spec-only
1239# ...edit dist/FooBar-1.0.spec
1240> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1241\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001242
Greg Wardb6528972000-09-07 02:40:37 +00001243(Although a better way to do this is probably to override the standard
1244\command{bdist\_rpm} command with one that writes whatever else you want
1245to the \file{.spec} file; see section~\ref{extending} for information on
1246extending the Distutils.)
1247
1248
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001249\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001250\label{creating-wininst}
1251
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001252Executable installers are the natural format for binary
Fred Drake17f690f2001-07-14 02:14:42 +00001253distributions on Windows. They display a nice graphical user interface,
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001254display some information about the module distribution to be installed taken
Fred Drake457c4192001-08-16 21:25:24 +00001255from the meta-data in the setup script, let the user select a few
Thomas Heller5f52f722001-02-19 17:48:03 +00001256(currently maybe too few) options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001257
Thomas Heller5f52f722001-02-19 17:48:03 +00001258Since the meta-data is taken from the setup script, creating
1259Windows installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001260
Thomas Heller5f52f722001-02-19 17:48:03 +00001261\begin{verbatim}
1262python setup.py bdist_wininst
1263\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001264
Thomas Heller5f52f722001-02-19 17:48:03 +00001265or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001266
Thomas Heller5f52f722001-02-19 17:48:03 +00001267\begin{verbatim}
1268python setup.py bdist --formats=wininst
1269\end{verbatim}
1270
1271If you have a pure module distribution (only containing pure
1272Python modules and packages), the resulting installer will be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001273version independent and have a name like \file{foo-1.0.win32.exe}.
Thomas Heller5f52f722001-02-19 17:48:03 +00001274These installers can even be created on \UNIX{} or MacOS platforms.
1275
1276If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001277created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001278The installer filename will reflect this and now has the form
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001279\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001280for every Python version you want to support.
1281
1282The installer will try to compile pure modules into bytecode after
1283installation on the target system in normal and optimizing mode.
1284If you don't want this to happen for some reason, you can run
1285the bdist_wininst command with the \longprogramopt{no-target-compile} and/or
1286the \longprogramopt{no-target-optimize} option.
Greg Wardb6528972000-09-07 02:40:37 +00001287
Fred Drakea09262e2001-03-01 18:35:43 +00001288%\section{Examples}
1289%\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001290
1291
Fred Drakea09262e2001-03-01 18:35:43 +00001292%\subsection{Pure Python distribution (by module)}
1293%\label{pure-mod}
Greg Ward16aafcd2000-04-09 04:06:44 +00001294
1295
Fred Drakea09262e2001-03-01 18:35:43 +00001296%\subsection{Pure Python distribution (by package)}
1297%\label{pure-pkg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001298
1299
Fred Drakea09262e2001-03-01 18:35:43 +00001300%\subsection{Single extension module}
1301%\label{single-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001302
1303
Fred Drakea09262e2001-03-01 18:35:43 +00001304%\subsection{Multiple extension modules}
1305%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001306
1307
Fred Drakea09262e2001-03-01 18:35:43 +00001308%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001309
1310
Greg Ward4a9e7222000-04-25 02:57:36 +00001311
Fred Drakea09262e2001-03-01 18:35:43 +00001312%\section{Extending the Distutils}
1313%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001314
1315
Fred Drakea09262e2001-03-01 18:35:43 +00001316%\subsection{Extending existing commands}
1317%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001318
1319
Fred Drakea09262e2001-03-01 18:35:43 +00001320%\subsection{Writing new commands}
1321%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001322
Fred Drakea09262e2001-03-01 18:35:43 +00001323%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001324
Greg Ward4a9e7222000-04-25 02:57:36 +00001325
1326
Greg Ward16aafcd2000-04-09 04:06:44 +00001327\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001328\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001329
1330
Fred Drakea09262e2001-03-01 18:35:43 +00001331%\subsection{Building modules: the \protect\command{build} command family}
1332%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001333
Fred Drakea09262e2001-03-01 18:35:43 +00001334%\subsubsection{\protect\command{build}}
1335%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001336
Fred Drakea09262e2001-03-01 18:35:43 +00001337%\subsubsection{\protect\command{build\_py}}
1338%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001339
Fred Drakea09262e2001-03-01 18:35:43 +00001340%\subsubsection{\protect\command{build\_ext}}
1341%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001342
Fred Drakea09262e2001-03-01 18:35:43 +00001343%\subsubsection{\protect\command{build\_clib}}
1344%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001345
1346
Greg Wardfacb8db2000-04-09 04:32:40 +00001347\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001348\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001349
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001350The install command ensures that the build commands have been run and then
1351runs the subcommands \command{install\_lib},
1352\command{install\_data} and
1353\command{install\_scripts}.
1354
Fred Drakea09262e2001-03-01 18:35:43 +00001355%\subsubsection{\protect\command{install\_lib}}
1356%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001357
1358\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001359\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001360This command installs all data files provided with the distribution.
1361
1362\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001363\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001364This command installs all (Python) scripts in the distribution.
1365
Greg Ward16aafcd2000-04-09 04:06:44 +00001366
Fred Drakea09262e2001-03-01 18:35:43 +00001367%\subsection{Cleaning up: the \protect\command{clean} command}
1368%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001369
1370
Fred Drakeeff9a872000-10-26 16:41:03 +00001371\subsection{Creating a source distribution: the
1372 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001373\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001374
1375
1376\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001377
Greg Ward16aafcd2000-04-09 04:06:44 +00001378The manifest template commands are:
1379\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001380 \lineii{include \var{pat1} \var{pat2} ... }
1381 {include all files matching any of the listed patterns}
1382 \lineii{exclude \var{pat1} \var{pat2} ... }
1383 {exclude all files matching any of the listed patterns}
1384 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1385 {include all files under \var{dir} matching any of the listed patterns}
1386 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1387 {exclude all files under \var{dir} matching any of the listed patterns}
1388 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001389 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001390 any of the listed patterns}
1391 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001392 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001393 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001394 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1395 \lineii{graft \var{dir}}{include all files under \var{dir}}
1396\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001397The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001398sequence of regular filename characters, \code{?} matches any single
1399regular filename character, and \code{[\var{range}]} matches any of the
1400characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001401\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001402platform-specific: on \UNIX{} it is anything except slash; on Windows
1403anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001404
Fred Drakeeff9a872000-10-26 16:41:03 +00001405\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001406
1407
Fred Drakea09262e2001-03-01 18:35:43 +00001408%\subsection{Creating a built distribution: the
1409% \protect\command{bdist} command family}
1410%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001411
1412
Fred Drakeab70b382001-08-02 15:13:15 +00001413%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001414
Fred Drakeab70b382001-08-02 15:13:15 +00001415%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001416
Fred Drakeab70b382001-08-02 15:13:15 +00001417%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001418
Fred Drakeab70b382001-08-02 15:13:15 +00001419%\subsubsection{\protect\command{bdist\_wininst}}
1420
1421
1422\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001423
1424
Greg Wardabc52162000-02-26 00:52:48 +00001425\end{document}