blob: 913899fe521533789f7d146b55beb2286e69a6e1 [file] [log] [blame]
Greg Wardabc52162000-02-26 00:52:48 +00001\documentclass{howto}
2\usepackage{ltxmarkup}
Greg Ward16aafcd2000-04-09 04:06:44 +00003\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00004
Greg Ward16aafcd2000-04-09 04:06:44 +00005\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +00006
Greg Wardabc52162000-02-26 00:52:48 +00007\author{Greg Ward}
8\authoraddress{E-mail: \email{gward@python.net}}
9
Greg Ward16aafcd2000-04-09 04:06:44 +000010
11\newcommand{\XXX}[1]{\textbf{**#1**}}
12
13
Greg Wardabc52162000-02-26 00:52:48 +000014\begin{document}
15
Greg Ward16aafcd2000-04-09 04:06:44 +000016
17\section{Introduction}
18\label{sec:intro}
19
20In the past, Python module developers have not had much infrastructure
21support for distributing modules, nor have Python users had much support
22for installing and maintaining third-party modules. With the
23introduction of the Python Distribution Utilities (Distutils for short)
24in Python 1.6, this situation should start to improve.
25
26This document only covers using the Distutils to distribute your Python
27modules. Using the Distutils does not tie you to Python 1.6, though:
28the Distutils work just fine with Python 1.5, and it is reasonable (and
29expected to become commonplace) to expect users of Python 1.5 to
30download and install the Distutils separately before they can install
31your modules. Python 1.6 users, of course, won't have to add anything
32to their Python installation in order to use the Distutils to install
33third-party modules.
34
35This document concentrates on the role of developer/distributor: if
36you're looking for information on installing Python modules, you should
37refer to the ``Installing Python Modules'' manual.
38
39
40\section{Concepts & Terminology}
41\label{sec:concepts}
42
43Using the Distutils is quite simple, both for module developers and for
44users/administrators installing third-party modules. As a developer,
45your responsibilites (apart from writing solid, well-documented and
46well-tested code, of course!) are:
47\begin{itemize}
48\item write a setup script (\file{setup.py} by convention)
49\item (optional) write a setup configuration file
50\item create a source distribution
51\item (optional) create one or more built (binary) distributions
52\end{itemize}
53Each of these tasks is covered in this document.
54
55Not all module developers have access to a multitude of platforms, so
56it's not always feasible to expect them to create a multitude of built
57distributions. It is hoped that a class of intermediaries, called
58\emph{packagers}, will arise to take address this need. Packagers will
59take source distributions released by module developers, build them on
60one or more platforms, and release the resulting built distributions.
61Thus, users on the most popular platforms will be able to install most
62popular Python module distributions in the most natural way for their
63platform, without having to run a single setup script or compile a line
64of code.
65
66
67\subsection{A simple example}
68\label{sec:simple-example}
69
70The setup script is usually quite simple, although since it's written in
71Python, there are no arbitrary limits to what you can do. If all you
72want to do is distribute a module called \module{foo}, contained in a
73file \file{foo.py}, then you can get away with as little as this:
74\begin{verbatim}
75from distutils.core import setup
76setup (name = "foo",
77 version = "1.0",
78 py_modules = ["foo"])
79\end{verbatim}
80Some observations:
81\begin{itemize}
82\item all information that you supply to the Distutils is supplied as
83 keyword arguments to the \func{setup()} function
84\item those keyword arguments fall into two categories: package
85 meta-data (name, version number) and information about what's in the
86 package (list of pure modules, in this case)
87\item modules are specified by module name, not filename (the same will
88 hold true for packages and extensions)
89\item it's recommended that you supply a little more meta-data, in
90 particular your name, email address and a URL for the project
91\end{itemize}
92
93To create a source distribution for this module, you would run
94\begin{verbatim}
95python setup.py sdist
96\end{verbatim}
97which will create an archive file (e.g., tarball on Unix, zip file on
98Windows) containing your setup script, \file{setup.py}, and your module,
99\file{foo.py}. The archive file will be named \file{Foo-1.0.tar.gz} (or
100\file{.zip}), and will unpack into a directory \file{Foo-1.0}.
101
102If an end-user wishes to install your \module{foo} module, all she has
103to do is download \file{Foo-1.0.tar.gz}) (or \file{.zip}), unpack it,
104and---from the \file{Foo-1.0} directory---run
105\begin{verbatim}
106python setup.py install
107\end{verbatim}
108which will ultimately copy \file{foo.py} to the appropriate directory
109for third-party modules in their Python installation.
110
111This simple example demonstrates some fundamental concepts of the
112Distutils: first, both developers and installers have the same basic
113user interface, i.e. the setup script. The difference is which
114Distutils \emph{commands} they use: the \command{sdist} command is
115almost exclusively for module developers, while \command{install} is
116more often for installers (although most developers will want to install
117their own code occasionally).
118
119\XXX{only partially implemented}%
120If you want to make things really easy for your users, you can create
121one or more built distributions for them. For instance, if you are
122running on a Windows machine, and want to make things easy for other
123Windows users, you can create an executable installer (the most
124appropriate type of built distribution for this platform) with the
125\command{bdist\_wise} command. (Wise is the installation tool used to
126create Windows installers for Python itself, so we have adopted it for
127use by any Python module distribution. You'll need to have version XXX
128of Wise installed on your system for the \command{bdist\_wise} to work;
129it's available from \url{http://foo/bar/baz}. For example:
130\begin{verbatim}
131python setup.py bdist_wise
132\end{verbatim}
133will create an executable installer, \file{Foo-1_0.exe}, in the current
134directory.
135
136\XXX{not implemented yet}
137Other \command{bdist\_*} commands exist for RPM-based Linux systems
138(\command{bdist\_rpm}), Debian-based Linux systems
139(\command{bdist\_deb}), ...
140
141
142\subsection{General Python terminology}
143\label{sec:python-terms}
144
145If you're reading this document, you probably have a good idea of what
146modules, extensions, and so forth are. Nevertheless, just to be sure
147that everyone is operating from a common starting point, we offer the
148following glossary of common Python terms:
149\begin{description}
150\item[module] the basic unit of code reusability in Python: a block of
151 code imported by some other code. There are three types of modules
152 that concern us here: pure Python modules, extension modules, and
153 packages.
154\item[pure Python module] a module written in Python and contained in a
155 single \file{.py} file (and possibly associated \file{.pyc} and/or
156 \file{.pyo} files). Sometimes referred to as a ``pure module.''
157\item[extension module] a module written in the low-level language of
158 the Python implemention: C/C++ for CPython, Java for JPython.
159 Typically contained in a single dynamically loadable pre-compiled
160 file, e.g. a shared object (\file{.so}) file for CPython extensions on
161 Unix, a DLL (given the \file{.pyd} extension) for CPython extensions
162 on Windows, or a Java class file for JPython extensions.
163\item[package] a module that contains other modules; typically contained
164 in a directory in the filesystem and distinguished from other
165 directories by the presence of a file \file{\_\_init\_\_.py}.
166\end{description}
167
168
169\subsection{Distutils-specific terminology}
170\label{sec:distutils-term}
171
172The following terms apply more specifically to the domain of
173distributing Python modules using the Distutils:
174\begin{description}
175\item[module distribution] a collection of Python modules distributed
176 together as a single downloadable resource and meant to be installed
177 \emph{en masse}. Examples of some well-known module distributions are
178 Numeric Python, PyXML, PIL (the Python Imaging Library), or
179 mxDateTime. (This would be called a \emph{package}, except that term
180 is already spoken for in the Python context: a single module
181 distribution may contain zero, one, or many Python packages.)
182\item[pure module distribution] a module distribution that contains only
183 pure Python modules and packages. Sometimes referred to as a ``pure
184 distribution.''
185\item[non-pure module distribution] a module distribution that contains
186 at least one extension module. Sometimes referred to as a ``non-pure
187 distribution.''
188\item[distribution root] the top-level directory of your source tree (or
189 source distribution); the directory where \file{setup.py} exists and
190 is run from
191\end{description}
192
193
194\section{Writing the Setup Script}
195\label{sec:setup-script}
196
197The setup script is the centre of all activity in building,
198distributing, and installing modules using the Distutils. The main
199purpose of the setup script is to describe your module distribution to
200the Distutils, so that the various commands that operate on your modules
201do the right thing. As we saw in section~\ref{sec:simple-example}
202above, the setup script consists mainly of a call to \func{setup()}, and
203all information supplied to the Distutils is suppled as keyword
204arguments to \func{setup()}.
205
206Here's a slightly more involved example, which we'll follow for the next
207couple of sections: the Distutils' own setup script. (Keep in mind that
208although the Distutils are included with Python 1.6, they also have an
209independent existence so that Python 1.5 users can use them to install
210other module distributions.)
211
212\begin{verbatim}
213#!/usr/bin/env python
214
215from distutils.core import setup
216
217setup (name = "Distutils",
218 version = "1.0",
219 description = "Python Module Distribution Utilities",
220 author = "Greg Ward",
221 author_email = "gward@python.net",
222 url = "http://www.python.org/sigs/distutils-sig/",
223
224 packages = ['distutils', 'distutils.command'],
225 )
226\end{verbatim}
227There are only two differences between this and the trivial one-file
228distribution presented in section~\ref{sec:simple-example}: more
229meta-data, and the specification of pure Python modules by package,
230rather than by module. This is important since the Distutils consist of
231a couple of dozen modules split into (so far) two packages; an explicit
232list of every module would be tedious to generate and difficult to
233maintain.
234
235
236\subsection{Package directories}
237\label{sec:package-dirs}
238
239The \option{packages} option tells the Distutils to process (build,
240distribute, install, etc.) all pure Python modules found in each package
241mentioned in the \option{packages} list. In order to do this, of
242course, there has to be a correspondence between package names and
243directories in the filesystem. The default correspondence is the most
244obvious one, i.e. package \package{distutils} is found in the directory
245\file{distutils} relative to the distribution root. Thus, when you say
246\code{packages = ['foo']} in your setup script, you are promising that
247the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
248be spelled differently on your system, but you get the idea) relative to
249the directory where your setup script lives. (If you break this
250promise, the Distutils will issue a warning but process the broken
251package anyways.)
252
253If you use a different convention to lay out your source directory,
254that's no problem: you just have to supply the \option{package\_dir}
255option to tell the Distutils about your convention. For example, say
256you keep all Python source under \file{lib}, so that modules not in any
257package are right in \file{lib}, modules in the \package{foo} package
258are in \file{lib/foo}, and so forth. Then you would put
259\begin{verbatim}
260package_dir = {'': 'lib'}
261\end{verbatim}
262in your setup script. (The keys to this dictionary are package names,
263and an empty package name stands for the ``root package,'' i.e. no
264package at all. The values are directory names relative to your
265distribution root.) In this case, when you say
266\code{packages = ['foo']}, you are promising that the file
267\file{lib/foo/\_\_init\_\_.py} exists.
268
269Another possible convention is to put the \package{foo} package right in
270\file{lib}, the \package{foo.bar} package in \file{lib/bar}, etc. This
271would be written in the setup script as
272\begin{verbatim}
273package_dir = {'foo': 'lib'}
274\end{verbatim}
275Note that a \code{\var{package}: \var{dir}} entry in the
276\option{package\_dir} option implicitly applies to all packages below
277\var{package}, so the \package{foo.bar} case is automatically handled
278here. In this example, having \code{packages = ['foo', 'foo.bar']}
279tells the Distutils to look for \file{lib/__init__.py} and
280\file{lib/bar/__init__.py}.
281
282
283\subsection{Listing individual modules}
284\label{sec:listing-modules}
285
286For a small module distribution, you might prefer to list all modules
287rather than listing packages---especially the case of a single module
288that goes in the ``root package'' (i.e., no package at all). This
289simplest case was shown in section~\ref{sec:simple-example}; here is a
290slightly more involved example:
291\begin{verbatim}
292py_modules = ['mod1', 'pkg.mod2']
293\end{verbatim}
294This describes two modules, one of them in the ``root'' package, the
295other in the \package{pkg} package. Again, the default
296package/directory layout implies that these two modules can be found in
297\file{mod1.py} and \file{pkg/mod2.py}, and that \file{pkg/__init__.py}
298exists as well. And again, you can override the package/directory
299layout using the \option{package\_dir} option. \XXX{not sure if this is
300 actually true---must check!}
301
302
303\section{Writing the Setup Configuration File}
304\label{sec:setup-config}
305
306\XXX{not implemented yet!}
307
308Often, it's not possible to write down everything needed to build a
309distribution \emph{a priori}. You need to get some information from the
310user, or from the user's system, in order to proceed. For example, you
311might include an optional extension module that provides an interface to
312a particular C library. If that library is installed on the user's
313system, then you can build your optional extension---but you need to
314know where to find the header and library file. If it's not installed,
315you need to know this so you can omit your optional extension.
316
317The preferred way to do this, of course, would be for you to tell the
318Distutils which optional features (C libraries, system calls, external
319utilities, etc.) you're looking for, and it would inspect the user's
320system and try to find them. This functionality may appear in a future
321version of the Distutils, but it isn't there now. So, for the time
322being, we rely on the user building and installing your software to
323provide the necessary information. The vehicle for doing so is the
324setup configuration file, \file{setup.cfg}.
325
326\XXX{need more here!}
327
328
329\section{Creating a Source Distribution}
330\label{sec:source-dist}
331
332As shown in section~\ref{sec:simple-example}, you use the
333\command{sdist} command to create a source distribution. In the
334simplest case,
335\begin{verbatim}
336python setup.py sdist
337\end{verbatim}
338(assuming you haven't specified any \command{sdist} options in the setup
339script or config file), \command{sdist} creates the the archive of the
340default format for the current platform. The default formats are:
341\begin{tableii}{ll}{textrm}%
342 {Platform}{Default archive format for source distributions}
343 \lineii{Unix}{gzipped tar file (\file{.tar.gz})}
344 \lineii{Windows}{zip file}
345\end{tableii}
346You can specify as many formats as you like using the \option{--formats}
347option, for example:
348\begin{verbatim}
349python setup.py sdist --formats=gztar,zip
350\end{verbatim}
351to create a gzipped tarball and a zip file. The available formats are:
352\begin{tableii}{ll}{textrm}%
353 {Format}{Description}
354 \lineii{zip}{zip file (\file{.zip})}
355 \lineii{gztar}{gzipped tar file (\file{.tar.gz})}
356 \lineii{ztar}{compressed tar file (\file{.tar.Z})}
357 \lineii{tar}{tar file (\file{.tar})}
358\end{tableii}
359
360
361\subsection{The manifest and manifest template}
362\label{sec:manifest}
363
364Without any additional information, the \command{sdist} command puts a
365minimal set of files into the source distribution:
366\begin{itemize}
367\item all Python source files implied by the \option{py_modules} and
368 \option{packages} options
369\item all C source files mentioned in the \option{ext_modules} or
370 \option{libraries} options (\XXX{getting C library sources currently
371 broken -- no get_source_files() method in build_clib.py!})
372\item anything that looks like a test script: \file{test/test*.py}
373 (currently, the Distutils don't do anything with test scripts except
374 include them in source distributions, but in the future there will be
375 a standard for testing Python module distributions)
376\item \file{README.txt} (or \file{README}) and \file{setup.py}
377\end{itemize}
378Sometimes this is enough, but usually you will want to specify
379additional files to distribute. The typical way to do this is to write
380a \emph{manifest template}, called \file{MANIFEST.in} by default. The
381\command{sdist} command processes this template and generates a manifest
382file, \file{MANIFEST}. (If you prefer, you can skip the manifest
383template and generate the manifest yourself: it just lists one file per
384line.)
385
386The manifest template has one command per line, where each command
387specifies a set of files to include or exclude from the source
388distribution. For an example, again we turn to the Distutils' own
389manifest template:
390\begin{verbatim}
391include *.txt
392recursive-include examples *.txt
393recursive-include examples *.py
394prune examples/sample?/build
395\end{verbatim}
396The meanings should be fairly clear: include all files in the
397distribution root matching \code{*.txt}, all files anywhere under the
398\file{examples} directory matching \code{*.txt} or \code{*.py}, and
399exclude all directories matching \code{examples/sample?/build}. There
400are several other commands available in the manifest template
401mini-language; see section~\ref{sec:sdist-cmd}.
402
403The order of commands in the manifest template very much matters:
404initially, we have the list of default files as described above, and
405each command in the template adds to or removes from that list of files.
406When we have fully processed the manifest template, we have our complete
407list of files. This list is written to the manifest for future
408reference, and then used to build the source distribution archive(s).
409
410We can now see how the \command{sdist} command will build the list of
411files to include in the Distutils source distribution:
412\begin{enumerate}
413\item include all Python source files in the \file{distutils} and
414 \file{distutils/command} subdirectories (because packages
415 corresponding to those two directories were mentioned in the
416 \option{packages} option in the setup script)
417\item include \file{test/test*.py} (always included)
418\item include \file{README.txt} and \file{setup.py} (always included)
419\item include \file{*.txt} in the distribution root (this will find
420 \file{README.txt} a second time, but such redundancies are weeded out
421 later)
422\item in the sub-tree under \file{examples}, include anything matching
423 \file{*.txt}
424\item in the sub-tree under \file{examples}, include anything matching
425 \file{*.py}
426\item remove all files in the sub-trees starting at directories matching
427 \file{examples/sample?/build}---this may exclude files included by the
428 previous two steps, so it's important that the \code{prune} command in
429 the manifest template comes after the two \code{recursive-include}
430 commands
431\end{itemize}
432
433
434\subsection{Manifest-related options}
435\label{sec:manifest-options}
436
437The normal course of operations for the \command{sdist} command is as
438follows:
439\begin{itemize}
440\item if \file{MANIFEST.in} is more recent than \file{MANIFEST}, or
441 \file{MANIFEST} doesn't exist at all, recreate \file{MANIFEST} by
442 processing \file{MANIFEST.in}
443\item use the list of files now in \file{MANIFEST} (either just
444 generated or read in) to create the source distribution archive(s)
445\end{itemize}
446There are a couple of options that modify this behaviour.
447
448First, you might want to force the manifest to be regenerated---for
449example, if you have added or removed files or directories that match an
450existing pattern in the manifest template, you should regenerate the
451manifest:
452\begin{verbatim}
453python setup.py sdist --force-manifest
454\end{verbatim}
455\XXX{this is stupid, but is there a better way to do it without
456 reprocessing MANIFEST.in every single bloody time?}
457
458Or, you might just want to (re)generate the manifest, but not create a
459source distribution:
460\begin{verbatim}
461python setup.py sdist --manifest-only
462\end{verbatim}
463(\option{--manifest-only} implies \option{--force-manifest}.)
464
465If you don't want to use the default file set, you can supply the
466\option{--no-defaults} option. If you use \option{--no-defaults} and
467don't supply a manifest template (or it's empty, or nothing matches the
468patterns in it), then your source distribution will be empty.
469
470
471\section{Creating Built Distributions}
472\label{sec:built-dist}
473
474
475
476\section{Examples}
477\label{sec:examples}
478
479
480\subsection{Pure Python distribution (by module)}
481\label{sec:pure-mod}
482
483
484\subsection{Pure Python distribution (by package)}
485\label{sec:pure-pkg}
486
487
488\subsection{Single extension module}
489\label{sec:single-ext}
490
491
492\subsection{Multiple extension modules}
493\label{sec:multiple-ext}
494
495
496\subsection{Putting it all together}
497
498
499\section{Reference}
500\label{sec:ref}
501
502
503\subsection{Building modules: the \command{build} command family}
504\label{sec:build-cmds}
505
506\subsubsection{\command{build}}
507\label{sec:build-cmd}
508
509\subsubsection{\command{build\_py}}
510\label{sec:build-py-cmd}
511
512\subsubsection{\command{build\_ext}}
513\label{sec:build-ext-cmd}
514
515\subsubsection{\command{build\_clib}}
516\label{sec:build-clib-cmd}
517
518
519\subsection{Installing modules: the \command{install} command family}
520\label{sec:install-cmd}
521
522
523\subsection{Cleaning up: the \command{clean} command}
524\label{sec:clean-cmd}
525
526
527\subsection{Creating a source distribution: the \command{sdist} command}
528\label{sec:sdist-cmd}
529
530
531\XXX{fragment moved down from above: needs context!}
532The manifest template commands are:
533\begin{tableii}{ll}{command}{Command}{Description}
534 \lineii{include \var{pat}}{include all files matching \var{pat}}
535 \lineii{exclude \var{pat}}{exclude all files matching \var{pat}}
536 \lineii{recursive-include \var{dir} \var{pat}}
537 {include all files under \var{dir} matching \var{pat}}
538 \lineii{recursive-exclude \var{dir} \var{pat}}
539 {exclude all files under \var{dir} matching \var{pat}}
540 \lineii{global-include \var{pat}}
541 {include all files anywhere in the source tree matching \var{pat}}
542 \lineii{global-exclude \var{pat}}
543 {exclude all files anywhere in the source tree matching \var{pat}}
544 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
545 \lineii{graft \var{dir}}{include all files under \var{dir}}
546\end{tableii}
547The patterns here are Unix-style ``glob'' patterns: \code{*} matches any
548sequence of regular filename characters, \code{?} matches any single
549regular filename character, and \code{[\var{range}]} matches any of the
550characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
551\code{a-f0-9_.}). The definition of ``regular filename character'' is
552platform-specific: on Unix it is anything except slash; on Windows
553anything except backslash or colon; on Mac OS anything except colon.
554\XXX{Windows and Mac OS support not there yet}
555
556
557\subsection{Creating a ``built'' distribution: the \command{bdist} command
558 family}
559\label{sec:bdist-cmds}
560
561
562\subsubsection{\command{blib}}
563
564\subsubsection{\command{blib\_dumb}}
565
566\subsubsection{\command{blib\_rpm}}
567
568\subsubsection{\command{blib\_wise}}
569
570
571
572
573
574
575
576
Greg Wardabc52162000-02-26 00:52:48 +0000577\end{document}