blob: 91c92914d98f7bf670b6825ae56d6425cbd72252 [file] [log] [blame]
Fred Drake211a2eb2004-03-22 21:44:43 +00001\documentclass{manual}
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
Fred Drake20d47382004-01-23 15:23:49 +000013\input{boilerplate}
14
Greg Wardabc52162000-02-26 00:52:48 +000015\author{Greg Ward}
Fred Drakeb914ef02004-01-02 06:57:50 +000016\authoraddress{
17 \strong{Python Software Foundation}\\
18 Email: \email{distutils-sig@python.org}
19}
Greg Wardabc52162000-02-26 00:52:48 +000020
Greg Warde3cca262000-08-31 16:36:31 +000021\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000022
Greg Wardabc52162000-02-26 00:52:48 +000023\begin{document}
24
Greg Wardfacb8db2000-04-09 04:32:40 +000025\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000026\begin{abstract}
27 \noindent
28 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000029 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000030 how to use the Distutils to make Python modules and extensions easily
31 available to a wider audience with very little overhead for
32 build/release/install mechanics.
33\end{abstract}
34
Fred Drakea09262e2001-03-01 18:35:43 +000035% The ugly "%begin{latexonly}" pseudo-environment supresses the table
36% of contents for HTML generation.
37%
38%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000039\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000040%end{latexonly}
41
Greg Ward16aafcd2000-04-09 04:06:44 +000042
Fred Drake211a2eb2004-03-22 21:44:43 +000043\chapter{An Introduction to Distutils}
Greg Warde78298a2000-04-28 17:12:24 +000044\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000045
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000046This document covers using the Distutils to distribute your Python
47modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000048you're looking for information on installing Python modules, you
49should refer to the \citetitle[../inst/inst.html]{Installing Python
50Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000051
52
Greg Wardfacb8db2000-04-09 04:32:40 +000053\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000054\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000055
56Using the Distutils is quite simple, both for module developers and for
57users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000058your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000059well-tested code, of course!) are:
60\begin{itemize}
61\item write a setup script (\file{setup.py} by convention)
62\item (optional) write a setup configuration file
63\item create a source distribution
64\item (optional) create one or more built (binary) distributions
65\end{itemize}
66Each of these tasks is covered in this document.
67
68Not all module developers have access to a multitude of platforms, so
69it's not always feasible to expect them to create a multitude of built
70distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000071\emph{packagers}, will arise to address this need. Packagers will take
72source distributions released by module developers, build them on one or
73more platforms, and release the resulting built distributions. Thus,
74users on the most popular platforms will be able to install most popular
75Python module distributions in the most natural way for their platform,
76without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000077
78
Fred Drake211a2eb2004-03-22 21:44:43 +000079\section{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000080\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000081
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000082The setup script is usually quite simple, although since it's written
83in Python, there are no arbitrary limits to what you can do with it,
84though you should be careful about putting arbitrarily expensive
85operations in your setup script. Unlike, say, Autoconf-style configure
86scripts, the setup script may be run multiple times in the course of
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000087building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000088
89If all you want to do is distribute a module called \module{foo},
90contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000091simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000092
Greg Ward16aafcd2000-04-09 04:06:44 +000093\begin{verbatim}
94from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000095setup(name="foo",
96 version="1.0",
97 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000098\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000099
Greg Ward16aafcd2000-04-09 04:06:44 +0000100Some observations:
101\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000102\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000103 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000104\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000105 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000106 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000107\item modules are specified by module name, not filename (the same will
108 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000109\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000110 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000111 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000112\end{itemize}
113
Greg Ward370248d2000-06-24 01:45:47 +0000114To create a source distribution for this module, you would create a
115setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000116
Greg Ward16aafcd2000-04-09 04:06:44 +0000117\begin{verbatim}
118python setup.py sdist
119\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000120
Fred Drakeeff9a872000-10-26 16:41:03 +0000121which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000122Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000123\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
124\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000125
126If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000127to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
128and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000129
Greg Ward16aafcd2000-04-09 04:06:44 +0000130\begin{verbatim}
131python setup.py install
132\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000133
Greg Ward16aafcd2000-04-09 04:06:44 +0000134which will ultimately copy \file{foo.py} to the appropriate directory
135for third-party modules in their Python installation.
136
137This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000138Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000139user interface, i.e. the setup script. The difference is which
140Distutils \emph{commands} they use: the \command{sdist} command is
141almost exclusively for module developers, while \command{install} is
142more often for installers (although most developers will want to install
143their own code occasionally).
144
Greg Ward16aafcd2000-04-09 04:06:44 +0000145If you want to make things really easy for your users, you can create
146one or more built distributions for them. For instance, if you are
147running on a Windows machine, and want to make things easy for other
148Windows users, you can create an executable installer (the most
149appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000150\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000151
Greg Ward16aafcd2000-04-09 04:06:44 +0000152\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000153python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000154\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000155
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000156will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000157current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000158
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000159Other useful built distribution formats are RPM, implemented by the
160\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000161(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
162(\command{bdist_sdux}). For example, the following command will
163create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000164
Greg Ward1d8f57a2000-08-05 00:43:11 +0000165\begin{verbatim}
166python setup.py bdist_rpm
167\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000168
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000169(The \command{bdist\_rpm} command uses the \command{rpm} executable,
170therefore this has to be run on an RPM-based system such as Red Hat
171Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000172
173You can find out what distribution formats are available at any time by
174running
Fred Drakea09262e2001-03-01 18:35:43 +0000175
Greg Ward1d8f57a2000-08-05 00:43:11 +0000176\begin{verbatim}
177python setup.py bdist --help-formats
178\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000179
180
Fred Drake211a2eb2004-03-22 21:44:43 +0000181\section{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000182\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000183
184If you're reading this document, you probably have a good idea of what
185modules, extensions, and so forth are. Nevertheless, just to be sure
186that everyone is operating from a common starting point, we offer the
187following glossary of common Python terms:
188\begin{description}
189\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000190 code imported by some other code. Three types of modules concern us
191 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000192
Greg Ward16aafcd2000-04-09 04:06:44 +0000193\item[pure Python module] a module written in Python and contained in a
194 single \file{.py} file (and possibly associated \file{.pyc} and/or
195 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000196
Greg Ward16aafcd2000-04-09 04:06:44 +0000197\item[extension module] a module written in the low-level language of
Fred Drake2884d6d2003-07-02 12:27:43 +0000198 the Python implementation: C/\Cpp{} for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000199 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000200 file, e.g. a shared object (\file{.so}) file for Python extensions on
201 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000202 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drake2884d6d2003-07-02 12:27:43 +0000203 currently, the Distutils only handles C/\Cpp{} extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000204
Greg Ward16aafcd2000-04-09 04:06:44 +0000205\item[package] a module that contains other modules; typically contained
206 in a directory in the filesystem and distinguished from other
207 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000208
Greg Ward6153fa12000-05-26 02:24:28 +0000209\item[root package] the root of the hierarchy of packages. (This isn't
210 really a package, since it doesn't have an \file{\_\_init\_\_.py}
211 file. But we have to call it something.) The vast majority of the
212 standard library is in the root package, as are many small, standalone
213 third-party modules that don't belong to a larger module collection.
214 Unlike regular packages, modules in the root package can be found in
215 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000216 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000217\end{description}
218
219
Fred Drake211a2eb2004-03-22 21:44:43 +0000220\section{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000221\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000222
223The following terms apply more specifically to the domain of
224distributing Python modules using the Distutils:
225\begin{description}
226\item[module distribution] a collection of Python modules distributed
227 together as a single downloadable resource and meant to be installed
228 \emph{en masse}. Examples of some well-known module distributions are
229 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000230 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000231 is already taken in the Python context: a single module distribution
232 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000233
Greg Ward16aafcd2000-04-09 04:06:44 +0000234\item[pure module distribution] a module distribution that contains only
235 pure Python modules and packages. Sometimes referred to as a ``pure
236 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000237
Greg Ward16aafcd2000-04-09 04:06:44 +0000238\item[non-pure module distribution] a module distribution that contains
239 at least one extension module. Sometimes referred to as a ``non-pure
240 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000241
Greg Ward16aafcd2000-04-09 04:06:44 +0000242\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000243 source distribution); the directory where \file{setup.py} exists. Generally
244 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000245\end{description}
246
247
Fred Drake211a2eb2004-03-22 21:44:43 +0000248\chapter{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000249\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000250
251The setup script is the centre of all activity in building,
252distributing, and installing modules using the Distutils. The main
253purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000254the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000255do the right thing. As we saw in section~\ref{simple-example} above,
256the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000257most information supplied to the Distutils by the module developer is
258supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000259
260Here's a slightly more involved example, which we'll follow for the next
261couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000262although the Distutils are included with Python 1.6 and later, they also
263have an independent existence so that Python 1.5.2 users can use them to
264install other module distributions. The Distutils' own setup script,
265shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000266
267\begin{verbatim}
268#!/usr/bin/env python
269
270from distutils.core import setup
271
Fred Drakea09262e2001-03-01 18:35:43 +0000272setup(name="Distutils",
273 version="1.0",
274 description="Python Distribution Utilities",
275 author="Greg Ward",
276 author_email="gward@python.net",
277 url="http://www.python.org/sigs/distutils-sig/",
278 packages=['distutils', 'distutils.command'],
279 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000280\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000281
Greg Ward16aafcd2000-04-09 04:06:44 +0000282There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000283distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000284metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000285rather than by module. This is important since the Distutils consist of
286a couple of dozen modules split into (so far) two packages; an explicit
287list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000288maintain. For more information on the additional meta-data, see
289section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000290
Greg Ward46b98e32000-04-14 01:53:36 +0000291Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000292script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000293slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000294platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000295current platform before actually using the pathname. This makes your
296setup script portable across operating systems, which of course is one
297of the major goals of the Distutils. In this spirit, all pathnames in
Fred Drake781380c2004-02-19 23:17:46 +0000298this document are slash-separated. (Mac OS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000299mind that the \emph{absence} of a leading slash indicates a relative
Fred Drake781380c2004-02-19 23:17:46 +0000300path, the opposite of the Mac OS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000301
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000302This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000303functions. If you, for example, use standard Python functions such as
304\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000305should be careful to write portable code instead of hardcoding path
306separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000307
Thomas Heller5f52f722001-02-19 17:48:03 +0000308\begin{verbatim}
309 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
310 os.listdir(os.path.join('mydir', 'subdir'))
311\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000312
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000313
Greg Ward2afffd42000-08-06 20:37:24 +0000314\subsection{Listing whole packages}
315\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000316
317The \option{packages} option tells the Distutils to process (build,
318distribute, install, etc.) all pure Python modules found in each package
319mentioned in the \option{packages} list. In order to do this, of
320course, there has to be a correspondence between package names and
321directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000322obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000323\file{distutils} relative to the distribution root. Thus, when you say
324\code{packages = ['foo']} in your setup script, you are promising that
325the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
326be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000327the directory where your setup script lives. If you break this
328promise, the Distutils will issue a warning but still process the broken
329package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000330
331If you use a different convention to lay out your source directory,
332that's no problem: you just have to supply the \option{package\_dir}
333option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000334you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000335``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000336\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
337and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000338
Greg Ward16aafcd2000-04-09 04:06:44 +0000339\begin{verbatim}
340package_dir = {'': 'lib'}
341\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000342
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000343in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000344and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000345directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000346you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000347\file{lib/foo/\_\_init\_\_.py} exists.
348
Greg Ward1ecc2512000-04-19 22:36:24 +0000349Another possible convention is to put the \module{foo} package right in
350\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000351would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000352
Greg Ward16aafcd2000-04-09 04:06:44 +0000353\begin{verbatim}
354package_dir = {'foo': 'lib'}
355\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000356
Greg Ward59d382e2000-05-26 01:04:47 +0000357A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
358dictionary implicitly applies to all packages below \var{package}, so
359the \module{foo.bar} case is automatically handled here. In this
360example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
361to look for \file{lib/\_\_init\_\_.py} and
362\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
363\option{package\_dir} applies recursively, you must explicitly list all
364packages in \option{packages}: the Distutils will \emph{not} recursively
365scan your source tree looking for any directory with an
366\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000367
368
369\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000370\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000371
372For a small module distribution, you might prefer to list all modules
373rather than listing packages---especially the case of a single module
374that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000375simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000376slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000377
Greg Ward16aafcd2000-04-09 04:06:44 +0000378\begin{verbatim}
379py_modules = ['mod1', 'pkg.mod2']
380\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000381
Greg Ward16aafcd2000-04-09 04:06:44 +0000382This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000383other in the \module{pkg} package. Again, the default package/directory
384layout implies that these two modules can be found in \file{mod1.py} and
385\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000386And again, you can override the package/directory correspondence using
387the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000388
389
390\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000391\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000392
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000393% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000394Just as writing Python extension modules is a bit more complicated than
395writing pure Python modules, describing them to the Distutils is a bit
396more complicated. Unlike pure modules, it's not enough just to list
397modules or packages and expect the Distutils to go out and find the
398right files; you have to specify the extension name, source file(s), and
399any compile/link requirements (include directories, libraries to link
400with, etc.).
401
402All of this is done through another keyword argument to
403\function{setup()}, the \option{extensions} option. \option{extensions}
404is just a list of \class{Extension} instances, each of which describes a
405single extension module. Suppose your distribution includes a single
406extension, called \module{foo} and implemented by \file{foo.c}. If no
407additional instructions to the compiler/linker are needed, describing
408this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000409
Greg Ward2afffd42000-08-06 20:37:24 +0000410\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000411uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000412\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000413
Greg Ward2afffd42000-08-06 20:37:24 +0000414The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000415\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000416script for a module distribution that contains only this one extension
417and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000418
Greg Ward2afffd42000-08-06 20:37:24 +0000419\begin{verbatim}
420from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000421setup(name="foo", version="1.0",
422 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000423\end{verbatim}
424
425The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000426machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000427great deal of flexibility in describing Python extensions, which is
428explained in the following sections.
429
430
431\subsubsection{Extension names and packages}
432
433The first argument to the \class{Extension} constructor is always the
434name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000435
Greg Ward2afffd42000-08-06 20:37:24 +0000436\begin{verbatim}
437Extension("foo", ["src/foo1.c", "src/foo2.c"])
438\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000439
Greg Ward2afffd42000-08-06 20:37:24 +0000440describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000441
Greg Ward2afffd42000-08-06 20:37:24 +0000442\begin{verbatim}
443Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
444\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000445
Greg Ward2afffd42000-08-06 20:37:24 +0000446describes the same extension in the \module{pkg} package. The source
447files and resulting object code are identical in both cases; the only
448difference is where in the filesystem (and therefore where in Python's
449namespace hierarchy) the resulting extension lives.
450
451If you have a number of extensions all in the same package (or all under
452the same base package), use the \option{ext\_package} keyword argument
453to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000454
Greg Ward2afffd42000-08-06 20:37:24 +0000455\begin{verbatim}
456setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000457 ext_package="pkg",
458 ext_modules=[Extension("foo", ["foo.c"]),
459 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000460 )
461\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000462
Greg Ward2afffd42000-08-06 20:37:24 +0000463will compile \file{foo.c} to the extension \module{pkg.foo}, and
464\file{bar.c} to \module{pkg.subpkg.bar}.
465
466
467\subsubsection{Extension source files}
468
469The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000470source files. Since the Distutils currently only support C, \Cpp, and
471Objective-C extensions, these are normally C/\Cpp/Objective-C source
472files. (Be sure to use appropriate extensions to distinguish \Cpp\
473source files: \file{.cc} and \file{.cpp} seem to be recognized by both
474\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000475
476However, you can also include SWIG interface (\file{.i}) files in the
477list; the \command{build\_ext} command knows how to deal with SWIG
478extensions: it will run SWIG on the interface file and compile the
Fred Drake2884d6d2003-07-02 12:27:43 +0000479resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000480
481\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000482 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000483 here when the interface firms up.}
484
485On some platforms, you can include non-source files that are processed
486by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000487means Windows message text (\file{.mc}) files and resource definition
Fred Drake2884d6d2003-07-02 12:27:43 +0000488(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000489(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000490
491
492\subsubsection{Preprocessor options}
493
494Three optional arguments to \class{Extension} will help if you need to
495specify include directories to search or preprocessor macros to
496define/undefine: \code{include\_dirs}, \code{define\_macros}, and
497\code{undef\_macros}.
498
499For example, if your extension requires header files in the
500\file{include} directory under your distribution root, use the
501\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000502
Greg Ward2afffd42000-08-06 20:37:24 +0000503\begin{verbatim}
504Extension("foo", ["foo.c"], include_dirs=["include"])
505\end{verbatim}
506
507You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000508extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000509\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000510
Greg Ward2afffd42000-08-06 20:37:24 +0000511\begin{verbatim}
512Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
513\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000514
Greg Ward2afffd42000-08-06 20:37:24 +0000515You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000516distribute your code: it's probably better to write C code like
517\begin{verbatim}
518#include <X11/Xlib.h>
519\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000520
521If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000522you can take advantage of the fact that header files are installed in a
523consistent way by the Distutils \command{install\_header} command. For
524example, the Numerical Python header files are installed (on a standard
525Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
526(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000527installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000528directory---\file{/usr/local/include/python1.5} in this case---is always
529included in the search path when building Python extensions, the best
530approach is to write C code like
531\begin{verbatim}
532#include <Numerical/arrayobject.h>
533\end{verbatim}
534If you must put the \file{Numerical} include directory right into your
535header search path, though, you can find that directory using the
536Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000537
Greg Ward2afffd42000-08-06 20:37:24 +0000538\begin{verbatim}
539from distutils.sysconfig import get_python_inc
540incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
541setup(...,
542 Extension(..., include_dirs=[incdir]))
543\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000544
Greg Ward2afffd42000-08-06 20:37:24 +0000545Even though this is quite portable---it will work on any Python
546installation, regardless of platform---it's probably easier to just
547write your C code in the sensible way.
548
549You can define and undefine pre-processor macros with the
550\code{define\_macros} and \code{undef\_macros} options.
551\code{define\_macros} takes a list of \code{(name, value)} tuples, where
552\code{name} is the name of the macro to define (a string) and
553\code{value} is its value: either a string or \code{None}. (Defining a
554macro \code{FOO} to \code{None} is the equivalent of a bare
555\code{\#define FOO} in your C source: with most compilers, this sets
556\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
557a list of macros to undefine.
558
559For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000560
Greg Ward2afffd42000-08-06 20:37:24 +0000561\begin{verbatim}
562Extension(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000563 define_macros=[('NDEBUG', '1'),
564 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000565 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
566\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000567
Greg Ward2afffd42000-08-06 20:37:24 +0000568is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000569
Greg Ward2afffd42000-08-06 20:37:24 +0000570\begin{verbatim}
571#define NDEBUG 1
572#define HAVE_STRFTIME
573#undef HAVE_FOO
574#undef HAVE_BAR
575\end{verbatim}
576
577
578\subsubsection{Library options}
579
580You can also specify the libraries to link against when building your
581extension, and the directories to search for those libraries. The
582\code{libraries} option is a list of libraries to link against,
583\code{library\_dirs} is a list of directories to search for libraries at
584link-time, and \code{runtime\_library\_dirs} is a list of directories to
585search for shared (dynamically loaded) libraries at run-time.
586
587For example, if you need to link against libraries known to be in the
588standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000589
Greg Ward2afffd42000-08-06 20:37:24 +0000590\begin{verbatim}
591Extension(...,
592 libraries=["gdbm", "readline"])
593\end{verbatim}
594
595If you need to link with libraries in a non-standard location, you'll
596have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000597
Greg Ward2afffd42000-08-06 20:37:24 +0000598\begin{verbatim}
599Extension(...,
600 library_dirs=["/usr/X11R6/lib"],
601 libraries=["X11", "Xt"])
602\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000603
Greg Ward2afffd42000-08-06 20:37:24 +0000604(Again, this sort of non-portable construct should be avoided if you
605intend to distribute your code.)
606
Thomas Heller5f52f722001-02-19 17:48:03 +0000607\XXX{Should mention clib libraries here or somewhere else!}
608
609\subsubsection{Other options}
610
611There are still some other options which can be used to handle special
612cases.
613
614The \option{extra\_objects} option is a list of object files to be passed
615to the linker. These files must not have extensions, as the default
616extension for the compiler is used.
617
618\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000619to specify additional command line options for the respective compiler and
620linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000621
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000622\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000623of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000624is not needed when building compiled extensions: Distutils
625will automatically add \code{initmodule}
626to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000627
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000628\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000629So far we have been dealing with pure and non-pure Python modules,
630which are usually not run by themselves but imported by scripts.
631
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000632Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000633started from the command line. Scripts don't require Distutils to do
634anything very complicated. The only clever feature is that if the
635first line of the script starts with \code{\#!} and contains the word
636``python'', the Distutils will adjust the first line to refer to the
637current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000638
639The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000640in this way. From the PyXML setup script:
641
642\begin{verbatim}
643setup (...
644 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
645 )
646\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000647
648
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000649\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000650
Thomas Heller5f52f722001-02-19 17:48:03 +0000651The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000652files needed by the module distribution: configuration files, message
653catalogs, data files, anything which doesn't fit in the previous
654categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000655
Fred Drake632bda32002-03-08 22:02:06 +0000656\option{data\_files} specifies a sequence of (\var{directory},
657\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000658
Thomas Heller5f52f722001-02-19 17:48:03 +0000659\begin{verbatim}
660setup(...
661 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000662 ('config', ['cfg/data.cfg']),
663 ('/etc/init.d', ['init-script'])]
664 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000665\end{verbatim}
666
667Note that you can specify the directory names where the data files
668will be installed, but you cannot rename the data files themselves.
669
Fred Drake632bda32002-03-08 22:02:06 +0000670Each (\var{directory}, \var{files}) pair in the sequence specifies the
671installation directory and the files to install there. If
672\var{directory} is a relative path, it is interpreted relative to the
673installation prefix (Python's \code{sys.prefix} for pure-Python
674packages, \code{sys.exec_prefix} for packages that contain extension
675modules). Each file name in \var{files} is interpreted relative to
676the \file{setup.py} script at the top of the package source
677distribution. No directory information from \var{files} is used to
678determine the final location of the installed file; only the name of
679the file is used.
680
Thomas Heller5f52f722001-02-19 17:48:03 +0000681You can specify the \option{data\_files} options as a simple sequence
682of files without specifying a target directory, but this is not recommended,
683and the \command{install} command will print a warning in this case.
684To install data files directly in the target directory, an empty
685string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000686
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000687\subsection{Additional meta-data}
688\label{meta-data}
689
690The setup script may include additional meta-data beyond the name and
691version. This information includes:
692
Fred Drakec440af52003-04-25 16:43:28 +0000693\begin{tableiv}{l|l|l|c}{code}%
694 {Meta-Data}{Description}{Value}{Notes}
695 \lineiv{name}{name of the package}
696 {short string}{(1)}
697 \lineiv{version}{version of this release}
698 {short string}{(1)(2)}
699 \lineiv{author}{package author's name}
700 {short string}{(3)}
701 \lineiv{author_email}{email address of the package author}
702 {email address}{(3)}
703 \lineiv{maintainer}{package maintainer's name}
704 {short string}{(3)}
705 \lineiv{maintainer_email}{email address of the package maintainer}
706 {email address}{(3)}
707 \lineiv{url}{home page for the package}
708 {URL}{(1)}
709 \lineiv{description}{short, summary description of the package}
710 {short string}{}
711 \lineiv{long_description}{longer description of the package}
712 {long string}{}
713 \lineiv{download_url}{location where the package may be downloaded}
714 {URL}{(4)}
715 \lineiv{classifiers}{a list of Trove classifiers}
716 {list of strings}{(4)}
717\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000718
719\noindent Notes:
720\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000721\item[(1)] These fields are required.
722\item[(2)] It is recommended that versions take the form
723 \emph{major.minor\optional{.patch\optional{.sub}}}.
724\item[(3)] Either the author or the maintainer must be identified.
725\item[(4)] These fields should not be used if your package is to be
726 compatible with Python versions prior to 2.2.3 or 2.3. The list is
727 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
728
729\item["short string"] A single line of text, not more than 200 characters.
730\item["long string"] Multiple lines of plain text in ReStructuredText
731 format (see \url{http://docutils.sf.net/}).
732\item["list of strings"] See below.
733\end{description}
734
735None of the string values may be Unicode.
736
737Encoding the version information is an art in itself. Python packages
738generally adhere to the version format
739\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7400 for
741initial, experimental releases of software. It is incremented for
742releases that represent major milestones in a package. The minor
743number is incremented when important new features are added to the
744package. The patch number increments when bug-fix releases are
745made. Additional trailing version information is sometimes used to
746indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
747where functionality and API may change), "b1,b2,...,bN" (for beta
748releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
749pre-release release testing). Some examples:
750
751\begin{description}
752\item[0.1.0] the first, experimental release of a package
753\item[1.0.1a2] the second alpha release of the first patch version of 1.0
754\end{description}
755
756\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000757
758\begin{verbatim}
759setup(...
Fred Drakec440af52003-04-25 16:43:28 +0000760 classifiers = [
Fred Drake2a046232003-03-31 16:23:09 +0000761 'Development Status :: 4 - Beta',
762 'Environment :: Console',
763 'Environment :: Web Environment',
764 'Intended Audience :: End Users/Desktop',
765 'Intended Audience :: Developers',
766 'Intended Audience :: System Administrators',
767 'License :: OSI Approved :: Python Software Foundation License',
768 'Operating System :: MacOS :: MacOS X',
769 'Operating System :: Microsoft :: Windows',
770 'Operating System :: POSIX',
771 'Programming Language :: Python',
772 'Topic :: Communications :: Email',
773 'Topic :: Office/Business',
774 'Topic :: Software Development :: Bug Tracking',
775 ],
Fred Drake2a046232003-03-31 16:23:09 +0000776 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000777\end{verbatim}
778
Fred Drakec440af52003-04-25 16:43:28 +0000779If you wish to include classifiers in your \file{setup.py} file and also
780wish to remain backwards-compatible with Python releases prior to 2.2.3,
781then you can include the following code fragment in your \file{setup.py}
782before the \code{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000783
784\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000785# patch distutils if it can't cope with the "classifiers" or
786# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000787if sys.version < '2.2.3':
788 from distutils.dist import DistributionMetadata
789 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000790 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000791\end{verbatim}
792
Greg Ward16aafcd2000-04-09 04:06:44 +0000793
Thomas Heller675580f2003-06-30 19:33:29 +0000794\subsection{Debugging the setup script}
795\label{meta-data}
796
797Sometimes things go wrong, and the setup script doesn't do what the
798developer wants.
799
800Distutils catches any exceptions when running the setup script, and
801print a simple error message before the script is terminated. The
802motivation for this behaviour is to not confuse administrators who
803don't know much about Python and are trying to install a package. If
804they get a big long traceback from deep inside the guts of Distutils,
805they may think the package or the Python installation is broken
806because they don't read all the way down to the bottom and see that
807it's a permission problem.
808
809On the other hand, this doesn't help the developer to find the cause
810of the failure. For this purpose, the DISTUTILS_DEBUG environment
811variable can be set to anything except an empty string, and distutils
812will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000813full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000814
Fred Drake211a2eb2004-03-22 21:44:43 +0000815\chapter{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000816\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000817
Greg Ward16aafcd2000-04-09 04:06:44 +0000818Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000819distribution \emph{a priori}: you may need to get some information from
820the user, or from the user's system, in order to proceed. As long as
821that information is fairly simple---a list of directories to search for
822C header files or libraries, for example---then providing a
823configuration file, \file{setup.cfg}, for users to edit is a cheap and
824easy way to solicit it. Configuration files also let you provide
825default values for any command option, which the installer can then
826override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000827
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000828% (If you have more advanced needs, such as determining which extensions
829% to build based on what capabilities are present on the target system,
830% then you need the Distutils ``auto-configuration'' facility. This
831% started to appear in Distutils 0.9 but, as of this writing, isn't mature
832% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000833
Greg Ward47f99a62000-09-04 20:07:15 +0000834The setup configuration file is a useful middle-ground between the setup
835script---which, ideally, would be opaque to installers\footnote{This
836 ideal probably won't be achieved until auto-configuration is fully
837 supported by the Distutils.}---and the command-line to the setup
838script, which is outside of your control and entirely up to the
839installer. In fact, \file{setup.cfg} (and any other Distutils
840configuration files present on the target system) are processed after
841the contents of the setup script, but before the command-line. This has
842several useful consequences:
843\begin{itemize}
844\item installers can override some of what you put in \file{setup.py} by
845 editing \file{setup.cfg}
846\item you can provide non-standard defaults for options that are not
847 easily set in \file{setup.py}
848\item installers can override anything in \file{setup.cfg} using the
849 command-line options to \file{setup.py}
850\end{itemize}
851
852The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000853
Greg Ward47f99a62000-09-04 20:07:15 +0000854\begin{verbatim}
855[command]
856option=value
857...
858\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000859
Greg Ward47f99a62000-09-04 20:07:15 +0000860where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000861\command{build\_py}, \command{install}), and \var{option} is one of
862the options that command supports. Any number of options can be
863supplied for each command, and any number of command sections can be
864included in the file. Blank lines are ignored, as are comments, which
865run from a \character{\#} character until the end of the line. Long
866option values can be split across multiple lines simply by indenting
867the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000868
869You can find out the list of options supported by a particular command
870with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000871
Greg Ward47f99a62000-09-04 20:07:15 +0000872\begin{verbatim}
873> python setup.py --help build_ext
874[...]
875Options for 'build_ext' command:
876 --build-lib (-b) directory for compiled extension modules
877 --build-temp (-t) directory for temporary files (build by-products)
878 --inplace (-i) ignore build-lib and put compiled extensions into the
879 source directory alongside your pure Python modules
880 --include-dirs (-I) list of directories to search for header files
881 --define (-D) C preprocessor macros to define
882 --undef (-U) C preprocessor macros to undefine
883[...]
884\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000885
Greg Ward47f99a62000-09-04 20:07:15 +0000886Note that an option spelled \longprogramopt{foo-bar} on the command-line
887is spelled \option{foo\_bar} in configuration files.
888
889For example, say you want your extensions to be built
890``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000891want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000892in the same source directory as your pure Python modules
893\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
894\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000895
Greg Ward47f99a62000-09-04 20:07:15 +0000896\begin{verbatim}
897python setup.py build_ext --inplace
898\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000899
Greg Ward47f99a62000-09-04 20:07:15 +0000900But this requires that you always specify the \command{build\_ext}
901command explicitly, and remember to provide \longprogramopt{inplace}.
902An easier way is to ``set and forget'' this option, by encoding it in
903\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000904
Greg Ward47f99a62000-09-04 20:07:15 +0000905\begin{verbatim}
906[build_ext]
907inplace=1
908\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000909
Greg Ward47f99a62000-09-04 20:07:15 +0000910This will affect all builds of this module distribution, whether or not
911you explcitly specify \command{build\_ext}. If you include
912\file{setup.cfg} in your source distribution, it will also affect
913end-user builds---which is probably a bad idea for this option, since
914always building extensions in-place would break installation of the
915module distribution. In certain peculiar cases, though, modules are
916built right in their installation directory, so this is conceivably a
917useful ability. (Distributing extensions that expect to be built in
918their installation directory is almost always a bad idea, though.)
919
920Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000921change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000922everything required to generate a ``spec'' file for creating an RPM
923distribution. Some of this information comes from the setup script, and
924some is automatically generated by the Distutils (such as the list of
925files installed). But some of it has to be supplied as options to
926\command{bdist\_rpm}, which would be very tedious to do on the
927command-line for every run. Hence, here is a snippet from the
928Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000929
Greg Ward47f99a62000-09-04 20:07:15 +0000930\begin{verbatim}
931[bdist_rpm]
932release = 1
933packager = Greg Ward <gward@python.net>
934doc_files = CHANGES.txt
935 README.txt
936 USAGE.txt
937 doc/
938 examples/
939\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000940
Greg Ward47f99a62000-09-04 20:07:15 +0000941Note that the \option{doc\_files} option is simply a
942whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000943
944
Fred Drakea09262e2001-03-01 18:35:43 +0000945\begin{seealso}
946 \seetitle[../inst/config-syntax.html]{Installing Python
947 Modules}{More information on the configuration files is
948 available in the manual for system administrators.}
949\end{seealso}
950
951
Fred Drake211a2eb2004-03-22 21:44:43 +0000952\chapter{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000953\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000954
Greg Warde78298a2000-04-28 17:12:24 +0000955As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000956\command{sdist} command to create a source distribution. In the
957simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000958
Greg Ward16aafcd2000-04-09 04:06:44 +0000959\begin{verbatim}
960python setup.py sdist
961\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000962
Greg Ward19c67f82000-06-24 01:33:16 +0000963(assuming you haven't specified any \command{sdist} options in the setup
964script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000965default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000966tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
Fred Drake781380c2004-02-19 23:17:46 +0000967\XXX{no Mac OS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000968
Greg Wardd5767a52000-04-19 22:48:09 +0000969You can specify as many formats as you like using the
970\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000971
Greg Ward16aafcd2000-04-09 04:06:44 +0000972\begin{verbatim}
973python setup.py sdist --formats=gztar,zip
974\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000975
Greg Ward16aafcd2000-04-09 04:06:44 +0000976to create a gzipped tarball and a zip file. The available formats are:
Fred Drake781380c2004-02-19 23:17:46 +0000977
Greg Ward46b98e32000-04-14 01:53:36 +0000978\begin{tableiii}{l|l|c}{code}%
979 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000980 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
981 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000982 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000983 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000984 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000985\end{tableiii}
986
987\noindent Notes:
988\begin{description}
989\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000990\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000991\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000992 \module{zipfile} module (part of the standard Python library since
993 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000994\item[(4)] requires external utilities: \program{tar} and possibly one
995 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000996\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000997
998
Greg Ward54589d42000-09-06 01:37:35 +0000999
1000\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001001\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001002
Greg Ward54589d42000-09-06 01:37:35 +00001003If you don't supply an explicit list of files (or instructions on how to
1004generate one), the \command{sdist} command puts a minimal default set
1005into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001006\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001007\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001008 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001009\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001010 \option{libraries} options (\XXX{getting C library sources currently
Fred Drake781380c2004-02-19 23:17:46 +00001011 broken---no \method{get_source_files()} method in \file{build_clib.py}!})
Greg Ward16aafcd2000-04-09 04:06:44 +00001012\item anything that looks like a test script: \file{test/test*.py}
1013 (currently, the Distutils don't do anything with test scripts except
1014 include them in source distributions, but in the future there will be
1015 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001016\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1017 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001018\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001019
Greg Ward16aafcd2000-04-09 04:06:44 +00001020Sometimes this is enough, but usually you will want to specify
1021additional files to distribute. The typical way to do this is to write
1022a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001023manifest template is just a list of instructions for how to generate
1024your manifest file, \file{MANIFEST}, which is the exact list of files to
1025include in your source distribution. The \command{sdist} command
1026processes this template and generates a manifest based on its
1027instructions and what it finds in the filesystem.
1028
1029If you prefer to roll your own manifest file, the format is simple: one
1030filename per line, regular files (or symlinks to them) only. If you do
1031supply your own \file{MANIFEST}, you must specify everything: the
1032default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001033
1034The manifest template has one command per line, where each command
1035specifies a set of files to include or exclude from the source
1036distribution. For an example, again we turn to the Distutils' own
1037manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001038
Greg Ward16aafcd2000-04-09 04:06:44 +00001039\begin{verbatim}
1040include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001041recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001042prune examples/sample?/build
1043\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001044
Greg Ward16aafcd2000-04-09 04:06:44 +00001045The meanings should be fairly clear: include all files in the
1046distribution root matching \code{*.txt}, all files anywhere under the
1047\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001048exclude all directories matching \code{examples/sample?/build}. All of
1049this is done \emph{after} the standard include set, so you can exclude
1050files from the standard set with explicit instructions in the manifest
1051template. (Or, you can use the \longprogramopt{no-defaults} option to
1052disable the standard set entirely.) There are several other commands
1053available in the manifest template mini-language; see
1054section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001055
Greg Ward54589d42000-09-06 01:37:35 +00001056The order of commands in the manifest template matters: initially, we
1057have the list of default files as described above, and each command in
1058the template adds to or removes from that list of files. Once we have
1059fully processed the manifest template, we remove files that should not
1060be included in the source distribution:
1061\begin{itemize}
1062\item all files in the Distutils ``build'' tree (default \file{build/})
1063\item all files in directories named \file{RCS} or \file{CVS}
1064\end{itemize}
1065Now we have our complete list of files, which is written to the manifest
1066for future reference, and then used to build the source distribution
1067archive(s).
1068
1069You can disable the default set of included files with the
1070\longprogramopt{no-defaults} option, and you can disable the standard
1071exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001072
Greg Ward46b98e32000-04-14 01:53:36 +00001073Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001074\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001075Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001076\begin{enumerate}
1077\item include all Python source files in the \file{distutils} and
1078 \file{distutils/command} subdirectories (because packages
1079 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001080 \option{packages} option in the setup script---see
1081 section~\ref{setup-script})
1082\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1083 (standard files)
1084\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001085\item include \file{*.txt} in the distribution root (this will find
1086 \file{README.txt} a second time, but such redundancies are weeded out
1087 later)
Greg Ward54589d42000-09-06 01:37:35 +00001088\item include anything matching \file{*.txt} or \file{*.py} in the
1089 sub-tree under \file{examples},
1090\item exclude all files in the sub-trees starting at directories
1091 matching \file{examples/sample?/build}---this may exclude files
1092 included by the previous two steps, so it's important that the
1093 \code{prune} command in the manifest template comes after the
1094 \code{recursive-include} command
1095\item exclude the entire \file{build} tree, and any \file{RCS} or
1096 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001097\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001098Just like in the setup script, file and directory names in the manifest
1099template should always be slash-separated; the Distutils will take care
1100of converting them to the standard representation on your platform.
1101That way, the manifest template is portable across operating systems.
1102
Greg Ward16aafcd2000-04-09 04:06:44 +00001103
1104\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001105\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001106
1107The normal course of operations for the \command{sdist} command is as
1108follows:
1109\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001110\item if the manifest file, \file{MANIFEST} doesn't exist, read
1111 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001112\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001113 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001114\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1115 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1116 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001117\item use the list of files now in \file{MANIFEST} (either just
1118 generated or read in) to create the source distribution archive(s)
1119\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001120There are a couple of options that modify this behaviour. First, use
1121the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001122disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001123
Greg Ward54589d42000-09-06 01:37:35 +00001124Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001125example, if you have added or removed files or directories that match an
1126existing pattern in the manifest template, you should regenerate the
1127manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001128
Greg Ward16aafcd2000-04-09 04:06:44 +00001129\begin{verbatim}
1130python setup.py sdist --force-manifest
1131\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001132
1133Or, you might just want to (re)generate the manifest, but not create a
1134source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001135
Greg Ward16aafcd2000-04-09 04:06:44 +00001136\begin{verbatim}
1137python setup.py sdist --manifest-only
1138\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001139
Greg Ward54589d42000-09-06 01:37:35 +00001140\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1141\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1142\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001143
1144
Fred Drake211a2eb2004-03-22 21:44:43 +00001145\chapter{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001146\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001147
Greg Ward46b98e32000-04-14 01:53:36 +00001148A ``built distribution'' is what you're probably used to thinking of
1149either as a ``binary package'' or an ``installer'' (depending on your
1150background). It's not necessarily binary, though, because it might
1151contain only Python source code and/or byte-code; and we don't call it a
1152package, because that word is already spoken for in Python. (And
Fred Drake2a1bc502004-02-19 23:03:29 +00001153``installer'' is a term specific to the world of mainstream desktop
1154systems.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001155
Greg Ward46b98e32000-04-14 01:53:36 +00001156A built distribution is how you make life as easy as possible for
1157installers of your module distribution: for users of RPM-based Linux
1158systems, it's a binary RPM; for Windows users, it's an executable
1159installer; for Debian-based Linux users, it's a Debian package; and so
1160forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001161distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001162designed to enable module developers to concentrate on their
1163specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001164intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001165distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001166are packagers.
1167
1168Of course, the module developer could be his own packager; or the
1169packager could be a volunteer ``out there'' somewhere who has access to
1170a platform which the original developer does not; or it could be
1171software periodically grabbing new source distributions and turning them
1172into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001173access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001174setup script and the \command{bdist} command family to generate built
1175distributions.
1176
1177As a simple example, if I run the following command in the Distutils
1178source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001179
Greg Ward46b98e32000-04-14 01:53:36 +00001180\begin{verbatim}
1181python setup.py bdist
1182\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001183
Greg Ward46b98e32000-04-14 01:53:36 +00001184then the Distutils builds my module distribution (the Distutils itself
1185in this case), does a ``fake'' installation (also in the \file{build}
1186directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001187platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001188file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001189file is considered ``dumb'' because it has to be unpacked in a specific
1190location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001191
Fred Drakeeff9a872000-10-26 16:41:03 +00001192Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001193\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001194from the right place installs the Distutils just as though you had
1195downloaded the source distribution and run \code{python setup.py
1196 install}. (The ``right place'' is either the root of the filesystem or
1197Python's \filevar{prefix} directory, depending on the options given to
1198the \command{bdist\_dumb} command; the default is to make dumb
1199distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001200
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001201Obviously, for pure Python distributions, this isn't any simpler than
1202just running \code{python setup.py install}---but for non-pure
1203distributions, which include extensions that would need to be
1204compiled, it can mean the difference between someone being able to use
1205your extensions or not. And creating ``smart'' built distributions,
1206such as an RPM package or an executable installer for Windows, is far
1207more convenient for users even if your distribution doesn't include
1208any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001209
Greg Wardb6528972000-09-07 02:40:37 +00001210The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001211similar to the \command{sdist} command, which you can use to select the
1212types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001213
Greg Ward46b98e32000-04-14 01:53:36 +00001214\begin{verbatim}
1215python setup.py bdist --format=zip
1216\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001217
Fred Drakeeff9a872000-10-26 16:41:03 +00001218would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001219\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001220unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001221
1222The available formats for built distributions are:
Fred Drake781380c2004-02-19 23:17:46 +00001223
Greg Ward46b98e32000-04-14 01:53:36 +00001224\begin{tableiii}{l|l|c}{code}%
1225 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001226 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1227 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1228 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1229 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1230 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001231 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1232 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1233 \lineiii{rpm}{RPM}{(5)}
1234% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001235 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001236\end{tableiii}
1237
1238\noindent Notes:
1239\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001240\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001241\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001242\item[(3)] requires external utilities: \program{tar} and possibly one
1243 of \program{gzip}, \program{bzip2}, or \program{compress}
1244\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001245 \module{zipfile} module (part of the standard Python library since
1246 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001247\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1248 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001249\end{description}
1250
1251You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001252\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001253directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001254\command{bdist} ``sub-commands'' actually generate several similar
1255formats; for instance, the \command{bdist\_dumb} command generates all
1256the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1257\code{zip}), and \command{bdist\_rpm} generates both binary and source
1258RPMs. The \command{bdist} sub-commands, and the formats generated by
1259each, are:
Fred Drake781380c2004-02-19 23:17:46 +00001260
Greg Ward46b98e32000-04-14 01:53:36 +00001261\begin{tableii}{l|l}{command}%
1262 {Command}{Formats}
1263 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1264 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001265 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001266\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001267
Greg Wardb6528972000-09-07 02:40:37 +00001268The following sections give details on the individual \command{bdist\_*}
1269commands.
1270
1271
1272\subsection{Creating dumb built distributions}
1273\label{creating-dumb}
1274
1275\XXX{Need to document absolute vs. prefix-relative packages here, but
1276 first I have to implement it!}
1277
1278
1279\subsection{Creating RPM packages}
1280\label{creating-rpms}
1281
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001282The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001283Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1284RPM-based Linux distributions) is your usual environment, creating RPM
1285packages for other users of that same distribution is trivial.
1286Depending on the complexity of your module distribution and differences
1287between Linux distributions, you may also be able to create RPMs that
1288work on different RPM-based distributions.
1289
1290The usual way to create an RPM of your module distribution is to run the
1291\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001292
Greg Wardb6528972000-09-07 02:40:37 +00001293\begin{verbatim}
1294python setup.py bdist_rpm
1295\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001296
Greg Wardb6528972000-09-07 02:40:37 +00001297or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001298
Greg Wardb6528972000-09-07 02:40:37 +00001299\begin{verbatim}
1300python setup.py bdist --formats=rpm
1301\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001302
Greg Wardb6528972000-09-07 02:40:37 +00001303The former allows you to specify RPM-specific options; the latter allows
1304you to easily specify multiple formats in one run. If you need to do
1305both, you can explicitly specify multiple \command{bdist\_*} commands
1306and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001307
Greg Wardb6528972000-09-07 02:40:37 +00001308\begin{verbatim}
1309python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1310 bdist_wininst --target_version="2.0"
1311\end{verbatim}
1312
1313Creating RPM packages is driven by a \file{.spec} file, much as using
1314the Distutils is driven by the setup script. To make your life easier,
1315the \command{bdist\_rpm} command normally creates a \file{.spec} file
1316based on the information you supply in the setup script, on the command
1317line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001318sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001319script as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001320
Greg Wardb6528972000-09-07 02:40:37 +00001321\begin{tableii}{l|l}{textrm}%
1322 {RPM \file{.spec} file option or section}{Distutils setup script option}
1323 \lineii{Name}{\option{name}}
1324 \lineii{Summary (in preamble)}{\option{description}}
1325 \lineii{Version}{\option{version}}
1326 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1327 \option{maintainer} and \option{maintainer\_email}}
1328 \lineii{Copyright}{\option{licence}}
1329 \lineii{Url}{\option{url}}
1330 \lineii{\%description (section)}{\option{long\_description}}
1331\end{tableii}
1332
1333Additionally, there many options in \file{.spec} files that don't have
1334corresponding options in the setup script. Most of these are handled
1335through options to the \command{bdist\_rpm} command as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001336
Greg Wardb6528972000-09-07 02:40:37 +00001337\begin{tableiii}{l|l|l}{textrm}%
1338 {RPM \file{.spec} file option or section}%
1339 {\command{bdist\_rpm} option}%
1340 {default value}
1341 \lineiii{Release}{\option{release}}{``1''}
1342 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1343 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001344 \lineiii{Packager}{\option{packager}}{(none)}
1345 \lineiii{Provides}{\option{provides}}{(none)}
1346 \lineiii{Requires}{\option{requires}}{(none)}
1347 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1348 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001349 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1350 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1351 \lineiii{Icon}{\option{icon}}{(none)}
1352\end{tableiii}
Fred Drake781380c2004-02-19 23:17:46 +00001353
Greg Wardb6528972000-09-07 02:40:37 +00001354Obviously, supplying even a few of these options on the command-line
1355would be tedious and error-prone, so it's usually best to put them in
1356the setup configuration file, \file{setup.cfg}---see
1357section~\ref{setup-config}. If you distribute or package many Python
1358module distributions, you might want to put options that apply to all of
1359them in your personal Distutils configuration file
1360(\file{\textasciitilde/.pydistutils.cfg}).
1361
1362There are three steps to building a binary RPM package, all of which are
1363handled automatically by the Distutils:
Fred Drake781380c2004-02-19 23:17:46 +00001364
Greg Wardb6528972000-09-07 02:40:37 +00001365\begin{enumerate}
1366\item create a \file{.spec} file, which describes the package (analogous
1367 to the Distutils setup script; in fact, much of the information in the
1368 setup script winds up in the \file{.spec} file)
1369\item create the source RPM
1370\item create the ``binary'' RPM (which may or may not contain binary
1371 code, depending on whether your module distribution contains Python
1372 extensions)
1373\end{enumerate}
Fred Drake781380c2004-02-19 23:17:46 +00001374
Greg Wardb6528972000-09-07 02:40:37 +00001375Normally, RPM bundles the last two steps together; when you use the
1376Distutils, all three steps are typically bundled together.
1377
1378If you wish, you can separate these three steps. You can use the
Fred Drake781380c2004-02-19 23:17:46 +00001379\longprogramopt{spec-only} option to make \command{bdist_rpm} just
Greg Wardb6528972000-09-07 02:40:37 +00001380create the \file{.spec} file and exit; in this case, the \file{.spec}
1381file will be written to the ``distribution directory''---normally
1382\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1383option. (Normally, the \file{.spec} file winds up deep in the ``build
Fred Drake781380c2004-02-19 23:17:46 +00001384tree,'' in a temporary directory created by \command{bdist_rpm}.)
Greg Wardb6528972000-09-07 02:40:37 +00001385
Fred Drake781380c2004-02-19 23:17:46 +00001386% \XXX{this isn't implemented yet---is it needed?!}
1387% You can also specify a custom \file{.spec} file with the
1388% \longprogramopt{spec-file} option; used in conjunction with
1389% \longprogramopt{spec-only}, this gives you an opportunity to customize
1390% the \file{.spec} file manually:
1391%
1392% \begin{verbatim}
1393% > python setup.py bdist_rpm --spec-only
1394% # ...edit dist/FooBar-1.0.spec
1395% > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1396% \end{verbatim}
1397%
1398% (Although a better way to do this is probably to override the standard
1399% \command{bdist\_rpm} command with one that writes whatever else you want
1400% to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001401
1402
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001403\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001404\label{creating-wininst}
1405
Thomas Hellere61f3652002-11-15 20:13:26 +00001406Executable installers are the natural format for binary distributions
1407on Windows. They display a nice graphical user interface, display
1408some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001409from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001410options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001411
Thomas Hellere61f3652002-11-15 20:13:26 +00001412Since the metadata is taken from the setup script, creating Windows
1413installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001414
Thomas Heller5f52f722001-02-19 17:48:03 +00001415\begin{verbatim}
1416python setup.py bdist_wininst
1417\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001418
Thomas Heller36343f62002-11-15 19:20:56 +00001419or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001420
Thomas Heller5f52f722001-02-19 17:48:03 +00001421\begin{verbatim}
1422python setup.py bdist --formats=wininst
1423\end{verbatim}
1424
Thomas Hellere61f3652002-11-15 20:13:26 +00001425If you have a pure module distribution (only containing pure Python
1426modules and packages), the resulting installer will be version
1427independent and have a name like \file{foo-1.0.win32.exe}. These
Fred Drakec54d9252004-02-19 22:16:05 +00001428installers can even be created on \UNIX{} or Mac OS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001429
1430If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001431created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001432The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001433\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001434for every Python version you want to support.
1435
1436The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001437installation on the target system in normal and optimizing mode. If
1438you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001439\command{bdist_wininst} command with the
1440\longprogramopt{no-target-compile} and/or the
1441\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001442
Fred Drake0e9bfa32002-11-15 20:34:52 +00001443By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001444when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001445a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001446
1447The installer will also display a large title on the desktop
1448background window when it is run, which is constructed from the name
1449of your distribution and the version number. This can be changed to
1450another text by using the \longprogramopt{title} option.
1451
1452The installer file will be written to the ``distribution directory''
1453--- normally \file{dist/}, but customizable with the
1454\longprogramopt{dist-dir} option.
1455
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001456\subsubsection{The Postinstallation script}
1457\label{postinstallation-script}
1458
1459Starting with Python 2.3, a postinstallation script can be specified
1460which the \longprogramopt{install-script} option. The basename of the
1461script must be specified, and the script filename must also be listed
1462in the scripts argument to the setup function.
1463
1464This script will be run at installation time on the target system
Fred Drakec54d9252004-02-19 22:16:05 +00001465after all the files have been copied, with \code{argv[1]} set to
1466\programopt{-install}, and again at uninstallation time before the
1467files are removed with \code{argv[1]} set to \programopt{-remove}.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001468
1469The installation script runs embedded in the windows installer, every
Fred Drakec54d9252004-02-19 22:16:05 +00001470output (\code{sys.stdout}, \code{sys.stderr}) is redirected into a
1471buffer and will be displayed in the GUI after the script has finished.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001472
Fred Drakea9ee0da2004-02-19 22:28:15 +00001473Some functions especially useful in this context are available as
1474additional built-in functions in the installation script.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001475
Fred Drakea9ee0da2004-02-19 22:28:15 +00001476\begin{funcdesc}{directory_created}{path}
1477\funcline{file_created}{path}
1478 These functions should be called when a directory or file is created
1479 by the postinstall script at installation time. It will register
1480 \var{path} with the uninstaller, so that it will be removed when the
1481 distribution is uninstalled. To be safe, directories are only removed
1482 if they are empty.
1483\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001484
Fred Drakea9ee0da2004-02-19 22:28:15 +00001485\begin{funcdesc}{get_special_folder_path}{csidl_string}
1486 This function can be used to retrieve special folder locations on
1487 Windows like the Start Menu or the Desktop. It returns the full
1488 path to the folder. \var{csidl_string} must be one of the following
1489 strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001490
1491\begin{verbatim}
1492"CSIDL_APPDATA"
1493
1494"CSIDL_COMMON_STARTMENU"
1495"CSIDL_STARTMENU"
1496
1497"CSIDL_COMMON_DESKTOPDIRECTORY"
1498"CSIDL_DESKTOPDIRECTORY"
1499
1500"CSIDL_COMMON_STARTUP"
1501"CSIDL_STARTUP"
1502
1503"CSIDL_COMMON_PROGRAMS"
1504"CSIDL_PROGRAMS"
1505
1506"CSIDL_FONTS"
1507\end{verbatim}
1508
Fred Drakea9ee0da2004-02-19 22:28:15 +00001509 If the folder cannot be retrieved, \exception{OSError} is raised.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001510
Fred Drakea9ee0da2004-02-19 22:28:15 +00001511 Which folders are available depends on the exact Windows version,
1512 and probably also the configuration. For details refer to
1513 Microsoft's documentation of the
1514 \cfunction{SHGetSpecialFolderPath()} function.
1515\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001516
Fred Drakea9ee0da2004-02-19 22:28:15 +00001517\begin{funcdesc}{create_shortcut}{target, description,
1518 filename\optional{,
1519 arguments\optional{,
1520 workdir\optional{,
1521 iconpath\optional{, iconindex}}}}}
1522 This function creates a shortcut.
1523 \var{target} is the path to the program to be started by the shortcut.
1524 \var{description} is the description of the sortcut.
1525 \var{filename} is the title of the shortcut that the user will see.
1526 \var{arguments} specifies the command line arguments, if any.
1527 \var{workdir} is the working directory for the program.
1528 \var{iconpath} is the file containing the icon for the shortcut,
1529 and \var{iconindex} is the index of the icon in the file
1530 \var{iconpath}. Again, for details consult the Microsoft
1531 documentation for the \class{IShellLink} interface.
1532\end{funcdesc}
Greg Wardb6528972000-09-07 02:40:37 +00001533
Fred Drake211a2eb2004-03-22 21:44:43 +00001534\chapter{Registering with the Package Index}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001535\label{package-index}
1536
1537The Python Package Index (PyPI) holds meta-data describing distributions
1538packaged with distutils. The distutils command \command{register} is
1539used to submit your distribution's meta-data to the index. It is invoked
1540as follows:
1541
1542\begin{verbatim}
1543python setup.py register
1544\end{verbatim}
1545
1546Distutils will respond with the following prompt:
1547
1548\begin{verbatim}
1549running register
1550We need to know who you are, so please choose either:
1551 1. use your existing login,
1552 2. register as a new user,
1553 3. have the server generate a new password for you (and email it to you), or
1554 4. quit
1555Your selection [default 1]:
1556\end{verbatim}
1557
1558\noindent Note: if your username and password are saved locally, you will
1559not see this menu.
1560
1561If you have not registered with PyPI, then you will need to do so now. You
1562should choose option 2, and enter your details as required. Soon after
1563submitting your details, you will receive an email which will be used to
1564confirm your registration.
1565
1566Once you are registered, you may choose option 1 from the menu. You will
1567be prompted for your PyPI username and password, and \command{register}
1568will then submit your meta-data to the index.
1569
1570You may submit any number of versions of your distribution to the index. If
1571you alter the meta-data for a particular version, you may submit it again
1572and the index will be updated.
1573
1574PyPI holds a record for each (name, version) combination submitted. The
1575first user to submit information for a given name is designated the Owner
1576of that name. They may submit changes through the \command{register}
1577command or through the web interface. They may also designate other users
1578as Owners or Maintainers. Maintainers may edit the package information, but
1579not designate other Owners or Maintainers.
1580
1581By default PyPI will list all versions of a given package. To hide certain
1582versions, the Hidden property should be set to yes. This must be edited
1583through the web interface.
1584
1585
1586
Fred Drake211a2eb2004-03-22 21:44:43 +00001587\chapter{Examples}
Greg Ward007c04a2002-05-10 14:45:59 +00001588\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001589
Fred Drake211a2eb2004-03-22 21:44:43 +00001590\section{Pure Python distribution (by module)}
Greg Ward007c04a2002-05-10 14:45:59 +00001591\label{pure-mod}
1592
1593If you're just distributing a couple of modules, especially if they
1594don't live in a particular package, you can specify them individually
1595using the \option{py\_modules} option in the setup script.
1596
1597In the simplest case, you'll have two files to worry about: a setup
1598script and the single module you're distributing, \file{foo.py} in this
1599example:
1600\begin{verbatim}
1601<root>/
1602 setup.py
1603 foo.py
1604\end{verbatim}
1605(In all diagrams in this section, \verb|<root>| will refer to the
1606distribution root directory.) A minimal setup script to describe this
1607situation would be:
1608\begin{verbatim}
1609from distutils.core import setup
1610setup(name = "foo", version = "1.0",
1611 py_modules = ["foo"])
1612\end{verbatim}
1613Note that the name of the distribution is specified independently with
1614the \option{name} option, and there's no rule that says it has to be the
1615same as the name of the sole module in the distribution (although that's
1616probably a good convention to follow). However, the distribution name
1617is used to generate filenames, so you should stick to letters, digits,
1618underscores, and hyphens.
1619
1620Since \option{py\_modules} is a list, you can of course specify multiple
1621modules, eg. if you're distributing modules \module{foo} and
1622\module{bar}, your setup might look like this:
1623\begin{verbatim}
1624<root>/
1625 setup.py
1626 foo.py
1627 bar.py
1628\end{verbatim}
1629and the setup script might be
1630\begin{verbatim}
1631from distutils.core import setup
1632setup(name = "foobar", version = "1.0",
1633 py_modules = ["foo", "bar"])
1634\end{verbatim}
1635
1636You can put module source files into another directory, but if you have
1637enough modules to do that, it's probably easier to specify modules by
1638package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001639
1640
Fred Drake211a2eb2004-03-22 21:44:43 +00001641\section{Pure Python distribution (by package)}
Greg Ward007c04a2002-05-10 14:45:59 +00001642\label{pure-pkg}
1643
1644If you have more than a couple of modules to distribute, especially if
1645they are in multiple packages, it's probably easier to specify whole
1646packages rather than individual modules. This works even if your
1647modules are not in a package; you can just tell the Distutils to process
1648modules from the root package, and that works the same as any other
1649package (except that you don't have to have an \file{\_\_init\_\_.py}
1650file).
1651
1652The setup script from the last example could also be written as
1653\begin{verbatim}
1654from distutils.core import setup
1655setup(name = "foobar", version = "1.0",
1656 packages = [""])
1657\end{verbatim}
1658(The empty string stands for the root package.)
1659
1660If those two files are moved into a subdirectory, but remain in the root
1661package, e.g.:
1662\begin{verbatim}
1663<root>/
1664 setup.py
1665 src/ foo.py
1666 bar.py
1667\end{verbatim}
1668then you would still specify the root package, but you have to tell the
1669Distutils where source files in the root package live:
1670\begin{verbatim}
1671from distutils.core import setup
1672setup(name = "foobar", version = "1.0",
1673 package_dir = {"": "src"},
1674 packages = [""])
1675\end{verbatim}
1676
1677More typically, though, you will want to distribute multiple modules in
1678the same package (or in sub-packages). For example, if the \module{foo}
1679and \module{bar} modules belong in package \module{foobar}, one way to
1680layout your source tree is
1681\begin{verbatim}
1682<root>/
1683 setup.py
1684 foobar/
1685 __init__.py
1686 foo.py
1687 bar.py
1688\end{verbatim}
1689This is in fact the default layout expected by the Distutils, and the
1690one that requires the least work to describe in your setup script:
1691\begin{verbatim}
1692from distutils.core import setup
1693setup(name = "foobar", version = "1.0",
1694 packages = ["foobar"])
1695\end{verbatim}
1696
1697If you want to put modules in directories not named for their package,
1698then you need to use the \option{package\_dir} option again. For
1699example, if the \file{src} directory holds modules in the
1700\module{foobar} package:
1701\begin{verbatim}
1702<root>/
1703 setup.py
1704 src/
1705 __init__.py
1706 foo.py
1707 bar.py
1708\end{verbatim}
1709an appropriate setup script would be
1710\begin{verbatim}
1711from distutils.core import setup
1712setup(name = "foobar", version = "1.0",
1713 package_dir = {"foobar" : "src"},
1714 packages = ["foobar"])
1715\end{verbatim}
1716
1717Or, you might put modules from your main package right in the
1718distribution root:
1719\begin{verbatim}
1720<root>/
1721 setup.py
1722 __init__.py
1723 foo.py
1724 bar.py
1725\end{verbatim}
1726in which case your setup script would be
1727\begin{verbatim}
1728from distutils.core import setup
1729setup(name = "foobar", version = "1.0",
1730 package_dir = {"foobar" : ""},
1731 packages = ["foobar"])
1732\end{verbatim}
1733(The empty string also stands for the current directory.)
1734
1735If you have sub-packages, they must be explicitly listed in
1736\option{packages}, but any entries in \option{package\_dir}
1737automatically extend to sub-packages. (In other words, the Distutils
1738does \emph{not} scan your source tree, trying to figure out which
1739directories correspond to Python packages by looking for
1740\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1741sub-package:
1742\begin{verbatim}
1743<root>/
1744 setup.py
1745 foobar/
1746 __init__.py
1747 foo.py
1748 bar.py
1749 subfoo/
1750 __init__.py
1751 blah.py
1752\end{verbatim}
1753then the corresponding setup script would be
1754\begin{verbatim}
1755from distutils.core import setup
1756setup(name = "foobar", version = "1.0",
1757 packages = ["foobar", "foobar.subfoo"])
1758\end{verbatim}
1759(Again, the empty string in \option{package\_dir} stands for the current
1760directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001761
1762
Fred Drake211a2eb2004-03-22 21:44:43 +00001763\section{Single extension module}
Greg Ward007c04a2002-05-10 14:45:59 +00001764\label{single-ext}
1765
1766Extension modules are specified using the \option{ext\_modules} option.
1767\option{package\_dir} has no effect on where extension source files are
1768found; it only affects the source for pure Python modules. The simplest
1769case, a single extension module in a single C source file, is:
1770\begin{verbatim}
1771<root>/
1772 setup.py
1773 foo.c
1774\end{verbatim}
1775If the \module{foo} extension belongs in the root package, the setup
1776script for this could be
1777\begin{verbatim}
1778from distutils.core import setup
1779setup(name = "foobar", version = "1.0",
1780 ext_modules = [Extension("foo", ["foo.c"])])
1781\end{verbatim}
1782
1783If the extension actually belongs in a package, say \module{foopkg},
1784then
1785
1786With exactly the same source tree layout, this extension can be put in
1787the \module{foopkg} package simply by changing the name of the
1788extension:
1789\begin{verbatim}
1790from distutils.core import setup
1791setup(name = "foobar", version = "1.0",
1792 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1793\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001794
1795
Fred Drake211a2eb2004-03-22 21:44:43 +00001796%\section{Multiple extension modules}
Fred Drakea09262e2001-03-01 18:35:43 +00001797%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001798
1799
Fred Drake211a2eb2004-03-22 21:44:43 +00001800%\section{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001801
1802
Fred Drake211a2eb2004-03-22 21:44:43 +00001803%\chapter{Extending the Distutils}
Fred Drakea09262e2001-03-01 18:35:43 +00001804%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001805
1806
Fred Drake211a2eb2004-03-22 21:44:43 +00001807%\section{Extending existing commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001808%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001809
1810
Fred Drake211a2eb2004-03-22 21:44:43 +00001811%\section{Writing new commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001812%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001813
Fred Drakea09262e2001-03-01 18:35:43 +00001814%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001815
Greg Ward4a9e7222000-04-25 02:57:36 +00001816
1817
Fred Drake211a2eb2004-03-22 21:44:43 +00001818\chapter{Command Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001819\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001820
1821
Fred Drakea09262e2001-03-01 18:35:43 +00001822%\subsection{Building modules: the \protect\command{build} command family}
1823%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001824
Fred Drakea09262e2001-03-01 18:35:43 +00001825%\subsubsection{\protect\command{build}}
1826%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001827
Fred Drakea09262e2001-03-01 18:35:43 +00001828%\subsubsection{\protect\command{build\_py}}
1829%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001830
Fred Drakea09262e2001-03-01 18:35:43 +00001831%\subsubsection{\protect\command{build\_ext}}
1832%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001833
Fred Drakea09262e2001-03-01 18:35:43 +00001834%\subsubsection{\protect\command{build\_clib}}
1835%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001836
1837
Fred Drake211a2eb2004-03-22 21:44:43 +00001838\section{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001839\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001840
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001841The install command ensures that the build commands have been run and then
1842runs the subcommands \command{install\_lib},
1843\command{install\_data} and
1844\command{install\_scripts}.
1845
Fred Drakea09262e2001-03-01 18:35:43 +00001846%\subsubsection{\protect\command{install\_lib}}
1847%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001848
Fred Drake211a2eb2004-03-22 21:44:43 +00001849\subsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001850\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001851This command installs all data files provided with the distribution.
1852
Fred Drake211a2eb2004-03-22 21:44:43 +00001853\subsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001854\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001855This command installs all (Python) scripts in the distribution.
1856
Greg Ward16aafcd2000-04-09 04:06:44 +00001857
Fred Drakea09262e2001-03-01 18:35:43 +00001858%\subsection{Cleaning up: the \protect\command{clean} command}
1859%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001860
1861
Fred Drake211a2eb2004-03-22 21:44:43 +00001862\section{Creating a source distribution: the
Fred Drakeeff9a872000-10-26 16:41:03 +00001863 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001864\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001865
1866
1867\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001868
Greg Ward16aafcd2000-04-09 04:06:44 +00001869The manifest template commands are:
Fred Drake781380c2004-02-19 23:17:46 +00001870
Greg Ward16aafcd2000-04-09 04:06:44 +00001871\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001872 \lineii{include \var{pat1} \var{pat2} ... }
1873 {include all files matching any of the listed patterns}
1874 \lineii{exclude \var{pat1} \var{pat2} ... }
1875 {exclude all files matching any of the listed patterns}
1876 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1877 {include all files under \var{dir} matching any of the listed patterns}
1878 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1879 {exclude all files under \var{dir} matching any of the listed patterns}
1880 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001881 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001882 any of the listed patterns}
1883 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001884 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001885 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001886 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1887 \lineii{graft \var{dir}}{include all files under \var{dir}}
1888\end{tableii}
Fred Drake781380c2004-02-19 23:17:46 +00001889
Fred Drakeeff9a872000-10-26 16:41:03 +00001890The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001891sequence of regular filename characters, \code{?} matches any single
1892regular filename character, and \code{[\var{range}]} matches any of the
1893characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001894\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001895platform-specific: on \UNIX{} it is anything except slash; on Windows
Fred Drake781380c2004-02-19 23:17:46 +00001896anything except backslash or colon; on Mac OS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001897
Fred Drake781380c2004-02-19 23:17:46 +00001898\XXX{Windows and Mac OS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001899
1900
Fred Drake211a2eb2004-03-22 21:44:43 +00001901%\section{Creating a built distribution: the
Fred Drakea09262e2001-03-01 18:35:43 +00001902% \protect\command{bdist} command family}
1903%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001904
1905
Fred Drake211a2eb2004-03-22 21:44:43 +00001906%\subsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001907
Fred Drake211a2eb2004-03-22 21:44:43 +00001908%\subsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001909
Fred Drake211a2eb2004-03-22 21:44:43 +00001910%\subsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001911
Fred Drake211a2eb2004-03-22 21:44:43 +00001912%\subsection{\protect\command{bdist\_wininst}}
Fred Drakeab70b382001-08-02 15:13:15 +00001913
1914
1915\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001916
1917
Greg Wardabc52162000-02-26 00:52:48 +00001918\end{document}