blob: 641c9e068044f476c0d72950cf967c069a180ba1 [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
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
43\section{Introduction}
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
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000079\subsection{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
181\subsection{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
220\subsection{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
248\section{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
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000298this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000299mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000300path, the opposite of the MacOS 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
Greg Ward16aafcd2000-04-09 04:06:44 +0000815\section{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
Greg Ward16aafcd2000-04-09 04:06:44 +0000952\section{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.
967\XXX{no MacOS 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:
Greg Ward46b98e32000-04-14 01:53:36 +0000977\begin{tableiii}{l|l|c}{code}%
978 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000979 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
980 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000981 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000982 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000983 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000984\end{tableiii}
985
986\noindent Notes:
987\begin{description}
988\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000989\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000990\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000991 \module{zipfile} module (part of the standard Python library since
992 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000993\item[(4)] requires external utilities: \program{tar} and possibly one
994 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000995\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000996
997
Greg Ward54589d42000-09-06 01:37:35 +0000998
999\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001000\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001001
Greg Ward54589d42000-09-06 01:37:35 +00001002If you don't supply an explicit list of files (or instructions on how to
1003generate one), the \command{sdist} command puts a minimal default set
1004into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001005\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001006\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001007 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001008\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001009 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +00001010 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +00001011\item anything that looks like a test script: \file{test/test*.py}
1012 (currently, the Distutils don't do anything with test scripts except
1013 include them in source distributions, but in the future there will be
1014 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001015\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1016 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001017\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001018
Greg Ward16aafcd2000-04-09 04:06:44 +00001019Sometimes this is enough, but usually you will want to specify
1020additional files to distribute. The typical way to do this is to write
1021a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001022manifest template is just a list of instructions for how to generate
1023your manifest file, \file{MANIFEST}, which is the exact list of files to
1024include in your source distribution. The \command{sdist} command
1025processes this template and generates a manifest based on its
1026instructions and what it finds in the filesystem.
1027
1028If you prefer to roll your own manifest file, the format is simple: one
1029filename per line, regular files (or symlinks to them) only. If you do
1030supply your own \file{MANIFEST}, you must specify everything: the
1031default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001032
1033The manifest template has one command per line, where each command
1034specifies a set of files to include or exclude from the source
1035distribution. For an example, again we turn to the Distutils' own
1036manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001037
Greg Ward16aafcd2000-04-09 04:06:44 +00001038\begin{verbatim}
1039include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001040recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001041prune examples/sample?/build
1042\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001043
Greg Ward16aafcd2000-04-09 04:06:44 +00001044The meanings should be fairly clear: include all files in the
1045distribution root matching \code{*.txt}, all files anywhere under the
1046\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001047exclude all directories matching \code{examples/sample?/build}. All of
1048this is done \emph{after} the standard include set, so you can exclude
1049files from the standard set with explicit instructions in the manifest
1050template. (Or, you can use the \longprogramopt{no-defaults} option to
1051disable the standard set entirely.) There are several other commands
1052available in the manifest template mini-language; see
1053section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001054
Greg Ward54589d42000-09-06 01:37:35 +00001055The order of commands in the manifest template matters: initially, we
1056have the list of default files as described above, and each command in
1057the template adds to or removes from that list of files. Once we have
1058fully processed the manifest template, we remove files that should not
1059be included in the source distribution:
1060\begin{itemize}
1061\item all files in the Distutils ``build'' tree (default \file{build/})
1062\item all files in directories named \file{RCS} or \file{CVS}
1063\end{itemize}
1064Now we have our complete list of files, which is written to the manifest
1065for future reference, and then used to build the source distribution
1066archive(s).
1067
1068You can disable the default set of included files with the
1069\longprogramopt{no-defaults} option, and you can disable the standard
1070exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001071
Greg Ward46b98e32000-04-14 01:53:36 +00001072Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001073\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001074Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001075\begin{enumerate}
1076\item include all Python source files in the \file{distutils} and
1077 \file{distutils/command} subdirectories (because packages
1078 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001079 \option{packages} option in the setup script---see
1080 section~\ref{setup-script})
1081\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1082 (standard files)
1083\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001084\item include \file{*.txt} in the distribution root (this will find
1085 \file{README.txt} a second time, but such redundancies are weeded out
1086 later)
Greg Ward54589d42000-09-06 01:37:35 +00001087\item include anything matching \file{*.txt} or \file{*.py} in the
1088 sub-tree under \file{examples},
1089\item exclude all files in the sub-trees starting at directories
1090 matching \file{examples/sample?/build}---this may exclude files
1091 included by the previous two steps, so it's important that the
1092 \code{prune} command in the manifest template comes after the
1093 \code{recursive-include} command
1094\item exclude the entire \file{build} tree, and any \file{RCS} or
1095 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001096\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001097Just like in the setup script, file and directory names in the manifest
1098template should always be slash-separated; the Distutils will take care
1099of converting them to the standard representation on your platform.
1100That way, the manifest template is portable across operating systems.
1101
Greg Ward16aafcd2000-04-09 04:06:44 +00001102
1103\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001104\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001105
1106The normal course of operations for the \command{sdist} command is as
1107follows:
1108\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001109\item if the manifest file, \file{MANIFEST} doesn't exist, read
1110 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001111\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001112 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001113\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1114 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1115 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001116\item use the list of files now in \file{MANIFEST} (either just
1117 generated or read in) to create the source distribution archive(s)
1118\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001119There are a couple of options that modify this behaviour. First, use
1120the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001121disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001122
Greg Ward54589d42000-09-06 01:37:35 +00001123Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001124example, if you have added or removed files or directories that match an
1125existing pattern in the manifest template, you should regenerate the
1126manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001127
Greg Ward16aafcd2000-04-09 04:06:44 +00001128\begin{verbatim}
1129python setup.py sdist --force-manifest
1130\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001131
1132Or, you might just want to (re)generate the manifest, but not create a
1133source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001134
Greg Ward16aafcd2000-04-09 04:06:44 +00001135\begin{verbatim}
1136python setup.py sdist --manifest-only
1137\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001138
Greg Ward54589d42000-09-06 01:37:35 +00001139\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1140\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1141\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001142
1143
1144\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001145\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001146
Greg Ward46b98e32000-04-14 01:53:36 +00001147A ``built distribution'' is what you're probably used to thinking of
1148either as a ``binary package'' or an ``installer'' (depending on your
1149background). It's not necessarily binary, though, because it might
1150contain only Python source code and/or byte-code; and we don't call it a
1151package, because that word is already spoken for in Python. (And
1152``installer'' is a term specific to the Windows world. \XXX{do Mac
1153 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001154
Greg Ward46b98e32000-04-14 01:53:36 +00001155A built distribution is how you make life as easy as possible for
1156installers of your module distribution: for users of RPM-based Linux
1157systems, it's a binary RPM; for Windows users, it's an executable
1158installer; for Debian-based Linux users, it's a Debian package; and so
1159forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001160distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001161designed to enable module developers to concentrate on their
1162specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001163intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001164distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001165are packagers.
1166
1167Of course, the module developer could be his own packager; or the
1168packager could be a volunteer ``out there'' somewhere who has access to
1169a platform which the original developer does not; or it could be
1170software periodically grabbing new source distributions and turning them
1171into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001172access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001173setup script and the \command{bdist} command family to generate built
1174distributions.
1175
1176As a simple example, if I run the following command in the Distutils
1177source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001178
Greg Ward46b98e32000-04-14 01:53:36 +00001179\begin{verbatim}
1180python setup.py bdist
1181\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001182
Greg Ward46b98e32000-04-14 01:53:36 +00001183then the Distutils builds my module distribution (the Distutils itself
1184in this case), does a ``fake'' installation (also in the \file{build}
1185directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001186platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001187file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001188file is considered ``dumb'' because it has to be unpacked in a specific
1189location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001190
Fred Drakeeff9a872000-10-26 16:41:03 +00001191Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001192\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001193from the right place installs the Distutils just as though you had
1194downloaded the source distribution and run \code{python setup.py
1195 install}. (The ``right place'' is either the root of the filesystem or
1196Python's \filevar{prefix} directory, depending on the options given to
1197the \command{bdist\_dumb} command; the default is to make dumb
1198distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001199
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001200Obviously, for pure Python distributions, this isn't any simpler than
1201just running \code{python setup.py install}---but for non-pure
1202distributions, which include extensions that would need to be
1203compiled, it can mean the difference between someone being able to use
1204your extensions or not. And creating ``smart'' built distributions,
1205such as an RPM package or an executable installer for Windows, is far
1206more convenient for users even if your distribution doesn't include
1207any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001208
Greg Wardb6528972000-09-07 02:40:37 +00001209The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001210similar to the \command{sdist} command, which you can use to select the
1211types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001212
Greg Ward46b98e32000-04-14 01:53:36 +00001213\begin{verbatim}
1214python setup.py bdist --format=zip
1215\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001216
Fred Drakeeff9a872000-10-26 16:41:03 +00001217would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001218\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001219unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001220
1221The available formats for built distributions are:
1222\begin{tableiii}{l|l|c}{code}%
1223 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001224 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1225 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1226 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1227 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1228 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001229 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1230 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1231 \lineiii{rpm}{RPM}{(5)}
1232% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001233 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001234\end{tableiii}
1235
1236\noindent Notes:
1237\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001238\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001239\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001240\item[(3)] requires external utilities: \program{tar} and possibly one
1241 of \program{gzip}, \program{bzip2}, or \program{compress}
1242\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001243 \module{zipfile} module (part of the standard Python library since
1244 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001245\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1246 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001247\end{description}
1248
1249You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001250\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001251directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001252\command{bdist} ``sub-commands'' actually generate several similar
1253formats; for instance, the \command{bdist\_dumb} command generates all
1254the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1255\code{zip}), and \command{bdist\_rpm} generates both binary and source
1256RPMs. The \command{bdist} sub-commands, and the formats generated by
1257each, are:
1258\begin{tableii}{l|l}{command}%
1259 {Command}{Formats}
1260 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1261 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001262 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001263\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001264
Greg Wardb6528972000-09-07 02:40:37 +00001265The following sections give details on the individual \command{bdist\_*}
1266commands.
1267
1268
1269\subsection{Creating dumb built distributions}
1270\label{creating-dumb}
1271
1272\XXX{Need to document absolute vs. prefix-relative packages here, but
1273 first I have to implement it!}
1274
1275
1276\subsection{Creating RPM packages}
1277\label{creating-rpms}
1278
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001279The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001280Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1281RPM-based Linux distributions) is your usual environment, creating RPM
1282packages for other users of that same distribution is trivial.
1283Depending on the complexity of your module distribution and differences
1284between Linux distributions, you may also be able to create RPMs that
1285work on different RPM-based distributions.
1286
1287The usual way to create an RPM of your module distribution is to run the
1288\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001289
Greg Wardb6528972000-09-07 02:40:37 +00001290\begin{verbatim}
1291python setup.py bdist_rpm
1292\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001293
Greg Wardb6528972000-09-07 02:40:37 +00001294or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001295
Greg Wardb6528972000-09-07 02:40:37 +00001296\begin{verbatim}
1297python setup.py bdist --formats=rpm
1298\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001299
Greg Wardb6528972000-09-07 02:40:37 +00001300The former allows you to specify RPM-specific options; the latter allows
1301you to easily specify multiple formats in one run. If you need to do
1302both, you can explicitly specify multiple \command{bdist\_*} commands
1303and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001304
Greg Wardb6528972000-09-07 02:40:37 +00001305\begin{verbatim}
1306python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1307 bdist_wininst --target_version="2.0"
1308\end{verbatim}
1309
1310Creating RPM packages is driven by a \file{.spec} file, much as using
1311the Distutils is driven by the setup script. To make your life easier,
1312the \command{bdist\_rpm} command normally creates a \file{.spec} file
1313based on the information you supply in the setup script, on the command
1314line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001315sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001316script as follows:
1317\begin{tableii}{l|l}{textrm}%
1318 {RPM \file{.spec} file option or section}{Distutils setup script option}
1319 \lineii{Name}{\option{name}}
1320 \lineii{Summary (in preamble)}{\option{description}}
1321 \lineii{Version}{\option{version}}
1322 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1323 \option{maintainer} and \option{maintainer\_email}}
1324 \lineii{Copyright}{\option{licence}}
1325 \lineii{Url}{\option{url}}
1326 \lineii{\%description (section)}{\option{long\_description}}
1327\end{tableii}
1328
1329Additionally, there many options in \file{.spec} files that don't have
1330corresponding options in the setup script. Most of these are handled
1331through options to the \command{bdist\_rpm} command as follows:
1332\begin{tableiii}{l|l|l}{textrm}%
1333 {RPM \file{.spec} file option or section}%
1334 {\command{bdist\_rpm} option}%
1335 {default value}
1336 \lineiii{Release}{\option{release}}{``1''}
1337 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1338 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001339 \lineiii{Packager}{\option{packager}}{(none)}
1340 \lineiii{Provides}{\option{provides}}{(none)}
1341 \lineiii{Requires}{\option{requires}}{(none)}
1342 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1343 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001344 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1345 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1346 \lineiii{Icon}{\option{icon}}{(none)}
1347\end{tableiii}
1348Obviously, supplying even a few of these options on the command-line
1349would be tedious and error-prone, so it's usually best to put them in
1350the setup configuration file, \file{setup.cfg}---see
1351section~\ref{setup-config}. If you distribute or package many Python
1352module distributions, you might want to put options that apply to all of
1353them in your personal Distutils configuration file
1354(\file{\textasciitilde/.pydistutils.cfg}).
1355
1356There are three steps to building a binary RPM package, all of which are
1357handled automatically by the Distutils:
1358\begin{enumerate}
1359\item create a \file{.spec} file, which describes the package (analogous
1360 to the Distutils setup script; in fact, much of the information in the
1361 setup script winds up in the \file{.spec} file)
1362\item create the source RPM
1363\item create the ``binary'' RPM (which may or may not contain binary
1364 code, depending on whether your module distribution contains Python
1365 extensions)
1366\end{enumerate}
1367Normally, RPM bundles the last two steps together; when you use the
1368Distutils, all three steps are typically bundled together.
1369
1370If you wish, you can separate these three steps. You can use the
1371\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1372create the \file{.spec} file and exit; in this case, the \file{.spec}
1373file will be written to the ``distribution directory''---normally
1374\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1375option. (Normally, the \file{.spec} file winds up deep in the ``build
1376tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1377
1378\XXX{this isn't implemented yet---is it needed?!}
1379You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001380\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001381\longprogramopt{spec-only}, this gives you an opportunity to customize
1382the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001383
Greg Wardb6528972000-09-07 02:40:37 +00001384\begin{verbatim}
1385> python setup.py bdist_rpm --spec-only
1386# ...edit dist/FooBar-1.0.spec
1387> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1388\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001389
Greg Wardb6528972000-09-07 02:40:37 +00001390(Although a better way to do this is probably to override the standard
1391\command{bdist\_rpm} command with one that writes whatever else you want
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +00001392to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001393
1394
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001395\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001396\label{creating-wininst}
1397
Thomas Hellere61f3652002-11-15 20:13:26 +00001398Executable installers are the natural format for binary distributions
1399on Windows. They display a nice graphical user interface, display
1400some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001401from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001402options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001403
Thomas Hellere61f3652002-11-15 20:13:26 +00001404Since the metadata is taken from the setup script, creating Windows
1405installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001406
Thomas Heller5f52f722001-02-19 17:48:03 +00001407\begin{verbatim}
1408python setup.py bdist_wininst
1409\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001410
Thomas Heller36343f62002-11-15 19:20:56 +00001411or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001412
Thomas Heller5f52f722001-02-19 17:48:03 +00001413\begin{verbatim}
1414python setup.py bdist --formats=wininst
1415\end{verbatim}
1416
Thomas Hellere61f3652002-11-15 20:13:26 +00001417If you have a pure module distribution (only containing pure Python
1418modules and packages), the resulting installer will be version
1419independent and have a name like \file{foo-1.0.win32.exe}. These
1420installers can even be created on \UNIX{} or MacOS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001421
1422If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001423created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001424The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001425\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001426for every Python version you want to support.
1427
1428The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001429installation on the target system in normal and optimizing mode. If
1430you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001431\command{bdist_wininst} command with the
1432\longprogramopt{no-target-compile} and/or the
1433\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001434
Fred Drake0e9bfa32002-11-15 20:34:52 +00001435By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001436when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001437a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001438
1439The installer will also display a large title on the desktop
1440background window when it is run, which is constructed from the name
1441of your distribution and the version number. This can be changed to
1442another text by using the \longprogramopt{title} option.
1443
1444The installer file will be written to the ``distribution directory''
1445--- normally \file{dist/}, but customizable with the
1446\longprogramopt{dist-dir} option.
1447
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001448\subsubsection{The Postinstallation script}
1449\label{postinstallation-script}
1450
1451Starting with Python 2.3, a postinstallation script can be specified
1452which the \longprogramopt{install-script} option. The basename of the
1453script must be specified, and the script filename must also be listed
1454in the scripts argument to the setup function.
1455
1456This script will be run at installation time on the target system
1457after all the files have been copied, with argv[1] set to '-install',
1458and again at uninstallation time before the files are removed with argv[1]
1459set to '-remove'.
1460
1461The installation script runs embedded in the windows installer, every
1462output (sys.stdout, sys.stderr) is redirected into a buffer and will
1463be displayed in the GUI after the script has finished.
1464
1465Some functions especially useful in this context are available in the
1466installation script.
1467
1468\begin{verbatim}
Thomas Heller41e28092003-10-16 19:40:48 +00001469directory_created(pathname)
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001470file_created(pathname)
1471\end{verbatim}
1472
1473These functions should be called when a directory or file is created
1474by the postinstall script at installation time. It will register the
1475pathname with the uninstaller, so that it will be removed when the
1476distribution is uninstalled. To be safe, directories are only removed
1477if they are empty.
1478
1479\begin{verbatim}
1480get_special_folder_path(csidl_string)
1481\end{verbatim}
1482
1483This function can be used to retrieve special folder locations on
1484Windows like the Start Menu or the Desktop. It returns the full path
Thomas Hellera425dbc2003-09-17 17:11:01 +00001485to the folder. 'csidl_string' must be one of the following strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001486
1487\begin{verbatim}
1488"CSIDL_APPDATA"
1489
1490"CSIDL_COMMON_STARTMENU"
1491"CSIDL_STARTMENU"
1492
1493"CSIDL_COMMON_DESKTOPDIRECTORY"
1494"CSIDL_DESKTOPDIRECTORY"
1495
1496"CSIDL_COMMON_STARTUP"
1497"CSIDL_STARTUP"
1498
1499"CSIDL_COMMON_PROGRAMS"
1500"CSIDL_PROGRAMS"
1501
1502"CSIDL_FONTS"
1503\end{verbatim}
1504
1505If the folder cannot be retrieved, OSError is raised.
1506
1507Which folders are available depends on the exact Windows version, and probably
1508also the configuration. For details refer to Microsoft's documentation of the
Thomas Heller63b4dd32002-12-12 19:35:00 +00001509\code{SHGetSpecialFolderPath} function.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001510
1511\begin{verbatim}
1512create_shortcut(target, description, filename[, arguments[,
1513 workdir[, iconpath[, iconindex]]]])
1514\end{verbatim}
1515
1516This function creates a shortcut.
Thomas Heller63b4dd32002-12-12 19:35:00 +00001517\var{target} is the path to the program to be started by the shortcut.
1518\var{description} is the description of the sortcut.
1519\var{filename} is the title of the shortcut that the user will see.
1520\var{arguments} specifies the command line arguments, if any.
1521\var{workdir} is the working directory for the program.
1522\var{iconpath} is the file containing the icon for the shortcut,
1523and \var{iconindex} is the index of the icon in the file
1524\var{iconpath}. Again, for details consult the Microsoft
1525documentation for the \code{IShellLink} interface.
Greg Wardb6528972000-09-07 02:40:37 +00001526
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001527\section{Registering with the Package Index}
1528\label{package-index}
1529
1530The Python Package Index (PyPI) holds meta-data describing distributions
1531packaged with distutils. The distutils command \command{register} is
1532used to submit your distribution's meta-data to the index. It is invoked
1533as follows:
1534
1535\begin{verbatim}
1536python setup.py register
1537\end{verbatim}
1538
1539Distutils will respond with the following prompt:
1540
1541\begin{verbatim}
1542running register
1543We need to know who you are, so please choose either:
1544 1. use your existing login,
1545 2. register as a new user,
1546 3. have the server generate a new password for you (and email it to you), or
1547 4. quit
1548Your selection [default 1]:
1549\end{verbatim}
1550
1551\noindent Note: if your username and password are saved locally, you will
1552not see this menu.
1553
1554If you have not registered with PyPI, then you will need to do so now. You
1555should choose option 2, and enter your details as required. Soon after
1556submitting your details, you will receive an email which will be used to
1557confirm your registration.
1558
1559Once you are registered, you may choose option 1 from the menu. You will
1560be prompted for your PyPI username and password, and \command{register}
1561will then submit your meta-data to the index.
1562
1563You may submit any number of versions of your distribution to the index. If
1564you alter the meta-data for a particular version, you may submit it again
1565and the index will be updated.
1566
1567PyPI holds a record for each (name, version) combination submitted. The
1568first user to submit information for a given name is designated the Owner
1569of that name. They may submit changes through the \command{register}
1570command or through the web interface. They may also designate other users
1571as Owners or Maintainers. Maintainers may edit the package information, but
1572not designate other Owners or Maintainers.
1573
1574By default PyPI will list all versions of a given package. To hide certain
1575versions, the Hidden property should be set to yes. This must be edited
1576through the web interface.
1577
1578
1579
Greg Ward007c04a2002-05-10 14:45:59 +00001580\section{Examples}
1581\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001582
Greg Ward007c04a2002-05-10 14:45:59 +00001583\subsection{Pure Python distribution (by module)}
1584\label{pure-mod}
1585
1586If you're just distributing a couple of modules, especially if they
1587don't live in a particular package, you can specify them individually
1588using the \option{py\_modules} option in the setup script.
1589
1590In the simplest case, you'll have two files to worry about: a setup
1591script and the single module you're distributing, \file{foo.py} in this
1592example:
1593\begin{verbatim}
1594<root>/
1595 setup.py
1596 foo.py
1597\end{verbatim}
1598(In all diagrams in this section, \verb|<root>| will refer to the
1599distribution root directory.) A minimal setup script to describe this
1600situation would be:
1601\begin{verbatim}
1602from distutils.core import setup
1603setup(name = "foo", version = "1.0",
1604 py_modules = ["foo"])
1605\end{verbatim}
1606Note that the name of the distribution is specified independently with
1607the \option{name} option, and there's no rule that says it has to be the
1608same as the name of the sole module in the distribution (although that's
1609probably a good convention to follow). However, the distribution name
1610is used to generate filenames, so you should stick to letters, digits,
1611underscores, and hyphens.
1612
1613Since \option{py\_modules} is a list, you can of course specify multiple
1614modules, eg. if you're distributing modules \module{foo} and
1615\module{bar}, your setup might look like this:
1616\begin{verbatim}
1617<root>/
1618 setup.py
1619 foo.py
1620 bar.py
1621\end{verbatim}
1622and the setup script might be
1623\begin{verbatim}
1624from distutils.core import setup
1625setup(name = "foobar", version = "1.0",
1626 py_modules = ["foo", "bar"])
1627\end{verbatim}
1628
1629You can put module source files into another directory, but if you have
1630enough modules to do that, it's probably easier to specify modules by
1631package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001632
1633
Greg Ward007c04a2002-05-10 14:45:59 +00001634\subsection{Pure Python distribution (by package)}
1635\label{pure-pkg}
1636
1637If you have more than a couple of modules to distribute, especially if
1638they are in multiple packages, it's probably easier to specify whole
1639packages rather than individual modules. This works even if your
1640modules are not in a package; you can just tell the Distutils to process
1641modules from the root package, and that works the same as any other
1642package (except that you don't have to have an \file{\_\_init\_\_.py}
1643file).
1644
1645The setup script from the last example could also be written as
1646\begin{verbatim}
1647from distutils.core import setup
1648setup(name = "foobar", version = "1.0",
1649 packages = [""])
1650\end{verbatim}
1651(The empty string stands for the root package.)
1652
1653If those two files are moved into a subdirectory, but remain in the root
1654package, e.g.:
1655\begin{verbatim}
1656<root>/
1657 setup.py
1658 src/ foo.py
1659 bar.py
1660\end{verbatim}
1661then you would still specify the root package, but you have to tell the
1662Distutils where source files in the root package live:
1663\begin{verbatim}
1664from distutils.core import setup
1665setup(name = "foobar", version = "1.0",
1666 package_dir = {"": "src"},
1667 packages = [""])
1668\end{verbatim}
1669
1670More typically, though, you will want to distribute multiple modules in
1671the same package (or in sub-packages). For example, if the \module{foo}
1672and \module{bar} modules belong in package \module{foobar}, one way to
1673layout your source tree is
1674\begin{verbatim}
1675<root>/
1676 setup.py
1677 foobar/
1678 __init__.py
1679 foo.py
1680 bar.py
1681\end{verbatim}
1682This is in fact the default layout expected by the Distutils, and the
1683one that requires the least work to describe in your setup script:
1684\begin{verbatim}
1685from distutils.core import setup
1686setup(name = "foobar", version = "1.0",
1687 packages = ["foobar"])
1688\end{verbatim}
1689
1690If you want to put modules in directories not named for their package,
1691then you need to use the \option{package\_dir} option again. For
1692example, if the \file{src} directory holds modules in the
1693\module{foobar} package:
1694\begin{verbatim}
1695<root>/
1696 setup.py
1697 src/
1698 __init__.py
1699 foo.py
1700 bar.py
1701\end{verbatim}
1702an appropriate setup script would be
1703\begin{verbatim}
1704from distutils.core import setup
1705setup(name = "foobar", version = "1.0",
1706 package_dir = {"foobar" : "src"},
1707 packages = ["foobar"])
1708\end{verbatim}
1709
1710Or, you might put modules from your main package right in the
1711distribution root:
1712\begin{verbatim}
1713<root>/
1714 setup.py
1715 __init__.py
1716 foo.py
1717 bar.py
1718\end{verbatim}
1719in which case your setup script would be
1720\begin{verbatim}
1721from distutils.core import setup
1722setup(name = "foobar", version = "1.0",
1723 package_dir = {"foobar" : ""},
1724 packages = ["foobar"])
1725\end{verbatim}
1726(The empty string also stands for the current directory.)
1727
1728If you have sub-packages, they must be explicitly listed in
1729\option{packages}, but any entries in \option{package\_dir}
1730automatically extend to sub-packages. (In other words, the Distutils
1731does \emph{not} scan your source tree, trying to figure out which
1732directories correspond to Python packages by looking for
1733\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1734sub-package:
1735\begin{verbatim}
1736<root>/
1737 setup.py
1738 foobar/
1739 __init__.py
1740 foo.py
1741 bar.py
1742 subfoo/
1743 __init__.py
1744 blah.py
1745\end{verbatim}
1746then the corresponding setup script would be
1747\begin{verbatim}
1748from distutils.core import setup
1749setup(name = "foobar", version = "1.0",
1750 packages = ["foobar", "foobar.subfoo"])
1751\end{verbatim}
1752(Again, the empty string in \option{package\_dir} stands for the current
1753directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001754
1755
Greg Ward007c04a2002-05-10 14:45:59 +00001756\subsection{Single extension module}
1757\label{single-ext}
1758
1759Extension modules are specified using the \option{ext\_modules} option.
1760\option{package\_dir} has no effect on where extension source files are
1761found; it only affects the source for pure Python modules. The simplest
1762case, a single extension module in a single C source file, is:
1763\begin{verbatim}
1764<root>/
1765 setup.py
1766 foo.c
1767\end{verbatim}
1768If the \module{foo} extension belongs in the root package, the setup
1769script for this could be
1770\begin{verbatim}
1771from distutils.core import setup
1772setup(name = "foobar", version = "1.0",
1773 ext_modules = [Extension("foo", ["foo.c"])])
1774\end{verbatim}
1775
1776If the extension actually belongs in a package, say \module{foopkg},
1777then
1778
1779With exactly the same source tree layout, this extension can be put in
1780the \module{foopkg} package simply by changing the name of the
1781extension:
1782\begin{verbatim}
1783from distutils.core import setup
1784setup(name = "foobar", version = "1.0",
1785 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1786\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001787
1788
Fred Drakea09262e2001-03-01 18:35:43 +00001789%\subsection{Multiple extension modules}
1790%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001791
1792
Fred Drakea09262e2001-03-01 18:35:43 +00001793%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001794
1795
Fred Drakea09262e2001-03-01 18:35:43 +00001796%\section{Extending the Distutils}
1797%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001798
1799
Fred Drakea09262e2001-03-01 18:35:43 +00001800%\subsection{Extending existing commands}
1801%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001802
1803
Fred Drakea09262e2001-03-01 18:35:43 +00001804%\subsection{Writing new commands}
1805%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001806
Fred Drakea09262e2001-03-01 18:35:43 +00001807%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001808
Greg Ward4a9e7222000-04-25 02:57:36 +00001809
1810
Greg Ward16aafcd2000-04-09 04:06:44 +00001811\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001812\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001813
1814
Fred Drakea09262e2001-03-01 18:35:43 +00001815%\subsection{Building modules: the \protect\command{build} command family}
1816%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001817
Fred Drakea09262e2001-03-01 18:35:43 +00001818%\subsubsection{\protect\command{build}}
1819%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001820
Fred Drakea09262e2001-03-01 18:35:43 +00001821%\subsubsection{\protect\command{build\_py}}
1822%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001823
Fred Drakea09262e2001-03-01 18:35:43 +00001824%\subsubsection{\protect\command{build\_ext}}
1825%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001826
Fred Drakea09262e2001-03-01 18:35:43 +00001827%\subsubsection{\protect\command{build\_clib}}
1828%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001829
1830
Greg Wardfacb8db2000-04-09 04:32:40 +00001831\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001832\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001833
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001834The install command ensures that the build commands have been run and then
1835runs the subcommands \command{install\_lib},
1836\command{install\_data} and
1837\command{install\_scripts}.
1838
Fred Drakea09262e2001-03-01 18:35:43 +00001839%\subsubsection{\protect\command{install\_lib}}
1840%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001841
1842\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001843\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001844This command installs all data files provided with the distribution.
1845
1846\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001847\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001848This command installs all (Python) scripts in the distribution.
1849
Greg Ward16aafcd2000-04-09 04:06:44 +00001850
Fred Drakea09262e2001-03-01 18:35:43 +00001851%\subsection{Cleaning up: the \protect\command{clean} command}
1852%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001853
1854
Fred Drakeeff9a872000-10-26 16:41:03 +00001855\subsection{Creating a source distribution: the
1856 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001857\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001858
1859
1860\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001861
Greg Ward16aafcd2000-04-09 04:06:44 +00001862The manifest template commands are:
1863\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001864 \lineii{include \var{pat1} \var{pat2} ... }
1865 {include all files matching any of the listed patterns}
1866 \lineii{exclude \var{pat1} \var{pat2} ... }
1867 {exclude all files matching any of the listed patterns}
1868 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1869 {include all files under \var{dir} matching any of the listed patterns}
1870 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1871 {exclude all files under \var{dir} matching any of the listed patterns}
1872 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001873 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001874 any of the listed patterns}
1875 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001876 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001877 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001878 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1879 \lineii{graft \var{dir}}{include all files under \var{dir}}
1880\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001881The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001882sequence of regular filename characters, \code{?} matches any single
1883regular filename character, and \code{[\var{range}]} matches any of the
1884characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001885\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001886platform-specific: on \UNIX{} it is anything except slash; on Windows
1887anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001888
Fred Drakeeff9a872000-10-26 16:41:03 +00001889\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001890
1891
Fred Drakea09262e2001-03-01 18:35:43 +00001892%\subsection{Creating a built distribution: the
1893% \protect\command{bdist} command family}
1894%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001895
1896
Fred Drakeab70b382001-08-02 15:13:15 +00001897%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001898
Fred Drakeab70b382001-08-02 15:13:15 +00001899%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001900
Fred Drakeab70b382001-08-02 15:13:15 +00001901%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001902
Fred Drakeab70b382001-08-02 15:13:15 +00001903%\subsubsection{\protect\command{bdist\_wininst}}
1904
1905
1906\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001907
1908
Greg Wardabc52162000-02-26 00:52:48 +00001909\end{document}