blob: 57f65c69f8bf031ff0049286ae11d03ce62f5b83 [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
Greg Ward58437f22002-05-10 14:40:22 +0000508distribute your code: it's probably better to write C code like
509\begin{verbatim}
510#include <X11/Xlib.h>
511\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000512
513If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000514you can take advantage of the fact that header files are installed in a
515consistent way by the Distutils \command{install\_header} command. For
516example, the Numerical Python header files are installed (on a standard
517Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
518(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000519installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000520directory---\file{/usr/local/include/python1.5} in this case---is always
521included in the search path when building Python extensions, the best
522approach is to write C code like
523\begin{verbatim}
524#include <Numerical/arrayobject.h>
525\end{verbatim}
526If you must put the \file{Numerical} include directory right into your
527header search path, though, you can find that directory using the
528Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000529
Greg Ward2afffd42000-08-06 20:37:24 +0000530\begin{verbatim}
531from distutils.sysconfig import get_python_inc
532incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
533setup(...,
534 Extension(..., include_dirs=[incdir]))
535\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000536
Greg Ward2afffd42000-08-06 20:37:24 +0000537Even though this is quite portable---it will work on any Python
538installation, regardless of platform---it's probably easier to just
539write your C code in the sensible way.
540
541You can define and undefine pre-processor macros with the
542\code{define\_macros} and \code{undef\_macros} options.
543\code{define\_macros} takes a list of \code{(name, value)} tuples, where
544\code{name} is the name of the macro to define (a string) and
545\code{value} is its value: either a string or \code{None}. (Defining a
546macro \code{FOO} to \code{None} is the equivalent of a bare
547\code{\#define FOO} in your C source: with most compilers, this sets
548\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
549a list of macros to undefine.
550
551For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000552
Greg Ward2afffd42000-08-06 20:37:24 +0000553\begin{verbatim}
554Extension(...,
555 define_macros=[('NDEBUG', '1')],
556 ('HAVE_STRFTIME', None),
557 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
558\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000559
Greg Ward2afffd42000-08-06 20:37:24 +0000560is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000561
Greg Ward2afffd42000-08-06 20:37:24 +0000562\begin{verbatim}
563#define NDEBUG 1
564#define HAVE_STRFTIME
565#undef HAVE_FOO
566#undef HAVE_BAR
567\end{verbatim}
568
569
570\subsubsection{Library options}
571
572You can also specify the libraries to link against when building your
573extension, and the directories to search for those libraries. The
574\code{libraries} option is a list of libraries to link against,
575\code{library\_dirs} is a list of directories to search for libraries at
576link-time, and \code{runtime\_library\_dirs} is a list of directories to
577search for shared (dynamically loaded) libraries at run-time.
578
579For example, if you need to link against libraries known to be in the
580standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000581
Greg Ward2afffd42000-08-06 20:37:24 +0000582\begin{verbatim}
583Extension(...,
584 libraries=["gdbm", "readline"])
585\end{verbatim}
586
587If you need to link with libraries in a non-standard location, you'll
588have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000589
Greg Ward2afffd42000-08-06 20:37:24 +0000590\begin{verbatim}
591Extension(...,
592 library_dirs=["/usr/X11R6/lib"],
593 libraries=["X11", "Xt"])
594\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000595
Greg Ward2afffd42000-08-06 20:37:24 +0000596(Again, this sort of non-portable construct should be avoided if you
597intend to distribute your code.)
598
Thomas Heller5f52f722001-02-19 17:48:03 +0000599\XXX{Should mention clib libraries here or somewhere else!}
600
601\subsubsection{Other options}
602
603There are still some other options which can be used to handle special
604cases.
605
606The \option{extra\_objects} option is a list of object files to be passed
607to the linker. These files must not have extensions, as the default
608extension for the compiler is used.
609
610\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000611to specify additional command line options for the respective compiler and
612linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000613
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000614\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000615of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000616is not needed when building compiled extensions: Distutils
617will automatically add \code{initmodule}
618to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000619
620\subsection{Listing scripts}
621So far we have been dealing with pure and non-pure Python modules,
622which are usually not run by themselves but imported by scripts.
623
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000624Scripts are files containing Python source code, indended to be
625started from the command line. Scripts don't require Distutils to do
626anything very complicated. The only clever feature is that if the
627first line of the script starts with \code{\#!} and contains the word
628``python'', the Distutils will adjust the first line to refer to the
629current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000630
631The \option{scripts} option simply is a list of files to be handled
632in this way.
633
634
635\subsection{Listing additional files}
Fred Drakea09262e2001-03-01 18:35:43 +0000636
Thomas Heller5f52f722001-02-19 17:48:03 +0000637The \option{data\_files} option can be used to specify additional
638files needed by the module distribution: configuration files,
639data files, anything which does not fit in the previous categories.
640
Fred Drake632bda32002-03-08 22:02:06 +0000641\option{data\_files} specifies a sequence of (\var{directory},
642\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000643
Thomas Heller5f52f722001-02-19 17:48:03 +0000644\begin{verbatim}
645setup(...
646 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
647 ('config', ['cfg/data.cfg'])])
648\end{verbatim}
649
650Note that you can specify the directory names where the data files
651will be installed, but you cannot rename the data files themselves.
652
Fred Drake632bda32002-03-08 22:02:06 +0000653Each (\var{directory}, \var{files}) pair in the sequence specifies the
654installation directory and the files to install there. If
655\var{directory} is a relative path, it is interpreted relative to the
656installation prefix (Python's \code{sys.prefix} for pure-Python
657packages, \code{sys.exec_prefix} for packages that contain extension
658modules). Each file name in \var{files} is interpreted relative to
659the \file{setup.py} script at the top of the package source
660distribution. No directory information from \var{files} is used to
661determine the final location of the installed file; only the name of
662the file is used.
663
Thomas Heller5f52f722001-02-19 17:48:03 +0000664You can specify the \option{data\_files} options as a simple sequence
665of files without specifying a target directory, but this is not recommended,
666and the \command{install} command will print a warning in this case.
667To install data files directly in the target directory, an empty
668string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000669
670
671\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000672\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000673
Greg Ward16aafcd2000-04-09 04:06:44 +0000674Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000675distribution \emph{a priori}: you may need to get some information from
676the user, or from the user's system, in order to proceed. As long as
677that information is fairly simple---a list of directories to search for
678C header files or libraries, for example---then providing a
679configuration file, \file{setup.cfg}, for users to edit is a cheap and
680easy way to solicit it. Configuration files also let you provide
681default values for any command option, which the installer can then
682override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000683
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000684% (If you have more advanced needs, such as determining which extensions
685% to build based on what capabilities are present on the target system,
686% then you need the Distutils ``auto-configuration'' facility. This
687% started to appear in Distutils 0.9 but, as of this writing, isn't mature
688% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000689
Greg Ward47f99a62000-09-04 20:07:15 +0000690The setup configuration file is a useful middle-ground between the setup
691script---which, ideally, would be opaque to installers\footnote{This
692 ideal probably won't be achieved until auto-configuration is fully
693 supported by the Distutils.}---and the command-line to the setup
694script, which is outside of your control and entirely up to the
695installer. In fact, \file{setup.cfg} (and any other Distutils
696configuration files present on the target system) are processed after
697the contents of the setup script, but before the command-line. This has
698several useful consequences:
699\begin{itemize}
700\item installers can override some of what you put in \file{setup.py} by
701 editing \file{setup.cfg}
702\item you can provide non-standard defaults for options that are not
703 easily set in \file{setup.py}
704\item installers can override anything in \file{setup.cfg} using the
705 command-line options to \file{setup.py}
706\end{itemize}
707
708The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000709
Greg Ward47f99a62000-09-04 20:07:15 +0000710\begin{verbatim}
711[command]
712option=value
713...
714\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000715
Greg Ward47f99a62000-09-04 20:07:15 +0000716where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000717\command{build\_py}, \command{install}), and \var{option} is one of
718the options that command supports. Any number of options can be
719supplied for each command, and any number of command sections can be
720included in the file. Blank lines are ignored, as are comments, which
721run from a \character{\#} character until the end of the line. Long
722option values can be split across multiple lines simply by indenting
723the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000724
725You can find out the list of options supported by a particular command
726with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000727
Greg Ward47f99a62000-09-04 20:07:15 +0000728\begin{verbatim}
729> python setup.py --help build_ext
730[...]
731Options for 'build_ext' command:
732 --build-lib (-b) directory for compiled extension modules
733 --build-temp (-t) directory for temporary files (build by-products)
734 --inplace (-i) ignore build-lib and put compiled extensions into the
735 source directory alongside your pure Python modules
736 --include-dirs (-I) list of directories to search for header files
737 --define (-D) C preprocessor macros to define
738 --undef (-U) C preprocessor macros to undefine
739[...]
740\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000741
Greg Ward47f99a62000-09-04 20:07:15 +0000742Or consult section \ref{reference} of this document (the command
743reference).
744
745Note that an option spelled \longprogramopt{foo-bar} on the command-line
746is spelled \option{foo\_bar} in configuration files.
747
748For example, say you want your extensions to be built
749``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000750want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000751in the same source directory as your pure Python modules
752\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
753\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000754
Greg Ward47f99a62000-09-04 20:07:15 +0000755\begin{verbatim}
756python setup.py build_ext --inplace
757\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000758
Greg Ward47f99a62000-09-04 20:07:15 +0000759But this requires that you always specify the \command{build\_ext}
760command explicitly, and remember to provide \longprogramopt{inplace}.
761An easier way is to ``set and forget'' this option, by encoding it in
762\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000763
Greg Ward47f99a62000-09-04 20:07:15 +0000764\begin{verbatim}
765[build_ext]
766inplace=1
767\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000768
Greg Ward47f99a62000-09-04 20:07:15 +0000769This will affect all builds of this module distribution, whether or not
770you explcitly specify \command{build\_ext}. If you include
771\file{setup.cfg} in your source distribution, it will also affect
772end-user builds---which is probably a bad idea for this option, since
773always building extensions in-place would break installation of the
774module distribution. In certain peculiar cases, though, modules are
775built right in their installation directory, so this is conceivably a
776useful ability. (Distributing extensions that expect to be built in
777their installation directory is almost always a bad idea, though.)
778
779Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000780change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000781everything required to generate a ``spec'' file for creating an RPM
782distribution. Some of this information comes from the setup script, and
783some is automatically generated by the Distutils (such as the list of
784files installed). But some of it has to be supplied as options to
785\command{bdist\_rpm}, which would be very tedious to do on the
786command-line for every run. Hence, here is a snippet from the
787Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000788
Greg Ward47f99a62000-09-04 20:07:15 +0000789\begin{verbatim}
790[bdist_rpm]
791release = 1
792packager = Greg Ward <gward@python.net>
793doc_files = CHANGES.txt
794 README.txt
795 USAGE.txt
796 doc/
797 examples/
798\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000799
Greg Ward47f99a62000-09-04 20:07:15 +0000800Note that the \option{doc\_files} option is simply a
801whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000802
803
Fred Drakea09262e2001-03-01 18:35:43 +0000804\begin{seealso}
805 \seetitle[../inst/config-syntax.html]{Installing Python
806 Modules}{More information on the configuration files is
807 available in the manual for system administrators.}
808\end{seealso}
809
810
Greg Ward16aafcd2000-04-09 04:06:44 +0000811\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000812\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000813
Greg Warde78298a2000-04-28 17:12:24 +0000814As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000815\command{sdist} command to create a source distribution. In the
816simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000817
Greg Ward16aafcd2000-04-09 04:06:44 +0000818\begin{verbatim}
819python setup.py sdist
820\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000821
Greg Ward19c67f82000-06-24 01:33:16 +0000822(assuming you haven't specified any \command{sdist} options in the setup
823script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000824default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000825tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
826\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000827
Greg Wardd5767a52000-04-19 22:48:09 +0000828You can specify as many formats as you like using the
829\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000830
Greg Ward16aafcd2000-04-09 04:06:44 +0000831\begin{verbatim}
832python setup.py sdist --formats=gztar,zip
833\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000834
Greg Ward16aafcd2000-04-09 04:06:44 +0000835to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000836\begin{tableiii}{l|l|c}{code}%
837 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000838 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
839 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000840 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000841 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000842 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000843\end{tableiii}
844
845\noindent Notes:
846\begin{description}
847\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000848\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000849\item[(3)] requires either external \program{zip} utility or
850 \module{zipfile} module (not part of the standard Python library)
Greg Ward47f99a62000-09-04 20:07:15 +0000851\item[(4)] requires external utilities: \program{tar} and possibly one
852 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000853\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000854
855
Greg Ward54589d42000-09-06 01:37:35 +0000856
857\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000858\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000859
Greg Ward54589d42000-09-06 01:37:35 +0000860If you don't supply an explicit list of files (or instructions on how to
861generate one), the \command{sdist} command puts a minimal default set
862into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000863\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000864\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000865 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000866\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000867 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000868 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000869\item anything that looks like a test script: \file{test/test*.py}
870 (currently, the Distutils don't do anything with test scripts except
871 include them in source distributions, but in the future there will be
872 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000873\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
874 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000875\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000876
Greg Ward16aafcd2000-04-09 04:06:44 +0000877Sometimes this is enough, but usually you will want to specify
878additional files to distribute. The typical way to do this is to write
879a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000880manifest template is just a list of instructions for how to generate
881your manifest file, \file{MANIFEST}, which is the exact list of files to
882include in your source distribution. The \command{sdist} command
883processes this template and generates a manifest based on its
884instructions and what it finds in the filesystem.
885
886If you prefer to roll your own manifest file, the format is simple: one
887filename per line, regular files (or symlinks to them) only. If you do
888supply your own \file{MANIFEST}, you must specify everything: the
889default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000890
891The manifest template has one command per line, where each command
892specifies a set of files to include or exclude from the source
893distribution. For an example, again we turn to the Distutils' own
894manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +0000895
Greg Ward16aafcd2000-04-09 04:06:44 +0000896\begin{verbatim}
897include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000898recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000899prune examples/sample?/build
900\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000901
Greg Ward16aafcd2000-04-09 04:06:44 +0000902The meanings should be fairly clear: include all files in the
903distribution root matching \code{*.txt}, all files anywhere under the
904\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000905exclude all directories matching \code{examples/sample?/build}. All of
906this is done \emph{after} the standard include set, so you can exclude
907files from the standard set with explicit instructions in the manifest
908template. (Or, you can use the \longprogramopt{no-defaults} option to
909disable the standard set entirely.) There are several other commands
910available in the manifest template mini-language; see
911section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000912
Greg Ward54589d42000-09-06 01:37:35 +0000913The order of commands in the manifest template matters: initially, we
914have the list of default files as described above, and each command in
915the template adds to or removes from that list of files. Once we have
916fully processed the manifest template, we remove files that should not
917be included in the source distribution:
918\begin{itemize}
919\item all files in the Distutils ``build'' tree (default \file{build/})
920\item all files in directories named \file{RCS} or \file{CVS}
921\end{itemize}
922Now we have our complete list of files, which is written to the manifest
923for future reference, and then used to build the source distribution
924archive(s).
925
926You can disable the default set of included files with the
927\longprogramopt{no-defaults} option, and you can disable the standard
928exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000929
Greg Ward46b98e32000-04-14 01:53:36 +0000930Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000931\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000932Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000933\begin{enumerate}
934\item include all Python source files in the \file{distutils} and
935 \file{distutils/command} subdirectories (because packages
936 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000937 \option{packages} option in the setup script---see
938 section~\ref{setup-script})
939\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
940 (standard files)
941\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000942\item include \file{*.txt} in the distribution root (this will find
943 \file{README.txt} a second time, but such redundancies are weeded out
944 later)
Greg Ward54589d42000-09-06 01:37:35 +0000945\item include anything matching \file{*.txt} or \file{*.py} in the
946 sub-tree under \file{examples},
947\item exclude all files in the sub-trees starting at directories
948 matching \file{examples/sample?/build}---this may exclude files
949 included by the previous two steps, so it's important that the
950 \code{prune} command in the manifest template comes after the
951 \code{recursive-include} command
952\item exclude the entire \file{build} tree, and any \file{RCS} or
953 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000954\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000955Just like in the setup script, file and directory names in the manifest
956template should always be slash-separated; the Distutils will take care
957of converting them to the standard representation on your platform.
958That way, the manifest template is portable across operating systems.
959
Greg Ward16aafcd2000-04-09 04:06:44 +0000960
961\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000962\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000963
964The normal course of operations for the \command{sdist} command is as
965follows:
966\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000967\item if the manifest file, \file{MANIFEST} doesn't exist, read
968 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000969\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000970 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +0000971\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
972 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
973 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000974\item use the list of files now in \file{MANIFEST} (either just
975 generated or read in) to create the source distribution archive(s)
976\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000977There are a couple of options that modify this behaviour. First, use
978the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000979disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +0000980
Greg Ward54589d42000-09-06 01:37:35 +0000981Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000982example, if you have added or removed files or directories that match an
983existing pattern in the manifest template, you should regenerate the
984manifest:
Fred Drakea09262e2001-03-01 18:35:43 +0000985
Greg Ward16aafcd2000-04-09 04:06:44 +0000986\begin{verbatim}
987python setup.py sdist --force-manifest
988\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000989
990Or, you might just want to (re)generate the manifest, but not create a
991source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000992
Greg Ward16aafcd2000-04-09 04:06:44 +0000993\begin{verbatim}
994python setup.py sdist --manifest-only
995\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000996
Greg Ward54589d42000-09-06 01:37:35 +0000997\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
998\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
999\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001000
1001
1002\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001003\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001004
Greg Ward46b98e32000-04-14 01:53:36 +00001005A ``built distribution'' is what you're probably used to thinking of
1006either as a ``binary package'' or an ``installer'' (depending on your
1007background). It's not necessarily binary, though, because it might
1008contain only Python source code and/or byte-code; and we don't call it a
1009package, because that word is already spoken for in Python. (And
1010``installer'' is a term specific to the Windows world. \XXX{do Mac
1011 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001012
Greg Ward46b98e32000-04-14 01:53:36 +00001013A built distribution is how you make life as easy as possible for
1014installers of your module distribution: for users of RPM-based Linux
1015systems, it's a binary RPM; for Windows users, it's an executable
1016installer; for Debian-based Linux users, it's a Debian package; and so
1017forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001018distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001019designed to enable module developers to concentrate on their
1020specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001021intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001022distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001023are packagers.
1024
1025Of course, the module developer could be his own packager; or the
1026packager could be a volunteer ``out there'' somewhere who has access to
1027a platform which the original developer does not; or it could be
1028software periodically grabbing new source distributions and turning them
1029into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001030access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001031setup script and the \command{bdist} command family to generate built
1032distributions.
1033
1034As a simple example, if I run the following command in the Distutils
1035source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001036
Greg Ward46b98e32000-04-14 01:53:36 +00001037\begin{verbatim}
1038python setup.py bdist
1039\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001040
Greg Ward46b98e32000-04-14 01:53:36 +00001041then the Distutils builds my module distribution (the Distutils itself
1042in this case), does a ``fake'' installation (also in the \file{build}
1043directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001044platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001045file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001046file is considered ``dumb'' because it has to be unpacked in a specific
1047location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001048
Fred Drakeeff9a872000-10-26 16:41:03 +00001049Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001050\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001051from the right place installs the Distutils just as though you had
1052downloaded the source distribution and run \code{python setup.py
1053 install}. (The ``right place'' is either the root of the filesystem or
1054Python's \filevar{prefix} directory, depending on the options given to
1055the \command{bdist\_dumb} command; the default is to make dumb
1056distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001057
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001058Obviously, for pure Python distributions, this isn't any simpler than
1059just running \code{python setup.py install}---but for non-pure
1060distributions, which include extensions that would need to be
1061compiled, it can mean the difference between someone being able to use
1062your extensions or not. And creating ``smart'' built distributions,
1063such as an RPM package or an executable installer for Windows, is far
1064more convenient for users even if your distribution doesn't include
1065any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001066
Greg Wardb6528972000-09-07 02:40:37 +00001067The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001068similar to the \command{sdist} command, which you can use to select the
1069types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001070
Greg Ward46b98e32000-04-14 01:53:36 +00001071\begin{verbatim}
1072python setup.py bdist --format=zip
1073\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001074
Fred Drakeeff9a872000-10-26 16:41:03 +00001075would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001076\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001077unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001078
1079The available formats for built distributions are:
1080\begin{tableiii}{l|l|c}{code}%
1081 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001082 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1083 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1084 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1085 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1086 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001087 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1088 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1089 \lineiii{rpm}{RPM}{(5)}
1090% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001091 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001092\end{tableiii}
1093
1094\noindent Notes:
1095\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001096\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001097\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001098\item[(3)] requires external utilities: \program{tar} and possibly one
1099 of \program{gzip}, \program{bzip2}, or \program{compress}
1100\item[(4)] requires either external \program{zip} utility or
1101 \module{zipfile} module (not part of the standard Python library)
1102\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1103 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001104\end{description}
1105
1106You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001107\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001108directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001109\command{bdist} ``sub-commands'' actually generate several similar
1110formats; for instance, the \command{bdist\_dumb} command generates all
1111the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1112\code{zip}), and \command{bdist\_rpm} generates both binary and source
1113RPMs. The \command{bdist} sub-commands, and the formats generated by
1114each, are:
1115\begin{tableii}{l|l}{command}%
1116 {Command}{Formats}
1117 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1118 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001119 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001120\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001121
Greg Wardb6528972000-09-07 02:40:37 +00001122The following sections give details on the individual \command{bdist\_*}
1123commands.
1124
1125
1126\subsection{Creating dumb built distributions}
1127\label{creating-dumb}
1128
1129\XXX{Need to document absolute vs. prefix-relative packages here, but
1130 first I have to implement it!}
1131
1132
1133\subsection{Creating RPM packages}
1134\label{creating-rpms}
1135
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001136The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001137Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1138RPM-based Linux distributions) is your usual environment, creating RPM
1139packages for other users of that same distribution is trivial.
1140Depending on the complexity of your module distribution and differences
1141between Linux distributions, you may also be able to create RPMs that
1142work on different RPM-based distributions.
1143
1144The usual way to create an RPM of your module distribution is to run the
1145\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001146
Greg Wardb6528972000-09-07 02:40:37 +00001147\begin{verbatim}
1148python setup.py bdist_rpm
1149\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001150
Greg Wardb6528972000-09-07 02:40:37 +00001151or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001152
Greg Wardb6528972000-09-07 02:40:37 +00001153\begin{verbatim}
1154python setup.py bdist --formats=rpm
1155\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001156
Greg Wardb6528972000-09-07 02:40:37 +00001157The former allows you to specify RPM-specific options; the latter allows
1158you to easily specify multiple formats in one run. If you need to do
1159both, you can explicitly specify multiple \command{bdist\_*} commands
1160and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001161
Greg Wardb6528972000-09-07 02:40:37 +00001162\begin{verbatim}
1163python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1164 bdist_wininst --target_version="2.0"
1165\end{verbatim}
1166
1167Creating RPM packages is driven by a \file{.spec} file, much as using
1168the Distutils is driven by the setup script. To make your life easier,
1169the \command{bdist\_rpm} command normally creates a \file{.spec} file
1170based on the information you supply in the setup script, on the command
1171line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001172sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001173script as follows:
1174\begin{tableii}{l|l}{textrm}%
1175 {RPM \file{.spec} file option or section}{Distutils setup script option}
1176 \lineii{Name}{\option{name}}
1177 \lineii{Summary (in preamble)}{\option{description}}
1178 \lineii{Version}{\option{version}}
1179 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1180 \option{maintainer} and \option{maintainer\_email}}
1181 \lineii{Copyright}{\option{licence}}
1182 \lineii{Url}{\option{url}}
1183 \lineii{\%description (section)}{\option{long\_description}}
1184\end{tableii}
1185
1186Additionally, there many options in \file{.spec} files that don't have
1187corresponding options in the setup script. Most of these are handled
1188through options to the \command{bdist\_rpm} command as follows:
1189\begin{tableiii}{l|l|l}{textrm}%
1190 {RPM \file{.spec} file option or section}%
1191 {\command{bdist\_rpm} option}%
1192 {default value}
1193 \lineiii{Release}{\option{release}}{``1''}
1194 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1195 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001196 \lineiii{Packager}{\option{packager}}{(none)}
1197 \lineiii{Provides}{\option{provides}}{(none)}
1198 \lineiii{Requires}{\option{requires}}{(none)}
1199 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1200 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001201 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1202 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1203 \lineiii{Icon}{\option{icon}}{(none)}
1204\end{tableiii}
1205Obviously, supplying even a few of these options on the command-line
1206would be tedious and error-prone, so it's usually best to put them in
1207the setup configuration file, \file{setup.cfg}---see
1208section~\ref{setup-config}. If you distribute or package many Python
1209module distributions, you might want to put options that apply to all of
1210them in your personal Distutils configuration file
1211(\file{\textasciitilde/.pydistutils.cfg}).
1212
1213There are three steps to building a binary RPM package, all of which are
1214handled automatically by the Distutils:
1215\begin{enumerate}
1216\item create a \file{.spec} file, which describes the package (analogous
1217 to the Distutils setup script; in fact, much of the information in the
1218 setup script winds up in the \file{.spec} file)
1219\item create the source RPM
1220\item create the ``binary'' RPM (which may or may not contain binary
1221 code, depending on whether your module distribution contains Python
1222 extensions)
1223\end{enumerate}
1224Normally, RPM bundles the last two steps together; when you use the
1225Distutils, all three steps are typically bundled together.
1226
1227If you wish, you can separate these three steps. You can use the
1228\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1229create the \file{.spec} file and exit; in this case, the \file{.spec}
1230file will be written to the ``distribution directory''---normally
1231\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1232option. (Normally, the \file{.spec} file winds up deep in the ``build
1233tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1234
1235\XXX{this isn't implemented yet---is it needed?!}
1236You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001237\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001238\longprogramopt{spec-only}, this gives you an opportunity to customize
1239the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001240
Greg Wardb6528972000-09-07 02:40:37 +00001241\begin{verbatim}
1242> python setup.py bdist_rpm --spec-only
1243# ...edit dist/FooBar-1.0.spec
1244> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1245\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001246
Greg Wardb6528972000-09-07 02:40:37 +00001247(Although a better way to do this is probably to override the standard
1248\command{bdist\_rpm} command with one that writes whatever else you want
1249to the \file{.spec} file; see section~\ref{extending} for information on
1250extending the Distutils.)
1251
1252
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001253\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001254\label{creating-wininst}
1255
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001256Executable installers are the natural format for binary
Fred Drake17f690f2001-07-14 02:14:42 +00001257distributions on Windows. They display a nice graphical user interface,
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001258display some information about the module distribution to be installed taken
Fred Drake457c4192001-08-16 21:25:24 +00001259from the meta-data in the setup script, let the user select a few
Thomas Heller5f52f722001-02-19 17:48:03 +00001260(currently maybe too few) options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001261
Thomas Heller5f52f722001-02-19 17:48:03 +00001262Since the meta-data is taken from the setup script, creating
1263Windows installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001264
Thomas Heller5f52f722001-02-19 17:48:03 +00001265\begin{verbatim}
1266python setup.py bdist_wininst
1267\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001268
Thomas Heller5f52f722001-02-19 17:48:03 +00001269or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001270
Thomas Heller5f52f722001-02-19 17:48:03 +00001271\begin{verbatim}
1272python setup.py bdist --formats=wininst
1273\end{verbatim}
1274
1275If you have a pure module distribution (only containing pure
1276Python modules and packages), the resulting installer will be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001277version independent and have a name like \file{foo-1.0.win32.exe}.
Thomas Heller5f52f722001-02-19 17:48:03 +00001278These installers can even be created on \UNIX{} or MacOS platforms.
1279
1280If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001281created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001282The installer filename will reflect this and now has the form
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001283\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001284for every Python version you want to support.
1285
1286The installer will try to compile pure modules into bytecode after
1287installation on the target system in normal and optimizing mode.
1288If you don't want this to happen for some reason, you can run
1289the bdist_wininst command with the \longprogramopt{no-target-compile} and/or
1290the \longprogramopt{no-target-optimize} option.
Greg Wardb6528972000-09-07 02:40:37 +00001291
Fred Drakea09262e2001-03-01 18:35:43 +00001292%\section{Examples}
1293%\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001294
1295
Fred Drakea09262e2001-03-01 18:35:43 +00001296%\subsection{Pure Python distribution (by module)}
1297%\label{pure-mod}
Greg Ward16aafcd2000-04-09 04:06:44 +00001298
1299
Fred Drakea09262e2001-03-01 18:35:43 +00001300%\subsection{Pure Python distribution (by package)}
1301%\label{pure-pkg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001302
1303
Fred Drakea09262e2001-03-01 18:35:43 +00001304%\subsection{Single extension module}
1305%\label{single-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001306
1307
Fred Drakea09262e2001-03-01 18:35:43 +00001308%\subsection{Multiple extension modules}
1309%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001310
1311
Fred Drakea09262e2001-03-01 18:35:43 +00001312%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001313
1314
Greg Ward4a9e7222000-04-25 02:57:36 +00001315
Fred Drakea09262e2001-03-01 18:35:43 +00001316%\section{Extending the Distutils}
1317%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001318
1319
Fred Drakea09262e2001-03-01 18:35:43 +00001320%\subsection{Extending existing commands}
1321%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001322
1323
Fred Drakea09262e2001-03-01 18:35:43 +00001324%\subsection{Writing new commands}
1325%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001326
Fred Drakea09262e2001-03-01 18:35:43 +00001327%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001328
Greg Ward4a9e7222000-04-25 02:57:36 +00001329
1330
Greg Ward16aafcd2000-04-09 04:06:44 +00001331\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001332\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001333
1334
Fred Drakea09262e2001-03-01 18:35:43 +00001335%\subsection{Building modules: the \protect\command{build} command family}
1336%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001337
Fred Drakea09262e2001-03-01 18:35:43 +00001338%\subsubsection{\protect\command{build}}
1339%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001340
Fred Drakea09262e2001-03-01 18:35:43 +00001341%\subsubsection{\protect\command{build\_py}}
1342%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001343
Fred Drakea09262e2001-03-01 18:35:43 +00001344%\subsubsection{\protect\command{build\_ext}}
1345%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001346
Fred Drakea09262e2001-03-01 18:35:43 +00001347%\subsubsection{\protect\command{build\_clib}}
1348%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001349
1350
Greg Wardfacb8db2000-04-09 04:32:40 +00001351\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001352\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001353
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001354The install command ensures that the build commands have been run and then
1355runs the subcommands \command{install\_lib},
1356\command{install\_data} and
1357\command{install\_scripts}.
1358
Fred Drakea09262e2001-03-01 18:35:43 +00001359%\subsubsection{\protect\command{install\_lib}}
1360%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001361
1362\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001363\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001364This command installs all data files provided with the distribution.
1365
1366\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001367\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001368This command installs all (Python) scripts in the distribution.
1369
Greg Ward16aafcd2000-04-09 04:06:44 +00001370
Fred Drakea09262e2001-03-01 18:35:43 +00001371%\subsection{Cleaning up: the \protect\command{clean} command}
1372%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001373
1374
Fred Drakeeff9a872000-10-26 16:41:03 +00001375\subsection{Creating a source distribution: the
1376 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001377\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001378
1379
1380\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001381
Greg Ward16aafcd2000-04-09 04:06:44 +00001382The manifest template commands are:
1383\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001384 \lineii{include \var{pat1} \var{pat2} ... }
1385 {include all files matching any of the listed patterns}
1386 \lineii{exclude \var{pat1} \var{pat2} ... }
1387 {exclude all files matching any of the listed patterns}
1388 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1389 {include all files under \var{dir} matching any of the listed patterns}
1390 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1391 {exclude all files under \var{dir} matching any of the listed patterns}
1392 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001393 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001394 any of the listed patterns}
1395 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001396 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001397 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001398 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1399 \lineii{graft \var{dir}}{include all files under \var{dir}}
1400\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001401The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001402sequence of regular filename characters, \code{?} matches any single
1403regular filename character, and \code{[\var{range}]} matches any of the
1404characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001405\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001406platform-specific: on \UNIX{} it is anything except slash; on Windows
1407anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001408
Fred Drakeeff9a872000-10-26 16:41:03 +00001409\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001410
1411
Fred Drakea09262e2001-03-01 18:35:43 +00001412%\subsection{Creating a built distribution: the
1413% \protect\command{bdist} command family}
1414%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001415
1416
Fred Drakeab70b382001-08-02 15:13:15 +00001417%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001418
Fred Drakeab70b382001-08-02 15:13:15 +00001419%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001420
Fred Drakeab70b382001-08-02 15:13:15 +00001421%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001422
Fred Drakeab70b382001-08-02 15:13:15 +00001423%\subsubsection{\protect\command{bdist\_wininst}}
1424
1425
1426\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001427
1428
Greg Wardabc52162000-02-26 00:52:48 +00001429\end{document}