blob: 3c57373043b5450713e5ad7156abdc0452518ae8 [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
Greg Wardabc52162000-02-26 00:52:48 +000013\author{Greg Ward}
Fred Drakeb914ef02004-01-02 06:57:50 +000014\authoraddress{
15 \strong{Python Software Foundation}\\
16 Email: \email{distutils-sig@python.org}
17}
Greg Wardabc52162000-02-26 00:52:48 +000018
Greg Warde3cca262000-08-31 16:36:31 +000019\makeindex
Greg Ward16aafcd2000-04-09 04:06:44 +000020
Greg Wardabc52162000-02-26 00:52:48 +000021\begin{document}
22
Greg Wardfacb8db2000-04-09 04:32:40 +000023\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000024\begin{abstract}
25 \noindent
26 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000027 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000028 how to use the Distutils to make Python modules and extensions easily
29 available to a wider audience with very little overhead for
30 build/release/install mechanics.
31\end{abstract}
32
Fred Drakea09262e2001-03-01 18:35:43 +000033% The ugly "%begin{latexonly}" pseudo-environment supresses the table
34% of contents for HTML generation.
35%
36%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000037\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000038%end{latexonly}
39
Greg Ward16aafcd2000-04-09 04:06:44 +000040
41\section{Introduction}
Greg Warde78298a2000-04-28 17:12:24 +000042\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000043
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000044This document covers using the Distutils to distribute your Python
45modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000046you're looking for information on installing Python modules, you
47should refer to the \citetitle[../inst/inst.html]{Installing Python
48Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000049
50
Greg Wardfacb8db2000-04-09 04:32:40 +000051\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000052\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000053
54Using the Distutils is quite simple, both for module developers and for
55users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000056your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000057well-tested code, of course!) are:
58\begin{itemize}
59\item write a setup script (\file{setup.py} by convention)
60\item (optional) write a setup configuration file
61\item create a source distribution
62\item (optional) create one or more built (binary) distributions
63\end{itemize}
64Each of these tasks is covered in this document.
65
66Not all module developers have access to a multitude of platforms, so
67it's not always feasible to expect them to create a multitude of built
68distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000069\emph{packagers}, will arise to address this need. Packagers will take
70source distributions released by module developers, build them on one or
71more platforms, and release the resulting built distributions. Thus,
72users on the most popular platforms will be able to install most popular
73Python module distributions in the most natural way for their platform,
74without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000075
76
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000077\subsection{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000078\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000079
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000080The setup script is usually quite simple, although since it's written
81in Python, there are no arbitrary limits to what you can do with it,
82though you should be careful about putting arbitrarily expensive
83operations in your setup script. Unlike, say, Autoconf-style configure
84scripts, the setup script may be run multiple times in the course of
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000085building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000086
87If all you want to do is distribute a module called \module{foo},
88contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000089simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000090
Greg Ward16aafcd2000-04-09 04:06:44 +000091\begin{verbatim}
92from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000093setup(name="foo",
94 version="1.0",
95 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000096\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +000097
Greg Ward16aafcd2000-04-09 04:06:44 +000098Some observations:
99\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000100\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000101 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000102\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000103 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000104 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000105\item modules are specified by module name, not filename (the same will
106 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000107\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000108 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000109 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000110\end{itemize}
111
Greg Ward370248d2000-06-24 01:45:47 +0000112To create a source distribution for this module, you would create a
113setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000114
Greg Ward16aafcd2000-04-09 04:06:44 +0000115\begin{verbatim}
116python setup.py sdist
117\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000118
Fred Drakeeff9a872000-10-26 16:41:03 +0000119which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000120Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000121\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
122\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000123
124If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000125to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
126and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000127
Greg Ward16aafcd2000-04-09 04:06:44 +0000128\begin{verbatim}
129python setup.py install
130\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000131
Greg Ward16aafcd2000-04-09 04:06:44 +0000132which will ultimately copy \file{foo.py} to the appropriate directory
133for third-party modules in their Python installation.
134
135This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000136Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000137user interface, i.e. the setup script. The difference is which
138Distutils \emph{commands} they use: the \command{sdist} command is
139almost exclusively for module developers, while \command{install} is
140more often for installers (although most developers will want to install
141their own code occasionally).
142
Greg Ward16aafcd2000-04-09 04:06:44 +0000143If you want to make things really easy for your users, you can create
144one or more built distributions for them. For instance, if you are
145running on a Windows machine, and want to make things easy for other
146Windows users, you can create an executable installer (the most
147appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000148\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000149
Greg Ward16aafcd2000-04-09 04:06:44 +0000150\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000151python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000152\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000153
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000154will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000155current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000156
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000157Other useful built distribution formats are RPM, implemented by the
158\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000159(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
160(\command{bdist_sdux}). For example, the following command will
161create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000162
Greg Ward1d8f57a2000-08-05 00:43:11 +0000163\begin{verbatim}
164python setup.py bdist_rpm
165\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000166
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000167(The \command{bdist\_rpm} command uses the \command{rpm} executable,
168therefore this has to be run on an RPM-based system such as Red Hat
169Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000170
171You can find out what distribution formats are available at any time by
172running
Fred Drakea09262e2001-03-01 18:35:43 +0000173
Greg Ward1d8f57a2000-08-05 00:43:11 +0000174\begin{verbatim}
175python setup.py bdist --help-formats
176\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000177
178
179\subsection{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000180\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000181
182If you're reading this document, you probably have a good idea of what
183modules, extensions, and so forth are. Nevertheless, just to be sure
184that everyone is operating from a common starting point, we offer the
185following glossary of common Python terms:
186\begin{description}
187\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000188 code imported by some other code. Three types of modules concern us
189 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000190
Greg Ward16aafcd2000-04-09 04:06:44 +0000191\item[pure Python module] a module written in Python and contained in a
192 single \file{.py} file (and possibly associated \file{.pyc} and/or
193 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000194
Greg Ward16aafcd2000-04-09 04:06:44 +0000195\item[extension module] a module written in the low-level language of
Fred Drake2884d6d2003-07-02 12:27:43 +0000196 the Python implementation: C/\Cpp{} for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000197 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000198 file, e.g. a shared object (\file{.so}) file for Python extensions on
199 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000200 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drake2884d6d2003-07-02 12:27:43 +0000201 currently, the Distutils only handles C/\Cpp{} extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000202
Greg Ward16aafcd2000-04-09 04:06:44 +0000203\item[package] a module that contains other modules; typically contained
204 in a directory in the filesystem and distinguished from other
205 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000206
Greg Ward6153fa12000-05-26 02:24:28 +0000207\item[root package] the root of the hierarchy of packages. (This isn't
208 really a package, since it doesn't have an \file{\_\_init\_\_.py}
209 file. But we have to call it something.) The vast majority of the
210 standard library is in the root package, as are many small, standalone
211 third-party modules that don't belong to a larger module collection.
212 Unlike regular packages, modules in the root package can be found in
213 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000214 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000215\end{description}
216
217
218\subsection{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000219\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000220
221The following terms apply more specifically to the domain of
222distributing Python modules using the Distutils:
223\begin{description}
224\item[module distribution] a collection of Python modules distributed
225 together as a single downloadable resource and meant to be installed
226 \emph{en masse}. Examples of some well-known module distributions are
227 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000228 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000229 is already taken in the Python context: a single module distribution
230 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000231
Greg Ward16aafcd2000-04-09 04:06:44 +0000232\item[pure module distribution] a module distribution that contains only
233 pure Python modules and packages. Sometimes referred to as a ``pure
234 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000235
Greg Ward16aafcd2000-04-09 04:06:44 +0000236\item[non-pure module distribution] a module distribution that contains
237 at least one extension module. Sometimes referred to as a ``non-pure
238 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000239
Greg Ward16aafcd2000-04-09 04:06:44 +0000240\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000241 source distribution); the directory where \file{setup.py} exists. Generally
242 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000243\end{description}
244
245
246\section{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000247\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000248
249The setup script is the centre of all activity in building,
250distributing, and installing modules using the Distutils. The main
251purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000252the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000253do the right thing. As we saw in section~\ref{simple-example} above,
254the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000255most information supplied to the Distutils by the module developer is
256supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000257
258Here's a slightly more involved example, which we'll follow for the next
259couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000260although the Distutils are included with Python 1.6 and later, they also
261have an independent existence so that Python 1.5.2 users can use them to
262install other module distributions. The Distutils' own setup script,
263shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000264
265\begin{verbatim}
266#!/usr/bin/env python
267
268from distutils.core import setup
269
Fred Drakea09262e2001-03-01 18:35:43 +0000270setup(name="Distutils",
271 version="1.0",
272 description="Python Distribution Utilities",
273 author="Greg Ward",
274 author_email="gward@python.net",
275 url="http://www.python.org/sigs/distutils-sig/",
276 packages=['distutils', 'distutils.command'],
277 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000278\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000279
Greg Ward16aafcd2000-04-09 04:06:44 +0000280There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000281distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000282metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000283rather than by module. This is important since the Distutils consist of
284a couple of dozen modules split into (so far) two packages; an explicit
285list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000286maintain. For more information on the additional meta-data, see
287section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000288
Greg Ward46b98e32000-04-14 01:53:36 +0000289Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000290script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000291slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000292platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000293current platform before actually using the pathname. This makes your
294setup script portable across operating systems, which of course is one
295of the major goals of the Distutils. In this spirit, all pathnames in
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000296this document are slash-separated. (MacOS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000297mind that the \emph{absence} of a leading slash indicates a relative
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000298path, the opposite of the MacOS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000299
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000300This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000301functions. If you, for example, use standard Python functions such as
302\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000303should be careful to write portable code instead of hardcoding path
304separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000305
Thomas Heller5f52f722001-02-19 17:48:03 +0000306\begin{verbatim}
307 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
308 os.listdir(os.path.join('mydir', 'subdir'))
309\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000310
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000311
Greg Ward2afffd42000-08-06 20:37:24 +0000312\subsection{Listing whole packages}
313\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000314
315The \option{packages} option tells the Distutils to process (build,
316distribute, install, etc.) all pure Python modules found in each package
317mentioned in the \option{packages} list. In order to do this, of
318course, there has to be a correspondence between package names and
319directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000320obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000321\file{distutils} relative to the distribution root. Thus, when you say
322\code{packages = ['foo']} in your setup script, you are promising that
323the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
324be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000325the directory where your setup script lives. If you break this
326promise, the Distutils will issue a warning but still process the broken
327package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000328
329If you use a different convention to lay out your source directory,
330that's no problem: you just have to supply the \option{package\_dir}
331option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000332you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000333``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000334\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
335and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000336
Greg Ward16aafcd2000-04-09 04:06:44 +0000337\begin{verbatim}
338package_dir = {'': 'lib'}
339\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000340
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000341in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000342and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000343directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000344you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000345\file{lib/foo/\_\_init\_\_.py} exists.
346
Greg Ward1ecc2512000-04-19 22:36:24 +0000347Another possible convention is to put the \module{foo} package right in
348\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000349would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000350
Greg Ward16aafcd2000-04-09 04:06:44 +0000351\begin{verbatim}
352package_dir = {'foo': 'lib'}
353\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000354
Greg Ward59d382e2000-05-26 01:04:47 +0000355A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
356dictionary implicitly applies to all packages below \var{package}, so
357the \module{foo.bar} case is automatically handled here. In this
358example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
359to look for \file{lib/\_\_init\_\_.py} and
360\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
361\option{package\_dir} applies recursively, you must explicitly list all
362packages in \option{packages}: the Distutils will \emph{not} recursively
363scan your source tree looking for any directory with an
364\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000365
366
367\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000368\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000369
370For a small module distribution, you might prefer to list all modules
371rather than listing packages---especially the case of a single module
372that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000373simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000374slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000375
Greg Ward16aafcd2000-04-09 04:06:44 +0000376\begin{verbatim}
377py_modules = ['mod1', 'pkg.mod2']
378\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000379
Greg Ward16aafcd2000-04-09 04:06:44 +0000380This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000381other in the \module{pkg} package. Again, the default package/directory
382layout implies that these two modules can be found in \file{mod1.py} and
383\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000384And again, you can override the package/directory correspondence using
385the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000386
387
388\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000389\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000390
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000391% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000392Just as writing Python extension modules is a bit more complicated than
393writing pure Python modules, describing them to the Distutils is a bit
394more complicated. Unlike pure modules, it's not enough just to list
395modules or packages and expect the Distutils to go out and find the
396right files; you have to specify the extension name, source file(s), and
397any compile/link requirements (include directories, libraries to link
398with, etc.).
399
400All of this is done through another keyword argument to
401\function{setup()}, the \option{extensions} option. \option{extensions}
402is just a list of \class{Extension} instances, each of which describes a
403single extension module. Suppose your distribution includes a single
404extension, called \module{foo} and implemented by \file{foo.c}. If no
405additional instructions to the compiler/linker are needed, describing
406this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000407
Greg Ward2afffd42000-08-06 20:37:24 +0000408\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000409uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000410\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000411
Greg Ward2afffd42000-08-06 20:37:24 +0000412The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000413\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000414script for a module distribution that contains only this one extension
415and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000416
Greg Ward2afffd42000-08-06 20:37:24 +0000417\begin{verbatim}
418from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000419setup(name="foo", version="1.0",
420 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000421\end{verbatim}
422
423The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000424machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000425great deal of flexibility in describing Python extensions, which is
426explained in the following sections.
427
428
429\subsubsection{Extension names and packages}
430
431The first argument to the \class{Extension} constructor is always the
432name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000433
Greg Ward2afffd42000-08-06 20:37:24 +0000434\begin{verbatim}
435Extension("foo", ["src/foo1.c", "src/foo2.c"])
436\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000437
Greg Ward2afffd42000-08-06 20:37:24 +0000438describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000439
Greg Ward2afffd42000-08-06 20:37:24 +0000440\begin{verbatim}
441Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
442\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000443
Greg Ward2afffd42000-08-06 20:37:24 +0000444describes the same extension in the \module{pkg} package. The source
445files and resulting object code are identical in both cases; the only
446difference is where in the filesystem (and therefore where in Python's
447namespace hierarchy) the resulting extension lives.
448
449If you have a number of extensions all in the same package (or all under
450the same base package), use the \option{ext\_package} keyword argument
451to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000452
Greg Ward2afffd42000-08-06 20:37:24 +0000453\begin{verbatim}
454setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000455 ext_package="pkg",
456 ext_modules=[Extension("foo", ["foo.c"]),
457 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000458 )
459\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000460
Greg Ward2afffd42000-08-06 20:37:24 +0000461will compile \file{foo.c} to the extension \module{pkg.foo}, and
462\file{bar.c} to \module{pkg.subpkg.bar}.
463
464
465\subsubsection{Extension source files}
466
467The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000468source files. Since the Distutils currently only support C, \Cpp, and
469Objective-C extensions, these are normally C/\Cpp/Objective-C source
470files. (Be sure to use appropriate extensions to distinguish \Cpp\
471source files: \file{.cc} and \file{.cpp} seem to be recognized by both
472\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000473
474However, you can also include SWIG interface (\file{.i}) files in the
475list; the \command{build\_ext} command knows how to deal with SWIG
476extensions: it will run SWIG on the interface file and compile the
Fred Drake2884d6d2003-07-02 12:27:43 +0000477resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000478
479\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000480 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000481 here when the interface firms up.}
482
483On some platforms, you can include non-source files that are processed
484by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000485means Windows message text (\file{.mc}) files and resource definition
Fred Drake2884d6d2003-07-02 12:27:43 +0000486(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000487(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000488
489
490\subsubsection{Preprocessor options}
491
492Three optional arguments to \class{Extension} will help if you need to
493specify include directories to search or preprocessor macros to
494define/undefine: \code{include\_dirs}, \code{define\_macros}, and
495\code{undef\_macros}.
496
497For example, if your extension requires header files in the
498\file{include} directory under your distribution root, use the
499\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000500
Greg Ward2afffd42000-08-06 20:37:24 +0000501\begin{verbatim}
502Extension("foo", ["foo.c"], include_dirs=["include"])
503\end{verbatim}
504
505You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000506extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000507\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000508
Greg Ward2afffd42000-08-06 20:37:24 +0000509\begin{verbatim}
510Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
511\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000512
Greg Ward2afffd42000-08-06 20:37:24 +0000513You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000514distribute your code: it's probably better to write C code like
515\begin{verbatim}
516#include <X11/Xlib.h>
517\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000518
519If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000520you can take advantage of the fact that header files are installed in a
521consistent way by the Distutils \command{install\_header} command. For
522example, the Numerical Python header files are installed (on a standard
523Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
524(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000525installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000526directory---\file{/usr/local/include/python1.5} in this case---is always
527included in the search path when building Python extensions, the best
528approach is to write C code like
529\begin{verbatim}
530#include <Numerical/arrayobject.h>
531\end{verbatim}
532If you must put the \file{Numerical} include directory right into your
533header search path, though, you can find that directory using the
534Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000535
Greg Ward2afffd42000-08-06 20:37:24 +0000536\begin{verbatim}
537from distutils.sysconfig import get_python_inc
538incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
539setup(...,
540 Extension(..., include_dirs=[incdir]))
541\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000542
Greg Ward2afffd42000-08-06 20:37:24 +0000543Even though this is quite portable---it will work on any Python
544installation, regardless of platform---it's probably easier to just
545write your C code in the sensible way.
546
547You can define and undefine pre-processor macros with the
548\code{define\_macros} and \code{undef\_macros} options.
549\code{define\_macros} takes a list of \code{(name, value)} tuples, where
550\code{name} is the name of the macro to define (a string) and
551\code{value} is its value: either a string or \code{None}. (Defining a
552macro \code{FOO} to \code{None} is the equivalent of a bare
553\code{\#define FOO} in your C source: with most compilers, this sets
554\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
555a list of macros to undefine.
556
557For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000558
Greg Ward2afffd42000-08-06 20:37:24 +0000559\begin{verbatim}
560Extension(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000561 define_macros=[('NDEBUG', '1'),
562 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000563 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
564\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000565
Greg Ward2afffd42000-08-06 20:37:24 +0000566is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000567
Greg Ward2afffd42000-08-06 20:37:24 +0000568\begin{verbatim}
569#define NDEBUG 1
570#define HAVE_STRFTIME
571#undef HAVE_FOO
572#undef HAVE_BAR
573\end{verbatim}
574
575
576\subsubsection{Library options}
577
578You can also specify the libraries to link against when building your
579extension, and the directories to search for those libraries. The
580\code{libraries} option is a list of libraries to link against,
581\code{library\_dirs} is a list of directories to search for libraries at
582link-time, and \code{runtime\_library\_dirs} is a list of directories to
583search for shared (dynamically loaded) libraries at run-time.
584
585For example, if you need to link against libraries known to be in the
586standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000587
Greg Ward2afffd42000-08-06 20:37:24 +0000588\begin{verbatim}
589Extension(...,
590 libraries=["gdbm", "readline"])
591\end{verbatim}
592
593If you need to link with libraries in a non-standard location, you'll
594have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000595
Greg Ward2afffd42000-08-06 20:37:24 +0000596\begin{verbatim}
597Extension(...,
598 library_dirs=["/usr/X11R6/lib"],
599 libraries=["X11", "Xt"])
600\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000601
Greg Ward2afffd42000-08-06 20:37:24 +0000602(Again, this sort of non-portable construct should be avoided if you
603intend to distribute your code.)
604
Thomas Heller5f52f722001-02-19 17:48:03 +0000605\XXX{Should mention clib libraries here or somewhere else!}
606
607\subsubsection{Other options}
608
609There are still some other options which can be used to handle special
610cases.
611
612The \option{extra\_objects} option is a list of object files to be passed
613to the linker. These files must not have extensions, as the default
614extension for the compiler is used.
615
616\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000617to specify additional command line options for the respective compiler and
618linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000619
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000620\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000621of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000622is not needed when building compiled extensions: Distutils
623will automatically add \code{initmodule}
624to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000625
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000626\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000627So far we have been dealing with pure and non-pure Python modules,
628which are usually not run by themselves but imported by scripts.
629
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000630Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000631started from the command line. Scripts don't require Distutils to do
632anything very complicated. The only clever feature is that if the
633first line of the script starts with \code{\#!} and contains the word
634``python'', the Distutils will adjust the first line to refer to the
635current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000636
637The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000638in this way. From the PyXML setup script:
639
640\begin{verbatim}
641setup (...
642 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
643 )
644\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000645
646
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000647\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000648
Thomas Heller5f52f722001-02-19 17:48:03 +0000649The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000650files needed by the module distribution: configuration files, message
651catalogs, data files, anything which doesn't fit in the previous
652categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000653
Fred Drake632bda32002-03-08 22:02:06 +0000654\option{data\_files} specifies a sequence of (\var{directory},
655\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000656
Thomas Heller5f52f722001-02-19 17:48:03 +0000657\begin{verbatim}
658setup(...
659 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000660 ('config', ['cfg/data.cfg']),
661 ('/etc/init.d', ['init-script'])]
662 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000663\end{verbatim}
664
665Note that you can specify the directory names where the data files
666will be installed, but you cannot rename the data files themselves.
667
Fred Drake632bda32002-03-08 22:02:06 +0000668Each (\var{directory}, \var{files}) pair in the sequence specifies the
669installation directory and the files to install there. If
670\var{directory} is a relative path, it is interpreted relative to the
671installation prefix (Python's \code{sys.prefix} for pure-Python
672packages, \code{sys.exec_prefix} for packages that contain extension
673modules). Each file name in \var{files} is interpreted relative to
674the \file{setup.py} script at the top of the package source
675distribution. No directory information from \var{files} is used to
676determine the final location of the installed file; only the name of
677the file is used.
678
Thomas Heller5f52f722001-02-19 17:48:03 +0000679You can specify the \option{data\_files} options as a simple sequence
680of files without specifying a target directory, but this is not recommended,
681and the \command{install} command will print a warning in this case.
682To install data files directly in the target directory, an empty
683string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000684
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000685\subsection{Additional meta-data}
686\label{meta-data}
687
688The setup script may include additional meta-data beyond the name and
689version. This information includes:
690
Fred Drakec440af52003-04-25 16:43:28 +0000691\begin{tableiv}{l|l|l|c}{code}%
692 {Meta-Data}{Description}{Value}{Notes}
693 \lineiv{name}{name of the package}
694 {short string}{(1)}
695 \lineiv{version}{version of this release}
696 {short string}{(1)(2)}
697 \lineiv{author}{package author's name}
698 {short string}{(3)}
699 \lineiv{author_email}{email address of the package author}
700 {email address}{(3)}
701 \lineiv{maintainer}{package maintainer's name}
702 {short string}{(3)}
703 \lineiv{maintainer_email}{email address of the package maintainer}
704 {email address}{(3)}
705 \lineiv{url}{home page for the package}
706 {URL}{(1)}
707 \lineiv{description}{short, summary description of the package}
708 {short string}{}
709 \lineiv{long_description}{longer description of the package}
710 {long string}{}
711 \lineiv{download_url}{location where the package may be downloaded}
712 {URL}{(4)}
713 \lineiv{classifiers}{a list of Trove classifiers}
714 {list of strings}{(4)}
715\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000716
717\noindent Notes:
718\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000719\item[(1)] These fields are required.
720\item[(2)] It is recommended that versions take the form
721 \emph{major.minor\optional{.patch\optional{.sub}}}.
722\item[(3)] Either the author or the maintainer must be identified.
723\item[(4)] These fields should not be used if your package is to be
724 compatible with Python versions prior to 2.2.3 or 2.3. The list is
725 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
726
727\item["short string"] A single line of text, not more than 200 characters.
728\item["long string"] Multiple lines of plain text in ReStructuredText
729 format (see \url{http://docutils.sf.net/}).
730\item["list of strings"] See below.
731\end{description}
732
733None of the string values may be Unicode.
734
735Encoding the version information is an art in itself. Python packages
736generally adhere to the version format
737\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7380 for
739initial, experimental releases of software. It is incremented for
740releases that represent major milestones in a package. The minor
741number is incremented when important new features are added to the
742package. The patch number increments when bug-fix releases are
743made. Additional trailing version information is sometimes used to
744indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
745where functionality and API may change), "b1,b2,...,bN" (for beta
746releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
747pre-release release testing). Some examples:
748
749\begin{description}
750\item[0.1.0] the first, experimental release of a package
751\item[1.0.1a2] the second alpha release of the first patch version of 1.0
752\end{description}
753
754\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000755
756\begin{verbatim}
757setup(...
Fred Drakec440af52003-04-25 16:43:28 +0000758 classifiers = [
Fred Drake2a046232003-03-31 16:23:09 +0000759 'Development Status :: 4 - Beta',
760 'Environment :: Console',
761 'Environment :: Web Environment',
762 'Intended Audience :: End Users/Desktop',
763 'Intended Audience :: Developers',
764 'Intended Audience :: System Administrators',
765 'License :: OSI Approved :: Python Software Foundation License',
766 'Operating System :: MacOS :: MacOS X',
767 'Operating System :: Microsoft :: Windows',
768 'Operating System :: POSIX',
769 'Programming Language :: Python',
770 'Topic :: Communications :: Email',
771 'Topic :: Office/Business',
772 'Topic :: Software Development :: Bug Tracking',
773 ],
Fred Drake2a046232003-03-31 16:23:09 +0000774 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000775\end{verbatim}
776
Fred Drakec440af52003-04-25 16:43:28 +0000777If you wish to include classifiers in your \file{setup.py} file and also
778wish to remain backwards-compatible with Python releases prior to 2.2.3,
779then you can include the following code fragment in your \file{setup.py}
780before the \code{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000781
782\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000783# patch distutils if it can't cope with the "classifiers" or
784# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000785if sys.version < '2.2.3':
786 from distutils.dist import DistributionMetadata
787 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000788 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000789\end{verbatim}
790
Greg Ward16aafcd2000-04-09 04:06:44 +0000791
Thomas Heller675580f2003-06-30 19:33:29 +0000792\subsection{Debugging the setup script}
793\label{meta-data}
794
795Sometimes things go wrong, and the setup script doesn't do what the
796developer wants.
797
798Distutils catches any exceptions when running the setup script, and
799print a simple error message before the script is terminated. The
800motivation for this behaviour is to not confuse administrators who
801don't know much about Python and are trying to install a package. If
802they get a big long traceback from deep inside the guts of Distutils,
803they may think the package or the Python installation is broken
804because they don't read all the way down to the bottom and see that
805it's a permission problem.
806
807On the other hand, this doesn't help the developer to find the cause
808of the failure. For this purpose, the DISTUTILS_DEBUG environment
809variable can be set to anything except an empty string, and distutils
810will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000811full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000812
Greg Ward16aafcd2000-04-09 04:06:44 +0000813\section{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000814\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000815
Greg Ward16aafcd2000-04-09 04:06:44 +0000816Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000817distribution \emph{a priori}: you may need to get some information from
818the user, or from the user's system, in order to proceed. As long as
819that information is fairly simple---a list of directories to search for
820C header files or libraries, for example---then providing a
821configuration file, \file{setup.cfg}, for users to edit is a cheap and
822easy way to solicit it. Configuration files also let you provide
823default values for any command option, which the installer can then
824override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000825
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000826% (If you have more advanced needs, such as determining which extensions
827% to build based on what capabilities are present on the target system,
828% then you need the Distutils ``auto-configuration'' facility. This
829% started to appear in Distutils 0.9 but, as of this writing, isn't mature
830% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000831
Greg Ward47f99a62000-09-04 20:07:15 +0000832The setup configuration file is a useful middle-ground between the setup
833script---which, ideally, would be opaque to installers\footnote{This
834 ideal probably won't be achieved until auto-configuration is fully
835 supported by the Distutils.}---and the command-line to the setup
836script, which is outside of your control and entirely up to the
837installer. In fact, \file{setup.cfg} (and any other Distutils
838configuration files present on the target system) are processed after
839the contents of the setup script, but before the command-line. This has
840several useful consequences:
841\begin{itemize}
842\item installers can override some of what you put in \file{setup.py} by
843 editing \file{setup.cfg}
844\item you can provide non-standard defaults for options that are not
845 easily set in \file{setup.py}
846\item installers can override anything in \file{setup.cfg} using the
847 command-line options to \file{setup.py}
848\end{itemize}
849
850The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000851
Greg Ward47f99a62000-09-04 20:07:15 +0000852\begin{verbatim}
853[command]
854option=value
855...
856\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000857
Greg Ward47f99a62000-09-04 20:07:15 +0000858where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000859\command{build\_py}, \command{install}), and \var{option} is one of
860the options that command supports. Any number of options can be
861supplied for each command, and any number of command sections can be
862included in the file. Blank lines are ignored, as are comments, which
863run from a \character{\#} character until the end of the line. Long
864option values can be split across multiple lines simply by indenting
865the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000866
867You can find out the list of options supported by a particular command
868with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000869
Greg Ward47f99a62000-09-04 20:07:15 +0000870\begin{verbatim}
871> python setup.py --help build_ext
872[...]
873Options for 'build_ext' command:
874 --build-lib (-b) directory for compiled extension modules
875 --build-temp (-t) directory for temporary files (build by-products)
876 --inplace (-i) ignore build-lib and put compiled extensions into the
877 source directory alongside your pure Python modules
878 --include-dirs (-I) list of directories to search for header files
879 --define (-D) C preprocessor macros to define
880 --undef (-U) C preprocessor macros to undefine
881[...]
882\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000883
Greg Ward47f99a62000-09-04 20:07:15 +0000884Note that an option spelled \longprogramopt{foo-bar} on the command-line
885is spelled \option{foo\_bar} in configuration files.
886
887For example, say you want your extensions to be built
888``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000889want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000890in the same source directory as your pure Python modules
891\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
892\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000893
Greg Ward47f99a62000-09-04 20:07:15 +0000894\begin{verbatim}
895python setup.py build_ext --inplace
896\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000897
Greg Ward47f99a62000-09-04 20:07:15 +0000898But this requires that you always specify the \command{build\_ext}
899command explicitly, and remember to provide \longprogramopt{inplace}.
900An easier way is to ``set and forget'' this option, by encoding it in
901\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000902
Greg Ward47f99a62000-09-04 20:07:15 +0000903\begin{verbatim}
904[build_ext]
905inplace=1
906\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000907
Greg Ward47f99a62000-09-04 20:07:15 +0000908This will affect all builds of this module distribution, whether or not
909you explcitly specify \command{build\_ext}. If you include
910\file{setup.cfg} in your source distribution, it will also affect
911end-user builds---which is probably a bad idea for this option, since
912always building extensions in-place would break installation of the
913module distribution. In certain peculiar cases, though, modules are
914built right in their installation directory, so this is conceivably a
915useful ability. (Distributing extensions that expect to be built in
916their installation directory is almost always a bad idea, though.)
917
918Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000919change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000920everything required to generate a ``spec'' file for creating an RPM
921distribution. Some of this information comes from the setup script, and
922some is automatically generated by the Distutils (such as the list of
923files installed). But some of it has to be supplied as options to
924\command{bdist\_rpm}, which would be very tedious to do on the
925command-line for every run. Hence, here is a snippet from the
926Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000927
Greg Ward47f99a62000-09-04 20:07:15 +0000928\begin{verbatim}
929[bdist_rpm]
930release = 1
931packager = Greg Ward <gward@python.net>
932doc_files = CHANGES.txt
933 README.txt
934 USAGE.txt
935 doc/
936 examples/
937\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000938
Greg Ward47f99a62000-09-04 20:07:15 +0000939Note that the \option{doc\_files} option is simply a
940whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000941
942
Fred Drakea09262e2001-03-01 18:35:43 +0000943\begin{seealso}
944 \seetitle[../inst/config-syntax.html]{Installing Python
945 Modules}{More information on the configuration files is
946 available in the manual for system administrators.}
947\end{seealso}
948
949
Greg Ward16aafcd2000-04-09 04:06:44 +0000950\section{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000951\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000952
Greg Warde78298a2000-04-28 17:12:24 +0000953As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000954\command{sdist} command to create a source distribution. In the
955simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000956
Greg Ward16aafcd2000-04-09 04:06:44 +0000957\begin{verbatim}
958python setup.py sdist
959\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000960
Greg Ward19c67f82000-06-24 01:33:16 +0000961(assuming you haven't specified any \command{sdist} options in the setup
962script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000963default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000964tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
965\XXX{no MacOS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000966
Greg Wardd5767a52000-04-19 22:48:09 +0000967You can specify as many formats as you like using the
968\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000969
Greg Ward16aafcd2000-04-09 04:06:44 +0000970\begin{verbatim}
971python setup.py sdist --formats=gztar,zip
972\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000973
Greg Ward16aafcd2000-04-09 04:06:44 +0000974to create a gzipped tarball and a zip file. The available formats are:
Greg Ward46b98e32000-04-14 01:53:36 +0000975\begin{tableiii}{l|l|c}{code}%
976 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000977 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
978 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000979 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000980 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000981 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000982\end{tableiii}
983
984\noindent Notes:
985\begin{description}
986\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000987\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000988\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000989 \module{zipfile} module (part of the standard Python library since
990 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000991\item[(4)] requires external utilities: \program{tar} and possibly one
992 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000993\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000994
995
Greg Ward54589d42000-09-06 01:37:35 +0000996
997\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +0000998\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +0000999
Greg Ward54589d42000-09-06 01:37:35 +00001000If you don't supply an explicit list of files (or instructions on how to
1001generate one), the \command{sdist} command puts a minimal default set
1002into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001003\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001004\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001005 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001006\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001007 \option{libraries} options (\XXX{getting C library sources currently
Greg Wardfacb8db2000-04-09 04:32:40 +00001008 broken -- no get\_source\_files() method in build\_clib.py!})
Greg Ward16aafcd2000-04-09 04:06:44 +00001009\item anything that looks like a test script: \file{test/test*.py}
1010 (currently, the Distutils don't do anything with test scripts except
1011 include them in source distributions, but in the future there will be
1012 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001013\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1014 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001015\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001016
Greg Ward16aafcd2000-04-09 04:06:44 +00001017Sometimes this is enough, but usually you will want to specify
1018additional files to distribute. The typical way to do this is to write
1019a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001020manifest template is just a list of instructions for how to generate
1021your manifest file, \file{MANIFEST}, which is the exact list of files to
1022include in your source distribution. The \command{sdist} command
1023processes this template and generates a manifest based on its
1024instructions and what it finds in the filesystem.
1025
1026If you prefer to roll your own manifest file, the format is simple: one
1027filename per line, regular files (or symlinks to them) only. If you do
1028supply your own \file{MANIFEST}, you must specify everything: the
1029default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001030
1031The manifest template has one command per line, where each command
1032specifies a set of files to include or exclude from the source
1033distribution. For an example, again we turn to the Distutils' own
1034manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001035
Greg Ward16aafcd2000-04-09 04:06:44 +00001036\begin{verbatim}
1037include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001038recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001039prune examples/sample?/build
1040\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001041
Greg Ward16aafcd2000-04-09 04:06:44 +00001042The meanings should be fairly clear: include all files in the
1043distribution root matching \code{*.txt}, all files anywhere under the
1044\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001045exclude all directories matching \code{examples/sample?/build}. All of
1046this is done \emph{after} the standard include set, so you can exclude
1047files from the standard set with explicit instructions in the manifest
1048template. (Or, you can use the \longprogramopt{no-defaults} option to
1049disable the standard set entirely.) There are several other commands
1050available in the manifest template mini-language; see
1051section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001052
Greg Ward54589d42000-09-06 01:37:35 +00001053The order of commands in the manifest template matters: initially, we
1054have the list of default files as described above, and each command in
1055the template adds to or removes from that list of files. Once we have
1056fully processed the manifest template, we remove files that should not
1057be included in the source distribution:
1058\begin{itemize}
1059\item all files in the Distutils ``build'' tree (default \file{build/})
1060\item all files in directories named \file{RCS} or \file{CVS}
1061\end{itemize}
1062Now we have our complete list of files, which is written to the manifest
1063for future reference, and then used to build the source distribution
1064archive(s).
1065
1066You can disable the default set of included files with the
1067\longprogramopt{no-defaults} option, and you can disable the standard
1068exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001069
Greg Ward46b98e32000-04-14 01:53:36 +00001070Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001071\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001072Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001073\begin{enumerate}
1074\item include all Python source files in the \file{distutils} and
1075 \file{distutils/command} subdirectories (because packages
1076 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001077 \option{packages} option in the setup script---see
1078 section~\ref{setup-script})
1079\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1080 (standard files)
1081\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001082\item include \file{*.txt} in the distribution root (this will find
1083 \file{README.txt} a second time, but such redundancies are weeded out
1084 later)
Greg Ward54589d42000-09-06 01:37:35 +00001085\item include anything matching \file{*.txt} or \file{*.py} in the
1086 sub-tree under \file{examples},
1087\item exclude all files in the sub-trees starting at directories
1088 matching \file{examples/sample?/build}---this may exclude files
1089 included by the previous two steps, so it's important that the
1090 \code{prune} command in the manifest template comes after the
1091 \code{recursive-include} command
1092\item exclude the entire \file{build} tree, and any \file{RCS} or
1093 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001094\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001095Just like in the setup script, file and directory names in the manifest
1096template should always be slash-separated; the Distutils will take care
1097of converting them to the standard representation on your platform.
1098That way, the manifest template is portable across operating systems.
1099
Greg Ward16aafcd2000-04-09 04:06:44 +00001100
1101\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001102\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001103
1104The normal course of operations for the \command{sdist} command is as
1105follows:
1106\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001107\item if the manifest file, \file{MANIFEST} doesn't exist, read
1108 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001109\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001110 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001111\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1112 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1113 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001114\item use the list of files now in \file{MANIFEST} (either just
1115 generated or read in) to create the source distribution archive(s)
1116\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001117There are a couple of options that modify this behaviour. First, use
1118the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001119disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001120
Greg Ward54589d42000-09-06 01:37:35 +00001121Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001122example, if you have added or removed files or directories that match an
1123existing pattern in the manifest template, you should regenerate the
1124manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001125
Greg Ward16aafcd2000-04-09 04:06:44 +00001126\begin{verbatim}
1127python setup.py sdist --force-manifest
1128\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001129
1130Or, you might just want to (re)generate the manifest, but not create a
1131source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001132
Greg Ward16aafcd2000-04-09 04:06:44 +00001133\begin{verbatim}
1134python setup.py sdist --manifest-only
1135\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001136
Greg Ward54589d42000-09-06 01:37:35 +00001137\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1138\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1139\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001140
1141
1142\section{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001143\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001144
Greg Ward46b98e32000-04-14 01:53:36 +00001145A ``built distribution'' is what you're probably used to thinking of
1146either as a ``binary package'' or an ``installer'' (depending on your
1147background). It's not necessarily binary, though, because it might
1148contain only Python source code and/or byte-code; and we don't call it a
1149package, because that word is already spoken for in Python. (And
1150``installer'' is a term specific to the Windows world. \XXX{do Mac
1151 people use it?})
Greg Ward16aafcd2000-04-09 04:06:44 +00001152
Greg Ward46b98e32000-04-14 01:53:36 +00001153A built distribution is how you make life as easy as possible for
1154installers of your module distribution: for users of RPM-based Linux
1155systems, it's a binary RPM; for Windows users, it's an executable
1156installer; for Debian-based Linux users, it's a Debian package; and so
1157forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001158distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001159designed to enable module developers to concentrate on their
1160specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001161intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001162distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001163are packagers.
1164
1165Of course, the module developer could be his own packager; or the
1166packager could be a volunteer ``out there'' somewhere who has access to
1167a platform which the original developer does not; or it could be
1168software periodically grabbing new source distributions and turning them
1169into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001170access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001171setup script and the \command{bdist} command family to generate built
1172distributions.
1173
1174As a simple example, if I run the following command in the Distutils
1175source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001176
Greg Ward46b98e32000-04-14 01:53:36 +00001177\begin{verbatim}
1178python setup.py bdist
1179\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001180
Greg Ward46b98e32000-04-14 01:53:36 +00001181then the Distutils builds my module distribution (the Distutils itself
1182in this case), does a ``fake'' installation (also in the \file{build}
1183directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001184platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001185file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001186file is considered ``dumb'' because it has to be unpacked in a specific
1187location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001188
Fred Drakeeff9a872000-10-26 16:41:03 +00001189Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001190\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001191from the right place installs the Distutils just as though you had
1192downloaded the source distribution and run \code{python setup.py
1193 install}. (The ``right place'' is either the root of the filesystem or
1194Python's \filevar{prefix} directory, depending on the options given to
1195the \command{bdist\_dumb} command; the default is to make dumb
1196distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001197
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001198Obviously, for pure Python distributions, this isn't any simpler than
1199just running \code{python setup.py install}---but for non-pure
1200distributions, which include extensions that would need to be
1201compiled, it can mean the difference between someone being able to use
1202your extensions or not. And creating ``smart'' built distributions,
1203such as an RPM package or an executable installer for Windows, is far
1204more convenient for users even if your distribution doesn't include
1205any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001206
Greg Wardb6528972000-09-07 02:40:37 +00001207The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001208similar to the \command{sdist} command, which you can use to select the
1209types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001210
Greg Ward46b98e32000-04-14 01:53:36 +00001211\begin{verbatim}
1212python setup.py bdist --format=zip
1213\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001214
Fred Drakeeff9a872000-10-26 16:41:03 +00001215would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001216\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001217unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001218
1219The available formats for built distributions are:
1220\begin{tableiii}{l|l|c}{code}%
1221 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001222 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1223 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1224 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1225 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1226 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001227 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1228 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1229 \lineiii{rpm}{RPM}{(5)}
1230% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001231 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001232\end{tableiii}
1233
1234\noindent Notes:
1235\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001236\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001237\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001238\item[(3)] requires external utilities: \program{tar} and possibly one
1239 of \program{gzip}, \program{bzip2}, or \program{compress}
1240\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001241 \module{zipfile} module (part of the standard Python library since
1242 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001243\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1244 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001245\end{description}
1246
1247You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001248\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001249directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001250\command{bdist} ``sub-commands'' actually generate several similar
1251formats; for instance, the \command{bdist\_dumb} command generates all
1252the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1253\code{zip}), and \command{bdist\_rpm} generates both binary and source
1254RPMs. The \command{bdist} sub-commands, and the formats generated by
1255each, are:
1256\begin{tableii}{l|l}{command}%
1257 {Command}{Formats}
1258 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1259 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001260 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001261\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001262
Greg Wardb6528972000-09-07 02:40:37 +00001263The following sections give details on the individual \command{bdist\_*}
1264commands.
1265
1266
1267\subsection{Creating dumb built distributions}
1268\label{creating-dumb}
1269
1270\XXX{Need to document absolute vs. prefix-relative packages here, but
1271 first I have to implement it!}
1272
1273
1274\subsection{Creating RPM packages}
1275\label{creating-rpms}
1276
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001277The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001278Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1279RPM-based Linux distributions) is your usual environment, creating RPM
1280packages for other users of that same distribution is trivial.
1281Depending on the complexity of your module distribution and differences
1282between Linux distributions, you may also be able to create RPMs that
1283work on different RPM-based distributions.
1284
1285The usual way to create an RPM of your module distribution is to run the
1286\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001287
Greg Wardb6528972000-09-07 02:40:37 +00001288\begin{verbatim}
1289python setup.py bdist_rpm
1290\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001291
Greg Wardb6528972000-09-07 02:40:37 +00001292or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001293
Greg Wardb6528972000-09-07 02:40:37 +00001294\begin{verbatim}
1295python setup.py bdist --formats=rpm
1296\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001297
Greg Wardb6528972000-09-07 02:40:37 +00001298The former allows you to specify RPM-specific options; the latter allows
1299you to easily specify multiple formats in one run. If you need to do
1300both, you can explicitly specify multiple \command{bdist\_*} commands
1301and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001302
Greg Wardb6528972000-09-07 02:40:37 +00001303\begin{verbatim}
1304python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1305 bdist_wininst --target_version="2.0"
1306\end{verbatim}
1307
1308Creating RPM packages is driven by a \file{.spec} file, much as using
1309the Distutils is driven by the setup script. To make your life easier,
1310the \command{bdist\_rpm} command normally creates a \file{.spec} file
1311based on the information you supply in the setup script, on the command
1312line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001313sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001314script as follows:
1315\begin{tableii}{l|l}{textrm}%
1316 {RPM \file{.spec} file option or section}{Distutils setup script option}
1317 \lineii{Name}{\option{name}}
1318 \lineii{Summary (in preamble)}{\option{description}}
1319 \lineii{Version}{\option{version}}
1320 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1321 \option{maintainer} and \option{maintainer\_email}}
1322 \lineii{Copyright}{\option{licence}}
1323 \lineii{Url}{\option{url}}
1324 \lineii{\%description (section)}{\option{long\_description}}
1325\end{tableii}
1326
1327Additionally, there many options in \file{.spec} files that don't have
1328corresponding options in the setup script. Most of these are handled
1329through options to the \command{bdist\_rpm} command as follows:
1330\begin{tableiii}{l|l|l}{textrm}%
1331 {RPM \file{.spec} file option or section}%
1332 {\command{bdist\_rpm} option}%
1333 {default value}
1334 \lineiii{Release}{\option{release}}{``1''}
1335 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1336 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001337 \lineiii{Packager}{\option{packager}}{(none)}
1338 \lineiii{Provides}{\option{provides}}{(none)}
1339 \lineiii{Requires}{\option{requires}}{(none)}
1340 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1341 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001342 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1343 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1344 \lineiii{Icon}{\option{icon}}{(none)}
1345\end{tableiii}
1346Obviously, supplying even a few of these options on the command-line
1347would be tedious and error-prone, so it's usually best to put them in
1348the setup configuration file, \file{setup.cfg}---see
1349section~\ref{setup-config}. If you distribute or package many Python
1350module distributions, you might want to put options that apply to all of
1351them in your personal Distutils configuration file
1352(\file{\textasciitilde/.pydistutils.cfg}).
1353
1354There are three steps to building a binary RPM package, all of which are
1355handled automatically by the Distutils:
1356\begin{enumerate}
1357\item create a \file{.spec} file, which describes the package (analogous
1358 to the Distutils setup script; in fact, much of the information in the
1359 setup script winds up in the \file{.spec} file)
1360\item create the source RPM
1361\item create the ``binary'' RPM (which may or may not contain binary
1362 code, depending on whether your module distribution contains Python
1363 extensions)
1364\end{enumerate}
1365Normally, RPM bundles the last two steps together; when you use the
1366Distutils, all three steps are typically bundled together.
1367
1368If you wish, you can separate these three steps. You can use the
1369\longprogramopt{spec-only} option to make \command{bdist\_rpm} just
1370create the \file{.spec} file and exit; in this case, the \file{.spec}
1371file will be written to the ``distribution directory''---normally
1372\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1373option. (Normally, the \file{.spec} file winds up deep in the ``build
1374tree,'' in a temporary directory created by \command{bdist\_rpm}.)
1375
1376\XXX{this isn't implemented yet---is it needed?!}
1377You can also specify a custom \file{.spec} file with the
Thomas Heller5f52f722001-02-19 17:48:03 +00001378\longprogramopt{spec-file} option; used in conjunction with
Greg Wardb6528972000-09-07 02:40:37 +00001379\longprogramopt{spec-only}, this gives you an opportunity to customize
1380the \file{.spec} file manually:
Fred Drakea09262e2001-03-01 18:35:43 +00001381
Greg Wardb6528972000-09-07 02:40:37 +00001382\begin{verbatim}
1383> python setup.py bdist_rpm --spec-only
1384# ...edit dist/FooBar-1.0.spec
1385> python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1386\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001387
Greg Wardb6528972000-09-07 02:40:37 +00001388(Although a better way to do this is probably to override the standard
1389\command{bdist\_rpm} command with one that writes whatever else you want
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +00001390to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001391
1392
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001393\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001394\label{creating-wininst}
1395
Thomas Hellere61f3652002-11-15 20:13:26 +00001396Executable installers are the natural format for binary distributions
1397on Windows. They display a nice graphical user interface, display
1398some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001399from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001400options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001401
Thomas Hellere61f3652002-11-15 20:13:26 +00001402Since the metadata is taken from the setup script, creating Windows
1403installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001404
Thomas Heller5f52f722001-02-19 17:48:03 +00001405\begin{verbatim}
1406python setup.py bdist_wininst
1407\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001408
Thomas Heller36343f62002-11-15 19:20:56 +00001409or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001410
Thomas Heller5f52f722001-02-19 17:48:03 +00001411\begin{verbatim}
1412python setup.py bdist --formats=wininst
1413\end{verbatim}
1414
Thomas Hellere61f3652002-11-15 20:13:26 +00001415If you have a pure module distribution (only containing pure Python
1416modules and packages), the resulting installer will be version
1417independent and have a name like \file{foo-1.0.win32.exe}. These
1418installers can even be created on \UNIX{} or MacOS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001419
1420If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001421created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001422The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001423\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001424for every Python version you want to support.
1425
1426The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001427installation on the target system in normal and optimizing mode. If
1428you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001429\command{bdist_wininst} command with the
1430\longprogramopt{no-target-compile} and/or the
1431\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001432
Fred Drake0e9bfa32002-11-15 20:34:52 +00001433By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001434when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001435a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001436
1437The installer will also display a large title on the desktop
1438background window when it is run, which is constructed from the name
1439of your distribution and the version number. This can be changed to
1440another text by using the \longprogramopt{title} option.
1441
1442The installer file will be written to the ``distribution directory''
1443--- normally \file{dist/}, but customizable with the
1444\longprogramopt{dist-dir} option.
1445
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001446\subsubsection{The Postinstallation script}
1447\label{postinstallation-script}
1448
1449Starting with Python 2.3, a postinstallation script can be specified
1450which the \longprogramopt{install-script} option. The basename of the
1451script must be specified, and the script filename must also be listed
1452in the scripts argument to the setup function.
1453
1454This script will be run at installation time on the target system
1455after all the files have been copied, with argv[1] set to '-install',
1456and again at uninstallation time before the files are removed with argv[1]
1457set to '-remove'.
1458
1459The installation script runs embedded in the windows installer, every
1460output (sys.stdout, sys.stderr) is redirected into a buffer and will
1461be displayed in the GUI after the script has finished.
1462
1463Some functions especially useful in this context are available in the
1464installation script.
1465
1466\begin{verbatim}
Thomas Heller41e28092003-10-16 19:40:48 +00001467directory_created(pathname)
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001468file_created(pathname)
1469\end{verbatim}
1470
1471These functions should be called when a directory or file is created
1472by the postinstall script at installation time. It will register the
1473pathname with the uninstaller, so that it will be removed when the
1474distribution is uninstalled. To be safe, directories are only removed
1475if they are empty.
1476
1477\begin{verbatim}
1478get_special_folder_path(csidl_string)
1479\end{verbatim}
1480
1481This function can be used to retrieve special folder locations on
1482Windows like the Start Menu or the Desktop. It returns the full path
Thomas Hellera425dbc2003-09-17 17:11:01 +00001483to the folder. 'csidl_string' must be one of the following strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001484
1485\begin{verbatim}
1486"CSIDL_APPDATA"
1487
1488"CSIDL_COMMON_STARTMENU"
1489"CSIDL_STARTMENU"
1490
1491"CSIDL_COMMON_DESKTOPDIRECTORY"
1492"CSIDL_DESKTOPDIRECTORY"
1493
1494"CSIDL_COMMON_STARTUP"
1495"CSIDL_STARTUP"
1496
1497"CSIDL_COMMON_PROGRAMS"
1498"CSIDL_PROGRAMS"
1499
1500"CSIDL_FONTS"
1501\end{verbatim}
1502
1503If the folder cannot be retrieved, OSError is raised.
1504
1505Which folders are available depends on the exact Windows version, and probably
1506also the configuration. For details refer to Microsoft's documentation of the
Thomas Heller63b4dd32002-12-12 19:35:00 +00001507\code{SHGetSpecialFolderPath} function.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001508
1509\begin{verbatim}
1510create_shortcut(target, description, filename[, arguments[,
1511 workdir[, iconpath[, iconindex]]]])
1512\end{verbatim}
1513
1514This function creates a shortcut.
Thomas Heller63b4dd32002-12-12 19:35:00 +00001515\var{target} is the path to the program to be started by the shortcut.
1516\var{description} is the description of the sortcut.
1517\var{filename} is the title of the shortcut that the user will see.
1518\var{arguments} specifies the command line arguments, if any.
1519\var{workdir} is the working directory for the program.
1520\var{iconpath} is the file containing the icon for the shortcut,
1521and \var{iconindex} is the index of the icon in the file
1522\var{iconpath}. Again, for details consult the Microsoft
1523documentation for the \code{IShellLink} interface.
Greg Wardb6528972000-09-07 02:40:37 +00001524
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001525\section{Registering with the Package Index}
1526\label{package-index}
1527
1528The Python Package Index (PyPI) holds meta-data describing distributions
1529packaged with distutils. The distutils command \command{register} is
1530used to submit your distribution's meta-data to the index. It is invoked
1531as follows:
1532
1533\begin{verbatim}
1534python setup.py register
1535\end{verbatim}
1536
1537Distutils will respond with the following prompt:
1538
1539\begin{verbatim}
1540running register
1541We need to know who you are, so please choose either:
1542 1. use your existing login,
1543 2. register as a new user,
1544 3. have the server generate a new password for you (and email it to you), or
1545 4. quit
1546Your selection [default 1]:
1547\end{verbatim}
1548
1549\noindent Note: if your username and password are saved locally, you will
1550not see this menu.
1551
1552If you have not registered with PyPI, then you will need to do so now. You
1553should choose option 2, and enter your details as required. Soon after
1554submitting your details, you will receive an email which will be used to
1555confirm your registration.
1556
1557Once you are registered, you may choose option 1 from the menu. You will
1558be prompted for your PyPI username and password, and \command{register}
1559will then submit your meta-data to the index.
1560
1561You may submit any number of versions of your distribution to the index. If
1562you alter the meta-data for a particular version, you may submit it again
1563and the index will be updated.
1564
1565PyPI holds a record for each (name, version) combination submitted. The
1566first user to submit information for a given name is designated the Owner
1567of that name. They may submit changes through the \command{register}
1568command or through the web interface. They may also designate other users
1569as Owners or Maintainers. Maintainers may edit the package information, but
1570not designate other Owners or Maintainers.
1571
1572By default PyPI will list all versions of a given package. To hide certain
1573versions, the Hidden property should be set to yes. This must be edited
1574through the web interface.
1575
1576
1577
Greg Ward007c04a2002-05-10 14:45:59 +00001578\section{Examples}
1579\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001580
Greg Ward007c04a2002-05-10 14:45:59 +00001581\subsection{Pure Python distribution (by module)}
1582\label{pure-mod}
1583
1584If you're just distributing a couple of modules, especially if they
1585don't live in a particular package, you can specify them individually
1586using the \option{py\_modules} option in the setup script.
1587
1588In the simplest case, you'll have two files to worry about: a setup
1589script and the single module you're distributing, \file{foo.py} in this
1590example:
1591\begin{verbatim}
1592<root>/
1593 setup.py
1594 foo.py
1595\end{verbatim}
1596(In all diagrams in this section, \verb|<root>| will refer to the
1597distribution root directory.) A minimal setup script to describe this
1598situation would be:
1599\begin{verbatim}
1600from distutils.core import setup
1601setup(name = "foo", version = "1.0",
1602 py_modules = ["foo"])
1603\end{verbatim}
1604Note that the name of the distribution is specified independently with
1605the \option{name} option, and there's no rule that says it has to be the
1606same as the name of the sole module in the distribution (although that's
1607probably a good convention to follow). However, the distribution name
1608is used to generate filenames, so you should stick to letters, digits,
1609underscores, and hyphens.
1610
1611Since \option{py\_modules} is a list, you can of course specify multiple
1612modules, eg. if you're distributing modules \module{foo} and
1613\module{bar}, your setup might look like this:
1614\begin{verbatim}
1615<root>/
1616 setup.py
1617 foo.py
1618 bar.py
1619\end{verbatim}
1620and the setup script might be
1621\begin{verbatim}
1622from distutils.core import setup
1623setup(name = "foobar", version = "1.0",
1624 py_modules = ["foo", "bar"])
1625\end{verbatim}
1626
1627You can put module source files into another directory, but if you have
1628enough modules to do that, it's probably easier to specify modules by
1629package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001630
1631
Greg Ward007c04a2002-05-10 14:45:59 +00001632\subsection{Pure Python distribution (by package)}
1633\label{pure-pkg}
1634
1635If you have more than a couple of modules to distribute, especially if
1636they are in multiple packages, it's probably easier to specify whole
1637packages rather than individual modules. This works even if your
1638modules are not in a package; you can just tell the Distutils to process
1639modules from the root package, and that works the same as any other
1640package (except that you don't have to have an \file{\_\_init\_\_.py}
1641file).
1642
1643The setup script from the last example could also be written as
1644\begin{verbatim}
1645from distutils.core import setup
1646setup(name = "foobar", version = "1.0",
1647 packages = [""])
1648\end{verbatim}
1649(The empty string stands for the root package.)
1650
1651If those two files are moved into a subdirectory, but remain in the root
1652package, e.g.:
1653\begin{verbatim}
1654<root>/
1655 setup.py
1656 src/ foo.py
1657 bar.py
1658\end{verbatim}
1659then you would still specify the root package, but you have to tell the
1660Distutils where source files in the root package live:
1661\begin{verbatim}
1662from distutils.core import setup
1663setup(name = "foobar", version = "1.0",
1664 package_dir = {"": "src"},
1665 packages = [""])
1666\end{verbatim}
1667
1668More typically, though, you will want to distribute multiple modules in
1669the same package (or in sub-packages). For example, if the \module{foo}
1670and \module{bar} modules belong in package \module{foobar}, one way to
1671layout your source tree is
1672\begin{verbatim}
1673<root>/
1674 setup.py
1675 foobar/
1676 __init__.py
1677 foo.py
1678 bar.py
1679\end{verbatim}
1680This is in fact the default layout expected by the Distutils, and the
1681one that requires the least work to describe in your setup script:
1682\begin{verbatim}
1683from distutils.core import setup
1684setup(name = "foobar", version = "1.0",
1685 packages = ["foobar"])
1686\end{verbatim}
1687
1688If you want to put modules in directories not named for their package,
1689then you need to use the \option{package\_dir} option again. For
1690example, if the \file{src} directory holds modules in the
1691\module{foobar} package:
1692\begin{verbatim}
1693<root>/
1694 setup.py
1695 src/
1696 __init__.py
1697 foo.py
1698 bar.py
1699\end{verbatim}
1700an appropriate setup script would be
1701\begin{verbatim}
1702from distutils.core import setup
1703setup(name = "foobar", version = "1.0",
1704 package_dir = {"foobar" : "src"},
1705 packages = ["foobar"])
1706\end{verbatim}
1707
1708Or, you might put modules from your main package right in the
1709distribution root:
1710\begin{verbatim}
1711<root>/
1712 setup.py
1713 __init__.py
1714 foo.py
1715 bar.py
1716\end{verbatim}
1717in which case your setup script would be
1718\begin{verbatim}
1719from distutils.core import setup
1720setup(name = "foobar", version = "1.0",
1721 package_dir = {"foobar" : ""},
1722 packages = ["foobar"])
1723\end{verbatim}
1724(The empty string also stands for the current directory.)
1725
1726If you have sub-packages, they must be explicitly listed in
1727\option{packages}, but any entries in \option{package\_dir}
1728automatically extend to sub-packages. (In other words, the Distutils
1729does \emph{not} scan your source tree, trying to figure out which
1730directories correspond to Python packages by looking for
1731\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1732sub-package:
1733\begin{verbatim}
1734<root>/
1735 setup.py
1736 foobar/
1737 __init__.py
1738 foo.py
1739 bar.py
1740 subfoo/
1741 __init__.py
1742 blah.py
1743\end{verbatim}
1744then the corresponding setup script would be
1745\begin{verbatim}
1746from distutils.core import setup
1747setup(name = "foobar", version = "1.0",
1748 packages = ["foobar", "foobar.subfoo"])
1749\end{verbatim}
1750(Again, the empty string in \option{package\_dir} stands for the current
1751directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001752
1753
Greg Ward007c04a2002-05-10 14:45:59 +00001754\subsection{Single extension module}
1755\label{single-ext}
1756
1757Extension modules are specified using the \option{ext\_modules} option.
1758\option{package\_dir} has no effect on where extension source files are
1759found; it only affects the source for pure Python modules. The simplest
1760case, a single extension module in a single C source file, is:
1761\begin{verbatim}
1762<root>/
1763 setup.py
1764 foo.c
1765\end{verbatim}
1766If the \module{foo} extension belongs in the root package, the setup
1767script for this could be
1768\begin{verbatim}
1769from distutils.core import setup
1770setup(name = "foobar", version = "1.0",
1771 ext_modules = [Extension("foo", ["foo.c"])])
1772\end{verbatim}
1773
1774If the extension actually belongs in a package, say \module{foopkg},
1775then
1776
1777With exactly the same source tree layout, this extension can be put in
1778the \module{foopkg} package simply by changing the name of the
1779extension:
1780\begin{verbatim}
1781from distutils.core import setup
1782setup(name = "foobar", version = "1.0",
1783 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1784\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001785
1786
Fred Drakea09262e2001-03-01 18:35:43 +00001787%\subsection{Multiple extension modules}
1788%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001789
1790
Fred Drakea09262e2001-03-01 18:35:43 +00001791%\subsection{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001792
1793
Fred Drakea09262e2001-03-01 18:35:43 +00001794%\section{Extending the Distutils}
1795%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001796
1797
Fred Drakea09262e2001-03-01 18:35:43 +00001798%\subsection{Extending existing commands}
1799%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001800
1801
Fred Drakea09262e2001-03-01 18:35:43 +00001802%\subsection{Writing new commands}
1803%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001804
Fred Drakea09262e2001-03-01 18:35:43 +00001805%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001806
Greg Ward4a9e7222000-04-25 02:57:36 +00001807
1808
Greg Ward16aafcd2000-04-09 04:06:44 +00001809\section{Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001810\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001811
1812
Fred Drakea09262e2001-03-01 18:35:43 +00001813%\subsection{Building modules: the \protect\command{build} command family}
1814%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001815
Fred Drakea09262e2001-03-01 18:35:43 +00001816%\subsubsection{\protect\command{build}}
1817%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001818
Fred Drakea09262e2001-03-01 18:35:43 +00001819%\subsubsection{\protect\command{build\_py}}
1820%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001821
Fred Drakea09262e2001-03-01 18:35:43 +00001822%\subsubsection{\protect\command{build\_ext}}
1823%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001824
Fred Drakea09262e2001-03-01 18:35:43 +00001825%\subsubsection{\protect\command{build\_clib}}
1826%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001827
1828
Greg Wardfacb8db2000-04-09 04:32:40 +00001829\subsection{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001830\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001831
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001832The install command ensures that the build commands have been run and then
1833runs the subcommands \command{install\_lib},
1834\command{install\_data} and
1835\command{install\_scripts}.
1836
Fred Drakea09262e2001-03-01 18:35:43 +00001837%\subsubsection{\protect\command{install\_lib}}
1838%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001839
1840\subsubsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001841\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001842This command installs all data files provided with the distribution.
1843
1844\subsubsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001845\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001846This command installs all (Python) scripts in the distribution.
1847
Greg Ward16aafcd2000-04-09 04:06:44 +00001848
Fred Drakea09262e2001-03-01 18:35:43 +00001849%\subsection{Cleaning up: the \protect\command{clean} command}
1850%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001851
1852
Fred Drakeeff9a872000-10-26 16:41:03 +00001853\subsection{Creating a source distribution: the
1854 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001855\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001856
1857
1858\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001859
Greg Ward16aafcd2000-04-09 04:06:44 +00001860The manifest template commands are:
1861\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001862 \lineii{include \var{pat1} \var{pat2} ... }
1863 {include all files matching any of the listed patterns}
1864 \lineii{exclude \var{pat1} \var{pat2} ... }
1865 {exclude all files matching any of the listed patterns}
1866 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1867 {include all files under \var{dir} matching any of the listed patterns}
1868 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1869 {exclude all files under \var{dir} matching any of the listed patterns}
1870 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001871 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001872 any of the listed patterns}
1873 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001874 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001875 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001876 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1877 \lineii{graft \var{dir}}{include all files under \var{dir}}
1878\end{tableii}
Fred Drakeeff9a872000-10-26 16:41:03 +00001879The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001880sequence of regular filename characters, \code{?} matches any single
1881regular filename character, and \code{[\var{range}]} matches any of the
1882characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001883\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001884platform-specific: on \UNIX{} it is anything except slash; on Windows
1885anything except backslash or colon; on MacOS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001886
Fred Drakeeff9a872000-10-26 16:41:03 +00001887\XXX{Windows and MacOS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001888
1889
Fred Drakea09262e2001-03-01 18:35:43 +00001890%\subsection{Creating a built distribution: the
1891% \protect\command{bdist} command family}
1892%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001893
1894
Fred Drakeab70b382001-08-02 15:13:15 +00001895%\subsubsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001896
Fred Drakeab70b382001-08-02 15:13:15 +00001897%\subsubsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001898
Fred Drakeab70b382001-08-02 15:13:15 +00001899%\subsubsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001900
Fred Drakeab70b382001-08-02 15:13:15 +00001901%\subsubsection{\protect\command{bdist\_wininst}}
1902
1903
1904\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00001905
1906
Greg Wardabc52162000-02-26 00:52:48 +00001907\end{document}