blob: c6acc0492f694a62c822f48b98f25945141c085d [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
Greg Ward954ce8b2002-05-10 14:42:10 +0000850 \module{zipfile} module (part of the standard Python library since
851 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000852\item[(4)] requires external utilities: \program{tar} and possibly one
853 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000854\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000855
856
Greg Ward54589d42000-09-06 01:37:35 +0000857
858\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000859\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000860
Greg Ward54589d42000-09-06 01:37:35 +0000861If you don't supply an explicit list of files (or instructions on how to
862generate one), the \command{sdist} command puts a minimal default set
863into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000864\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +0000865\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +0000866 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +0000867\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +0000868 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +0000869 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +0000870\item anything that looks like a test script: \file{test/test*.py}
871 (currently, the Distutils don't do anything with test scripts except
872 include them in source distributions, but in the future there will be
873 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +0000874\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
875 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +0000876\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000877
Greg Ward16aafcd2000-04-09 04:06:44 +0000878Sometimes this is enough, but usually you will want to specify
879additional files to distribute. The typical way to do this is to write
880a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +0000881manifest template is just a list of instructions for how to generate
882your manifest file, \file{MANIFEST}, which is the exact list of files to
883include in your source distribution. The \command{sdist} command
884processes this template and generates a manifest based on its
885instructions and what it finds in the filesystem.
886
887If you prefer to roll your own manifest file, the format is simple: one
888filename per line, regular files (or symlinks to them) only. If you do
889supply your own \file{MANIFEST}, you must specify everything: the
890default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +0000891
892The manifest template has one command per line, where each command
893specifies a set of files to include or exclude from the source
894distribution. For an example, again we turn to the Distutils' own
895manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +0000896
Greg Ward16aafcd2000-04-09 04:06:44 +0000897\begin{verbatim}
898include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +0000899recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +0000900prune examples/sample?/build
901\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000902
Greg Ward16aafcd2000-04-09 04:06:44 +0000903The meanings should be fairly clear: include all files in the
904distribution root matching \code{*.txt}, all files anywhere under the
905\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +0000906exclude all directories matching \code{examples/sample?/build}. All of
907this is done \emph{after} the standard include set, so you can exclude
908files from the standard set with explicit instructions in the manifest
909template. (Or, you can use the \longprogramopt{no-defaults} option to
910disable the standard set entirely.) There are several other commands
911available in the manifest template mini-language; see
912section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000913
Greg Ward54589d42000-09-06 01:37:35 +0000914The order of commands in the manifest template matters: initially, we
915have the list of default files as described above, and each command in
916the template adds to or removes from that list of files. Once we have
917fully processed the manifest template, we remove files that should not
918be included in the source distribution:
919\begin{itemize}
920\item all files in the Distutils ``build'' tree (default \file{build/})
921\item all files in directories named \file{RCS} or \file{CVS}
922\end{itemize}
923Now we have our complete list of files, which is written to the manifest
924for future reference, and then used to build the source distribution
925archive(s).
926
927You can disable the default set of included files with the
928\longprogramopt{no-defaults} option, and you can disable the standard
929exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000930
Greg Ward46b98e32000-04-14 01:53:36 +0000931Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +0000932\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +0000933Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +0000934\begin{enumerate}
935\item include all Python source files in the \file{distutils} and
936 \file{distutils/command} subdirectories (because packages
937 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +0000938 \option{packages} option in the setup script---see
939 section~\ref{setup-script})
940\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
941 (standard files)
942\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +0000943\item include \file{*.txt} in the distribution root (this will find
944 \file{README.txt} a second time, but such redundancies are weeded out
945 later)
Greg Ward54589d42000-09-06 01:37:35 +0000946\item include anything matching \file{*.txt} or \file{*.py} in the
947 sub-tree under \file{examples},
948\item exclude all files in the sub-trees starting at directories
949 matching \file{examples/sample?/build}---this may exclude files
950 included by the previous two steps, so it's important that the
951 \code{prune} command in the manifest template comes after the
952 \code{recursive-include} command
953\item exclude the entire \file{build} tree, and any \file{RCS} or
954 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +0000955\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +0000956Just like in the setup script, file and directory names in the manifest
957template should always be slash-separated; the Distutils will take care
958of converting them to the standard representation on your platform.
959That way, the manifest template is portable across operating systems.
960
Greg Ward16aafcd2000-04-09 04:06:44 +0000961
962\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +0000963\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +0000964
965The normal course of operations for the \command{sdist} command is as
966follows:
967\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +0000968\item if the manifest file, \file{MANIFEST} doesn't exist, read
969 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +0000970\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000971 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +0000972\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
973 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
974 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +0000975\item use the list of files now in \file{MANIFEST} (either just
976 generated or read in) to create the source distribution archive(s)
977\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +0000978There are a couple of options that modify this behaviour. First, use
979the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000980disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +0000981
Greg Ward54589d42000-09-06 01:37:35 +0000982Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +0000983example, if you have added or removed files or directories that match an
984existing pattern in the manifest template, you should regenerate the
985manifest:
Fred Drakea09262e2001-03-01 18:35:43 +0000986
Greg Ward16aafcd2000-04-09 04:06:44 +0000987\begin{verbatim}
988python setup.py sdist --force-manifest
989\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000990
991Or, you might just want to (re)generate the manifest, but not create a
992source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000993
Greg Ward16aafcd2000-04-09 04:06:44 +0000994\begin{verbatim}
995python setup.py sdist --manifest-only
996\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000997
Greg Ward54589d42000-09-06 01:37:35 +0000998\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
999\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1000\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001001
1002
1003\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001004\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001005
Greg Ward46b98e32000-04-14 01:53:36 +00001006A ``built distribution'' is what you're probably used to thinking of
1007either as a ``binary package'' or an ``installer'' (depending on your
1008background). It's not necessarily binary, though, because it might
1009contain only Python source code and/or byte-code; and we don't call it a
1010package, because that word is already spoken for in Python. (And
1011``installer'' is a term specific to the Windows world. \XXX{do Mac
1012 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001013
Greg Ward46b98e32000-04-14 01:53:36 +00001014A built distribution is how you make life as easy as possible for
1015installers of your module distribution: for users of RPM-based Linux
1016systems, it's a binary RPM; for Windows users, it's an executable
1017installer; for Debian-based Linux users, it's a Debian package; and so
1018forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001019distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001020designed to enable module developers to concentrate on their
1021specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001022intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001023distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001024are packagers.
1025
1026Of course, the module developer could be his own packager; or the
1027packager could be a volunteer ``out there'' somewhere who has access to
1028a platform which the original developer does not; or it could be
1029software periodically grabbing new source distributions and turning them
1030into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001031access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001032setup script and the \command{bdist} command family to generate built
1033distributions.
1034
1035As a simple example, if I run the following command in the Distutils
1036source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001037
Greg Ward46b98e32000-04-14 01:53:36 +00001038\begin{verbatim}
1039python setup.py bdist
1040\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001041
Greg Ward46b98e32000-04-14 01:53:36 +00001042then the Distutils builds my module distribution (the Distutils itself
1043in this case), does a ``fake'' installation (also in the \file{build}
1044directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001045platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001046file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001047file is considered ``dumb'' because it has to be unpacked in a specific
1048location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001049
Fred Drakeeff9a872000-10-26 16:41:03 +00001050Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001051\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001052from the right place installs the Distutils just as though you had
1053downloaded the source distribution and run \code{python setup.py
1054 install}. (The ``right place'' is either the root of the filesystem or
1055Python's \filevar{prefix} directory, depending on the options given to
1056the \command{bdist\_dumb} command; the default is to make dumb
1057distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001058
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001059Obviously, for pure Python distributions, this isn't any simpler than
1060just running \code{python setup.py install}---but for non-pure
1061distributions, which include extensions that would need to be
1062compiled, it can mean the difference between someone being able to use
1063your extensions or not. And creating ``smart'' built distributions,
1064such as an RPM package or an executable installer for Windows, is far
1065more convenient for users even if your distribution doesn't include
1066any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001067
Greg Wardb6528972000-09-07 02:40:37 +00001068The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001069similar to the \command{sdist} command, which you can use to select the
1070types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001071
Greg Ward46b98e32000-04-14 01:53:36 +00001072\begin{verbatim}
1073python setup.py bdist --format=zip
1074\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001075
Fred Drakeeff9a872000-10-26 16:41:03 +00001076would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001077\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001078unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001079
1080The available formats for built distributions are:
1081\begin{tableiii}{l|l|c}{code}%
1082 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001083 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1084 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1085 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1086 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1087 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001088 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1089 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1090 \lineiii{rpm}{RPM}{(5)}
1091% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001092 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001093\end{tableiii}
1094
1095\noindent Notes:
1096\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001097\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001098\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001099\item[(3)] requires external utilities: \program{tar} and possibly one
1100 of \program{gzip}, \program{bzip2}, or \program{compress}
1101\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001102 \module{zipfile} module (part of the standard Python library since
1103 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001104\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1105 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001106\end{description}
1107
1108You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001109\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001110directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001111\command{bdist} ``sub-commands'' actually generate several similar
1112formats; for instance, the \command{bdist\_dumb} command generates all
1113the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1114\code{zip}), and \command{bdist\_rpm} generates both binary and source
1115RPMs. The \command{bdist} sub-commands, and the formats generated by
1116each, are:
1117\begin{tableii}{l|l}{command}%
1118 {Command}{Formats}
1119 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1120 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001121 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001122\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001123
Greg Wardb6528972000-09-07 02:40:37 +00001124The following sections give details on the individual \command{bdist\_*}
1125commands.
1126
1127
1128\subsection{Creating dumb built distributions}
1129\label{creating-dumb}
1130
1131\XXX{Need to document absolute vs. prefix-relative packages here, but
1132 first I have to implement it!}
1133
1134
1135\subsection{Creating RPM packages}
1136\label{creating-rpms}
1137
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001138The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001139Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1140RPM-based Linux distributions) is your usual environment, creating RPM
1141packages for other users of that same distribution is trivial.
1142Depending on the complexity of your module distribution and differences
1143between Linux distributions, you may also be able to create RPMs that
1144work on different RPM-based distributions.
1145
1146The usual way to create an RPM of your module distribution is to run the
1147\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001148
Greg Wardb6528972000-09-07 02:40:37 +00001149\begin{verbatim}
1150python setup.py bdist_rpm
1151\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001152
Greg Wardb6528972000-09-07 02:40:37 +00001153or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001154
Greg Wardb6528972000-09-07 02:40:37 +00001155\begin{verbatim}
1156python setup.py bdist --formats=rpm
1157\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001158
Greg Wardb6528972000-09-07 02:40:37 +00001159The former allows you to specify RPM-specific options; the latter allows
1160you to easily specify multiple formats in one run. If you need to do
1161both, you can explicitly specify multiple \command{bdist\_*} commands
1162and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001163
Greg Wardb6528972000-09-07 02:40:37 +00001164\begin{verbatim}
1165python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1166 bdist_wininst --target_version="2.0"
1167\end{verbatim}
1168
1169Creating RPM packages is driven by a \file{.spec} file, much as using
1170the Distutils is driven by the setup script. To make your life easier,
1171the \command{bdist\_rpm} command normally creates a \file{.spec} file
1172based on the information you supply in the setup script, on the command
1173line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001174sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001175script as follows:
1176\begin{tableii}{l|l}{textrm}%
1177 {RPM \file{.spec} file option or section}{Distutils setup script option}
1178 \lineii{Name}{\option{name}}
1179 \lineii{Summary (in preamble)}{\option{description}}
1180 \lineii{Version}{\option{version}}
1181 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1182 \option{maintainer} and \option{maintainer\_email}}
1183 \lineii{Copyright}{\option{licence}}
1184 \lineii{Url}{\option{url}}
1185 \lineii{\%description (section)}{\option{long\_description}}
1186\end{tableii}
1187
1188Additionally, there many options in \file{.spec} files that don't have
1189corresponding options in the setup script. Most of these are handled
1190through options to the \command{bdist\_rpm} command as follows:
1191\begin{tableiii}{l|l|l}{textrm}%
1192 {RPM \file{.spec} file option or section}%
1193 {\command{bdist\_rpm} option}%
1194 {default value}
1195 \lineiii{Release}{\option{release}}{``1''}
1196 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1197 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001198 \lineiii{Packager}{\option{packager}}{(none)}
1199 \lineiii{Provides}{\option{provides}}{(none)}
1200 \lineiii{Requires}{\option{requires}}{(none)}
1201 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1202 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001203 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1204 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1205 \lineiii{Icon}{\option{icon}}{(none)}
1206\end{tableiii}
1207Obviously, supplying even a few of these options on the command-line
1208would be tedious and error-prone, so it's usually best to put them in
1209the setup configuration file, \file{setup.cfg}---see
1210section~\ref{setup-config}. If you distribute or package many Python
1211module distributions, you might want to put options that apply to all of
1212them in your personal Distutils configuration file
1213(\file{\textasciitilde/.pydistutils.cfg}).
1214
1215There are three steps to building a binary RPM package, all of which are
1216handled automatically by the Distutils:
1217\begin{enumerate}
1218\item create a \file{.spec} file, which describes the package (analogous
1219 to the Distutils setup script; in fact, much of the information in the
1220 setup script winds up in the \file{.spec} file)
1221\item create the source RPM
1222\item create the ``binary'' RPM (which may or may not contain binary
1223 code, depending on whether your module distribution contains Python
1224 extensions)
1225\end{enumerate}
1226Normally, RPM bundles the last two steps together; when you use the
1227Distutils, all three steps are typically bundled together.
1228
1229If you wish, you can separate these three steps. You can use the
1230\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1231create the \file{.spec} file and exit; in this case, the \file{.spec}
1232file will be written to the ``distribution directory''---normally
1233\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1234option. (Normally, the \file{.spec} file winds up deep in the ``build
1235tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1236
1237\XXX{this isn't implemented yet---is it needed?!}
1238You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001239\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001240\longprogramopt{spec-only}, this gives you an opportunity to customize
1241the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001242
Greg Wardb6528972000-09-07 02:40:37 +00001243\begin{verbatim}
1244> python setup.py bdist_rpm --spec-only
1245# ...edit dist/FooBar-1.0.spec
1246> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1247\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001248
Greg Wardb6528972000-09-07 02:40:37 +00001249(Although a better way to do this is probably to override the standard
1250\command{bdist\_rpm} command with one that writes whatever else you want
1251to the \file{.spec} file; see section~\ref{extending} for information on
1252extending the Distutils.)
1253
1254
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001255\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001256\label{creating-wininst}
1257
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001258Executable installers are the natural format for binary
Fred Drake17f690f2001-07-14 02:14:42 +00001259distributions on Windows. They display a nice graphical user interface,
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001260display some information about the module distribution to be installed taken
Fred Drake457c4192001-08-16 21:25:24 +00001261from the meta-data in the setup script, let the user select a few
Thomas Heller5f52f722001-02-19 17:48:03 +00001262(currently maybe too few) options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001263
Thomas Heller5f52f722001-02-19 17:48:03 +00001264Since the meta-data is taken from the setup script, creating
1265Windows installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001266
Thomas Heller5f52f722001-02-19 17:48:03 +00001267\begin{verbatim}
1268python setup.py bdist_wininst
1269\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001270
Thomas Heller5f52f722001-02-19 17:48:03 +00001271or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001272
Thomas Heller5f52f722001-02-19 17:48:03 +00001273\begin{verbatim}
1274python setup.py bdist --formats=wininst
1275\end{verbatim}
1276
1277If you have a pure module distribution (only containing pure
1278Python modules and packages), the resulting installer will be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001279version independent and have a name like \file{foo-1.0.win32.exe}.
Thomas Heller5f52f722001-02-19 17:48:03 +00001280These installers can even be created on \UNIX{} or MacOS platforms.
1281
1282If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001283created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001284The installer filename will reflect this and now has the form
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001285\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001286for every Python version you want to support.
1287
1288The installer will try to compile pure modules into bytecode after
1289installation on the target system in normal and optimizing mode.
1290If you don't want this to happen for some reason, you can run
1291the bdist_wininst command with the \longprogramopt{no-target-compile} and/or
1292the \longprogramopt{no-target-optimize} option.
Greg Wardb6528972000-09-07 02:40:37 +00001293
Greg Ward007c04a2002-05-10 14:45:59 +00001294\section{Examples}
1295\label{examples}
Greg Ward16aafcd2000-04-09 04:06:44 +00001296
1297
Greg Ward007c04a2002-05-10 14:45:59 +00001298\subsection{Pure Python distribution (by module)}
1299\label{pure-mod}
1300
1301If you're just distributing a couple of modules, especially if they
1302don't live in a particular package, you can specify them individually
1303using the \option{py\_modules} option in the setup script.
1304
1305In the simplest case, you'll have two files to worry about: a setup
1306script and the single module you're distributing, \file{foo.py} in this
1307example:
1308\begin{verbatim}
1309<root>/
1310 setup.py
1311 foo.py
1312\end{verbatim}
1313(In all diagrams in this section, \verb|<root>| will refer to the
1314distribution root directory.) A minimal setup script to describe this
1315situation would be:
1316\begin{verbatim}
1317from distutils.core import setup
1318setup(name = "foo", version = "1.0",
1319 py_modules = ["foo"])
1320\end{verbatim}
1321Note that the name of the distribution is specified independently with
1322the \option{name} option, and there's no rule that says it has to be the
1323same as the name of the sole module in the distribution (although that's
1324probably a good convention to follow). However, the distribution name
1325is used to generate filenames, so you should stick to letters, digits,
1326underscores, and hyphens.
1327
1328Since \option{py\_modules} is a list, you can of course specify multiple
1329modules, eg. if you're distributing modules \module{foo} and
1330\module{bar}, your setup might look like this:
1331\begin{verbatim}
1332<root>/
1333 setup.py
1334 foo.py
1335 bar.py
1336\end{verbatim}
1337and the setup script might be
1338\begin{verbatim}
1339from distutils.core import setup
1340setup(name = "foobar", version = "1.0",
1341 py_modules = ["foo", "bar"])
1342\end{verbatim}
1343
1344You can put module source files into another directory, but if you have
1345enough modules to do that, it's probably easier to specify modules by
1346package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001347
1348
Greg Ward007c04a2002-05-10 14:45:59 +00001349\subsection{Pure Python distribution (by package)}
1350\label{pure-pkg}
1351
1352If you have more than a couple of modules to distribute, especially if
1353they are in multiple packages, it's probably easier to specify whole
1354packages rather than individual modules. This works even if your
1355modules are not in a package; you can just tell the Distutils to process
1356modules from the root package, and that works the same as any other
1357package (except that you don't have to have an \file{\_\_init\_\_.py}
1358file).
1359
1360The setup script from the last example could also be written as
1361\begin{verbatim}
1362from distutils.core import setup
1363setup(name = "foobar", version = "1.0",
1364 packages = [""])
1365\end{verbatim}
1366(The empty string stands for the root package.)
1367
1368If those two files are moved into a subdirectory, but remain in the root
1369package, e.g.:
1370\begin{verbatim}
1371<root>/
1372 setup.py
1373 src/ foo.py
1374 bar.py
1375\end{verbatim}
1376then you would still specify the root package, but you have to tell the
1377Distutils where source files in the root package live:
1378\begin{verbatim}
1379from distutils.core import setup
1380setup(name = "foobar", version = "1.0",
1381 package_dir = {"": "src"},
1382 packages = [""])
1383\end{verbatim}
1384
1385More typically, though, you will want to distribute multiple modules in
1386the same package (or in sub-packages). For example, if the \module{foo}
1387and \module{bar} modules belong in package \module{foobar}, one way to
1388layout your source tree is
1389\begin{verbatim}
1390<root>/
1391 setup.py
1392 foobar/
1393 __init__.py
1394 foo.py
1395 bar.py
1396\end{verbatim}
1397This is in fact the default layout expected by the Distutils, and the
1398one that requires the least work to describe in your setup script:
1399\begin{verbatim}
1400from distutils.core import setup
1401setup(name = "foobar", version = "1.0",
1402 packages = ["foobar"])
1403\end{verbatim}
1404
1405If you want to put modules in directories not named for their package,
1406then you need to use the \option{package\_dir} option again. For
1407example, if the \file{src} directory holds modules in the
1408\module{foobar} package:
1409\begin{verbatim}
1410<root>/
1411 setup.py
1412 src/
1413 __init__.py
1414 foo.py
1415 bar.py
1416\end{verbatim}
1417an appropriate setup script would be
1418\begin{verbatim}
1419from distutils.core import setup
1420setup(name = "foobar", version = "1.0",
1421 package_dir = {"foobar" : "src"},
1422 packages = ["foobar"])
1423\end{verbatim}
1424
1425Or, you might put modules from your main package right in the
1426distribution root:
1427\begin{verbatim}
1428<root>/
1429 setup.py
1430 __init__.py
1431 foo.py
1432 bar.py
1433\end{verbatim}
1434in which case your setup script would be
1435\begin{verbatim}
1436from distutils.core import setup
1437setup(name = "foobar", version = "1.0",
1438 package_dir = {"foobar" : ""},
1439 packages = ["foobar"])
1440\end{verbatim}
1441(The empty string also stands for the current directory.)
1442
1443If you have sub-packages, they must be explicitly listed in
1444\option{packages}, but any entries in \option{package\_dir}
1445automatically extend to sub-packages. (In other words, the Distutils
1446does \emph{not} scan your source tree, trying to figure out which
1447directories correspond to Python packages by looking for
1448\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1449sub-package:
1450\begin{verbatim}
1451<root>/
1452 setup.py
1453 foobar/
1454 __init__.py
1455 foo.py
1456 bar.py
1457 subfoo/
1458 __init__.py
1459 blah.py
1460\end{verbatim}
1461then the corresponding setup script would be
1462\begin{verbatim}
1463from distutils.core import setup
1464setup(name = "foobar", version = "1.0",
1465 packages = ["foobar", "foobar.subfoo"])
1466\end{verbatim}
1467(Again, the empty string in \option{package\_dir} stands for the current
1468directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001469
1470
Greg Ward007c04a2002-05-10 14:45:59 +00001471\subsection{Single extension module}
1472\label{single-ext}
1473
1474Extension modules are specified using the \option{ext\_modules} option.
1475\option{package\_dir} has no effect on where extension source files are
1476found; it only affects the source for pure Python modules. The simplest
1477case, a single extension module in a single C source file, is:
1478\begin{verbatim}
1479<root>/
1480 setup.py
1481 foo.c
1482\end{verbatim}
1483If the \module{foo} extension belongs in the root package, the setup
1484script for this could be
1485\begin{verbatim}
1486from distutils.core import setup
1487setup(name = "foobar", version = "1.0",
1488 ext_modules = [Extension("foo", ["foo.c"])])
1489\end{verbatim}
1490
1491If the extension actually belongs in a package, say \module{foopkg},
1492then
1493
1494With exactly the same source tree layout, this extension can be put in
1495the \module{foopkg} package simply by changing the name of the
1496extension:
1497\begin{verbatim}
1498from distutils.core import setup
1499setup(name = "foobar", version = "1.0",
1500 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1501\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001502
1503
Fred Drakea09262e2001-03-01 18:35:43 +00001504%\subsection{Multiple extension modules}
1505%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001506
1507
Fred Drakea09262e2001-03-01 18:35:43 +00001508%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001509
1510
Fred Drakea09262e2001-03-01 18:35:43 +00001511%\section{Extending the Distutils}
1512%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001513
1514
Fred Drakea09262e2001-03-01 18:35:43 +00001515%\subsection{Extending existing commands}
1516%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001517
1518
Fred Drakea09262e2001-03-01 18:35:43 +00001519%\subsection{Writing new commands}
1520%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001521
Fred Drakea09262e2001-03-01 18:35:43 +00001522%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001523
Greg Ward4a9e7222000-04-25 02:57:36 +00001524
1525
Greg Ward16aafcd2000-04-09 04:06:44 +00001526\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001527\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001528
1529
Fred Drakea09262e2001-03-01 18:35:43 +00001530%\subsection{Building modules: the \protect\command{build} command family}
1531%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001532
Fred Drakea09262e2001-03-01 18:35:43 +00001533%\subsubsection{\protect\command{build}}
1534%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001535
Fred Drakea09262e2001-03-01 18:35:43 +00001536%\subsubsection{\protect\command{build\_py}}
1537%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001538
Fred Drakea09262e2001-03-01 18:35:43 +00001539%\subsubsection{\protect\command{build\_ext}}
1540%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001541
Fred Drakea09262e2001-03-01 18:35:43 +00001542%\subsubsection{\protect\command{build\_clib}}
1543%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001544
1545
Greg Wardfacb8db2000-04-09 04:32:40 +00001546\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001547\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001548
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001549The install command ensures that the build commands have been run and then
1550runs the subcommands \command{install\_lib},
1551\command{install\_data} and
1552\command{install\_scripts}.
1553
Fred Drakea09262e2001-03-01 18:35:43 +00001554%\subsubsection{\protect\command{install\_lib}}
1555%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001556
1557\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001558\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001559This command installs all data files provided with the distribution.
1560
1561\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001562\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001563This command installs all (Python) scripts in the distribution.
1564
Greg Ward16aafcd2000-04-09 04:06:44 +00001565
Fred Drakea09262e2001-03-01 18:35:43 +00001566%\subsection{Cleaning up: the \protect\command{clean} command}
1567%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001568
1569
Fred Drakeeff9a872000-10-26 16:41:03 +00001570\subsection{Creating a source distribution: the
1571 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001572\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001573
1574
1575\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001576
Greg Ward16aafcd2000-04-09 04:06:44 +00001577The manifest template commands are:
1578\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001579 \lineii{include \var{pat1} \var{pat2} ... }
1580 {include all files matching any of the listed patterns}
1581 \lineii{exclude \var{pat1} \var{pat2} ... }
1582 {exclude all files matching any of the listed patterns}
1583 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1584 {include all files under \var{dir} matching any of the listed patterns}
1585 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1586 {exclude all files under \var{dir} matching any of the listed patterns}
1587 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001588 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001589 any of the listed patterns}
1590 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001591 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001592 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001593 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1594 \lineii{graft \var{dir}}{include all files under \var{dir}}
1595\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001596The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001597sequence of regular filename characters, \code{?} matches any single
1598regular filename character, and \code{[\var{range}]} matches any of the
1599characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001600\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001601platform-specific: on \UNIX{} it is anything except slash; on Windows
1602anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001603
Fred Drakeeff9a872000-10-26 16:41:03 +00001604\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001605
1606
Fred Drakea09262e2001-03-01 18:35:43 +00001607%\subsection{Creating a built distribution: the
1608% \protect\command{bdist} command family}
1609%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001610
1611
Fred Drakeab70b382001-08-02 15:13:15 +00001612%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001613
Fred Drakeab70b382001-08-02 15:13:15 +00001614%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001615
Fred Drakeab70b382001-08-02 15:13:15 +00001616%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001617
Fred Drakeab70b382001-08-02 15:13:15 +00001618%\subsubsection{\protect\command{bdist\_wininst}}
1619
1620
1621\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001622
1623
Greg Wardabc52162000-02-26 00:52:48 +00001624\end{document}