7 % Document extension.read_setup_file
8 % Document build_clib command
11 \title{Distributing Python Modules
}
14 \authoraddress{Email:
\email{distutils-sig@python.org
}}
23 This
document describes the Python Distribution Utilities
24 (``Distutils'') from the module developer's point of view, describing
25 how to use the Distutils to make Python modules and extensions easily
26 available to a wider audience with very little overhead for
27 build/release/install mechanics.
30 % The ugly "%begin{latexonly}" pseudo-environment supresses the table
31 % of contents for HTML generation.
38 \section{Introduction
}
41 This
document covers using the Distutils to distribute your Python
42 modules, concentrating on the role of developer/distributor: if
43 you're looking for information on installing Python modules, you
44 should refer to the
\citetitle[../inst/inst.html
]{Installing Python
48 \section{Concepts \& Terminology
}
51 Using the Distutils is quite simple, both for module developers and for
52 users/administrators installing third-party modules. As a developer,
53 your responsibilities (apart from writing solid, well-documented and
54 well-tested code, of course!) are:
56 \item write a setup script (
\file{setup.py
} by convention)
57 \item (optional) write a setup configuration file
58 \item create a source distribution
59 \item (optional) create one or more built (binary) distributions
61 Each of these tasks is covered in this
document.
63 Not all module developers have access to a multitude of platforms, so
64 it's not always feasible to expect them to create a multitude of built
65 distributions. It is hoped that a class of intermediaries, called
66 \emph{packagers
}, will arise to address this need. Packagers will take
67 source distributions released by module developers, build them on one or
68 more platforms, and release the resulting built distributions. Thus,
69 users on the most popular platforms will be able to install most popular
70 Python module distributions in the most natural way for their platform,
71 without having to run a single setup script or compile a line of code.
74 \subsection{A Simple Example
}
75 \label{simple-example
}
77 The setup script is usually quite simple, although since it's written
78 in Python, there are no arbitrary limits to what you can do with it,
79 though you should be careful about putting arbitrarily expensive
80 operations in your setup script. Unlike, say, Autoconf-style configure
81 scripts, the setup script may be run multiple times in the course of
82 building and installing your module distribution.
84 If all you want to do is distribute a module called
\module{foo
},
85 contained in a file
\file{foo.py
}, then your setup script can be as
89 from distutils.core import setup
97 \item most information that you supply to the Distutils is supplied as
98 keyword arguments to the
\function{setup()
} function
99 \item those keyword arguments fall into two categories: package
100 metadata (name, version number) and information about what's in the
101 package (a list of pure Python modules, in this case)
102 \item modules are specified by module name, not filename (the same will
103 hold true for packages and extensions)
104 \item it's recommended that you supply a little more metadata, in
105 particular your name, email address and a URL for the project
106 (see section~
\ref{setup-script
} for an example)
109 To create a source distribution for this module, you would create a
110 setup script,
\file{setup.py
}, containing the above code, and run:
113 python setup.py sdist
116 which will create an archive file (e.g., tarball on
\UNIX, ZIP file on
117 Windows) containing your setup script
\file{setup.py
}, and your module
118 \file{foo.py
}. The archive file will be named
\file{foo-
1.0.tar.gz
} (or
119 \file{.zip
}), and will unpack into a directory
\file{foo-
1.0}.
121 If an end-user wishes to install your
\module{foo
} module, all she has
122 to do is download
\file{foo-
1.0.tar.gz
} (or
\file{.zip
}), unpack it,
123 and---from the
\file{foo-
1.0} directory---run
126 python setup.py install
129 which will ultimately copy
\file{foo.py
} to the appropriate directory
130 for third-party modules in their Python installation.
132 This simple example demonstrates some fundamental concepts of the
133 Distutils. First, both developers and installers have the same basic
134 user interface, i.e. the setup script. The difference is which
135 Distutils
\emph{commands
} they use: the
\command{sdist
} command is
136 almost exclusively for module developers, while
\command{install
} is
137 more often for installers (although most developers will want to install
138 their own code occasionally).
140 If you want to make things really easy for your users, you can create
141 one or more built distributions for them. For instance, if you are
142 running on a Windows machine, and want to make things easy for other
143 Windows users, you can create an executable installer (the most
144 appropriate type of built distribution for this platform) with the
145 \command{bdist
\_wininst} command. For example:
148 python setup.py bdist_wininst
151 will create an executable installer,
\file{foo-
1.0.win32.exe
}, in the
154 Other useful built distribution formats are RPM, implemented by the
155 \command{bdist
\_rpm} command, Solaris
\program{pkgtool
}
156 (
\command{bdist
\_pkgtool}), and HP-UX
\program{swinstall
}
157 (
\command{bdist_sdux
}). For example, the following command will
158 create an RPM file called
\file{foo-
1.0.noarch.rpm
}:
161 python setup.py bdist_rpm
164 (The
\command{bdist
\_rpm} command uses the
\command{rpm
} executable,
165 therefore this has to be run on an RPM-based system such as Red Hat
166 Linux, SuSE Linux, or Mandrake Linux.)
168 You can find out what distribution formats are available at any time by
172 python setup.py bdist --help-formats
176 \subsection{General Python terminology
}
179 If you're reading this
document, you probably have a good idea of what
180 modules, extensions, and so forth are. Nevertheless, just to be sure
181 that everyone is operating from a common starting point, we offer the
182 following glossary of common Python terms:
184 \item[module
] the basic unit of code reusability in Python: a block of
185 code imported by some other code. Three types of modules concern us
186 here: pure Python modules, extension modules, and packages.
188 \item[pure Python module
] a module written in Python and contained in a
189 single
\file{.py
} file (and possibly associated
\file{.pyc
} and/or
190 \file{.pyo
} files). Sometimes referred to as a ``pure module.''
192 \item[extension module
] a module written in the low-level language of
193 the Python implementation: C/
\Cpp{} for Python, Java for Jython.
194 Typically contained in a single dynamically loadable pre-compiled
195 file, e.g. a shared object (
\file{.so
}) file for Python extensions on
196 \UNIX, a DLL (given the
\file{.pyd
} extension) for Python extensions
197 on Windows, or a Java class file for Jython extensions. (Note that
198 currently, the Distutils only handles C/
\Cpp{} extensions for Python.)
200 \item[package
] a module that contains other modules; typically contained
201 in a directory in the filesystem and distinguished from other
202 directories by the presence of a file
\file{\_\_init\_\_.py
}.
204 \item[root package
] the root of the hierarchy of packages. (This isn't
205 really a package, since it doesn't have an
\file{\_\_init\_\_.py
}
206 file. But we have to call it something.) The vast majority of the
207 standard library is in the root package, as are many small, standalone
208 third-party modules that don't belong to a larger module collection.
209 Unlike regular packages, modules in the root package can be found in
210 many directories: in fact, every directory listed in
\code{sys.path
}
211 contributes modules to the root package.
215 \subsection{Distutils-specific terminology
}
216 \label{distutils-term
}
218 The following terms apply more specifically to the domain of
219 distributing Python modules using the Distutils:
221 \item[module distribution
] a collection of Python modules distributed
222 together as a single downloadable resource and meant to be installed
223 \emph{en masse
}. Examples of some well-known module distributions are
224 Numeric Python, PyXML, PIL (the Python Imaging Library), or
225 mxBase. (This would be called a
\emph{package
}, except that term
226 is already taken in the Python context: a single module distribution
227 may contain zero, one, or many Python packages.)
229 \item[pure module distribution
] a module distribution that contains only
230 pure Python modules and packages. Sometimes referred to as a ``pure
233 \item[non-pure module distribution
] a module distribution that contains
234 at least one extension module. Sometimes referred to as a ``non-pure
237 \item[distribution root
] the top-level directory of your source tree (or
238 source distribution); the directory where
\file{setup.py
} exists. Generally
239 \file{setup.py
} will be run from this directory.
243 \section{Writing the Setup Script
}
246 The setup script is the centre of all activity in building,
247 distributing, and installing modules using the Distutils. The main
248 purpose of the setup script is to describe your module distribution to
249 the Distutils, so that the various commands that operate on your modules
250 do the right thing. As we saw in section~
\ref{simple-example
} above,
251 the setup script consists mainly of a call to
\function{setup()
}, and
252 most information supplied to the Distutils by the module developer is
253 supplied as keyword arguments to
\function{setup()
}.
255 Here's a slightly more involved example, which we'll follow for the next
256 couple of sections: the Distutils' own setup script. (Keep in mind that
257 although the Distutils are included with Python
1.6 and later, they also
258 have an independent existence so that Python
1.5.2 users can use them to
259 install other module distributions. The Distutils' own setup script,
260 shown here, is used to install the package into Python
1.5.2.)
263 #!/usr/bin/env python
265 from distutils.core import setup
267 setup(name="Distutils",
269 description="Python Distribution Utilities",
271 author_email="gward@python.net",
272 url="http://www.python.org/sigs/distutils-sig/",
273 packages=
['distutils', 'distutils.command'
],
277 There are only two differences between this and the trivial one-file
278 distribution presented in section~
\ref{simple-example
}: more
279 metadata, and the specification of pure Python modules by package,
280 rather than by module. This is important since the Distutils consist of
281 a couple of dozen modules split into (so far) two packages; an explicit
282 list of every module would be tedious to generate and difficult to
283 maintain. For more information on the additional meta-data, see
284 section~
\ref{meta-data
}.
286 Note that any pathnames (files or directories) supplied in the setup
287 script should be written using the
\UNIX{} convention, i.e.
288 slash-separated. The Distutils will take care of converting this
289 platform-neutral representation into whatever is appropriate on your
290 current platform before actually using the pathname. This makes your
291 setup script portable across operating systems, which of course is one
292 of the major goals of the Distutils. In this spirit, all pathnames in
293 this
document are slash-separated. (MacOS programmers should keep in
294 mind that the
\emph{absence
} of a leading slash indicates a relative
295 path, the opposite of the MacOS convention with colons.)
297 This, of course, only applies to pathnames given to Distutils
298 functions. If you, for example, use standard Python functions such as
299 \function{glob.glob()
} or
\function{os.listdir()
} to specify files, you
300 should be careful to write portable code instead of hardcoding path
304 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
305 os.listdir(os.path.join('mydir', 'subdir'))
309 \subsection{Listing whole packages
}
310 \label{listing-packages
}
312 The
\option{packages
} option tells the Distutils to process (build,
313 distribute, install, etc.) all pure Python modules found in each package
314 mentioned in the
\option{packages
} list. In order to do this, of
315 course, there has to be a correspondence between package names and
316 directories in the filesystem. The default correspondence is the most
317 obvious one, i.e. package
\module{distutils
} is found in the directory
318 \file{distutils
} relative to the distribution root. Thus, when you say
319 \code{packages =
['foo'
]} in your setup script, you are promising that
320 the Distutils will find a file
\file{foo/
\_\_init\_\_.py
} (which might
321 be spelled differently on your system, but you get the idea) relative to
322 the directory where your setup script lives. If you break this
323 promise, the Distutils will issue a warning but still process the broken
326 If you use a different convention to lay out your source directory,
327 that's no problem: you just have to supply the
\option{package
\_dir}
328 option to tell the Distutils about your convention. For example, say
329 you keep all Python source under
\file{lib
}, so that modules in the
330 ``root package'' (i.e., not in any package at all) are in
331 \file{lib
}, modules in the
\module{foo
} package are in
\file{lib/foo
},
332 and so forth. Then you would put
335 package_dir =
{'': 'lib'
}
338 in your setup script. The keys to this dictionary are package names,
339 and an empty package name stands for the root package. The values are
340 directory names relative to your distribution root. In this case, when
341 you say
\code{packages =
['foo'
]}, you are promising that the file
342 \file{lib/foo/
\_\_init\_\_.py
} exists.
344 Another possible convention is to put the
\module{foo
} package right in
345 \file{lib
}, the
\module{foo.bar
} package in
\file{lib/bar
}, etc. This
346 would be written in the setup script as
349 package_dir =
{'foo': 'lib'
}
352 A
\code{\var{package
}:
\var{dir
}} entry in the
\option{package
\_dir}
353 dictionary implicitly applies to all packages below
\var{package
}, so
354 the
\module{foo.bar
} case is automatically handled here. In this
355 example, having
\code{packages =
['foo', 'foo.bar'
]} tells the Distutils
356 to look for
\file{lib/
\_\_init\_\_.py
} and
357 \file{lib/bar/
\_\_init\_\_.py
}. (Keep in mind that although
358 \option{package
\_dir} applies recursively, you must explicitly list all
359 packages in
\option{packages
}: the Distutils will
\emph{not
} recursively
360 scan your source tree looking for any directory with an
361 \file{\_\_init\_\_.py
} file.)
364 \subsection{Listing individual modules
}
365 \label{listing-modules
}
367 For a small module distribution, you might prefer to list all modules
368 rather than listing packages---especially the case of a single module
369 that goes in the ``root package'' (i.e., no package at all). This
370 simplest case was shown in section~
\ref{simple-example
}; here is a
371 slightly more involved example:
374 py_modules =
['mod1', 'pkg.mod2'
]
377 This describes two modules, one of them in the ``root'' package, the
378 other in the
\module{pkg
} package. Again, the default package/directory
379 layout implies that these two modules can be found in
\file{mod1.py
} and
380 \file{pkg/mod2.py
}, and that
\file{pkg/
\_\_init\_\_.py
} exists as well.
381 And again, you can override the package/directory correspondence using
382 the
\option{package
\_dir} option.
385 \subsection{Describing extension modules
}
386 \label{describing-extensions
}
388 % XXX read over this section
389 Just as writing Python extension modules is a bit more complicated than
390 writing pure Python modules, describing them to the Distutils is a bit
391 more complicated. Unlike pure modules, it's not enough just to list
392 modules or packages and expect the Distutils to go out and find the
393 right files; you have to specify the extension name, source file(s), and
394 any compile/link requirements (include directories, libraries to link
397 All of this is done through another keyword argument to
398 \function{setup()
}, the
\option{extensions
} option.
\option{extensions
}
399 is just a list of
\class{Extension
} instances, each of which describes a
400 single extension module. Suppose your distribution includes a single
401 extension, called
\module{foo
} and implemented by
\file{foo.c
}. If no
402 additional instructions to the compiler/linker are needed, describing
403 this extension is quite simple:
406 uExtension("foo",
["foo.c"
])
409 The
\class{Extension
} class can be imported from
410 \module{distutils.core
} along with
\function{setup()
}. Thus, the setup
411 script for a module distribution that contains only this one extension
412 and nothing else might be:
415 from distutils.core import setup, Extension
416 setup(name="foo", version="
1.0",
417 ext_modules=
[Extension("foo",
["foo.c"
])
])
420 The
\class{Extension
} class (actually, the underlying extension-building
421 machinery implemented by the
\command{build
\_ext} command) supports a
422 great deal of flexibility in describing Python extensions, which is
423 explained in the following sections.
426 \subsubsection{Extension names and packages
}
428 The first argument to the
\class{Extension
} constructor is always the
429 name of the extension, including any package names. For example,
432 Extension("foo",
["src/foo1.c", "src/foo2.c"
])
435 describes an extension that lives in the root package, while
438 Extension("pkg.foo",
["src/foo1.c", "src/foo2.c"
])
441 describes the same extension in the
\module{pkg
} package. The source
442 files and resulting object code are identical in both cases; the only
443 difference is where in the filesystem (and therefore where in Python's
444 namespace hierarchy) the resulting extension lives.
446 If you have a number of extensions all in the same package (or all under
447 the same base package), use the
\option{ext
\_package} keyword argument
448 to
\function{setup()
}. For example,
453 ext_modules=
[Extension("foo",
["foo.c"
]),
454 Extension("subpkg.bar",
["bar.c"
])
]
458 will compile
\file{foo.c
} to the extension
\module{pkg.foo
}, and
459 \file{bar.c
} to
\module{pkg.subpkg.bar
}.
462 \subsubsection{Extension source files
}
464 The second argument to the
\class{Extension
} constructor is a list of
465 source files. Since the Distutils currently only support C,
\Cpp, and
466 Objective-C extensions, these are normally C/
\Cpp/Objective-C source
467 files. (Be sure to use appropriate extensions to distinguish
\Cpp\
468 source files:
\file{.cc
} and
\file{.cpp
} seem to be recognized by both
469 \UNIX{} and Windows compilers.)
471 However, you can also include SWIG interface (
\file{.i
}) files in the
472 list; the
\command{build
\_ext} command knows how to deal with SWIG
473 extensions: it will run SWIG on the interface file and compile the
474 resulting C/
\Cpp{} file into your extension.
476 \XXX{SWIG support is rough around the edges and largely untested;
477 especially SWIG support for
\Cpp{} extensions! Explain in more detail
478 here when the interface firms up.
}
480 On some platforms, you can include non-source files that are processed
481 by the compiler and included in your extension. Currently, this just
482 means Windows message text (
\file{.mc
}) files and resource definition
483 (
\file{.rc
}) files for Visual
\Cpp. These will be compiled to binary resource
484 (
\file{.res
}) files and linked into the executable.
487 \subsubsection{Preprocessor options
}
489 Three optional arguments to
\class{Extension
} will help if you need to
490 specify include directories to search or preprocessor macros to
491 define/undefine:
\code{include
\_dirs},
\code{define
\_macros}, and
492 \code{undef
\_macros}.
494 For example, if your extension requires header files in the
495 \file{include
} directory under your distribution root, use the
496 \code{include
\_dirs} option:
499 Extension("foo",
["foo.c"
], include_dirs=
["include"
])
502 You can specify absolute directories there; if you know that your
503 extension will only be built on
\UNIX{} systems with X11R6 installed to
504 \file{/usr
}, you can get away with
507 Extension("foo",
["foo.c"
], include_dirs=
["/usr/include/X11"
])
510 You should avoid this sort of non-portable usage if you plan to
511 distribute your code: it's probably better to write C code like
513 #include <X11/Xlib.h>
516 If you need to include header files from some other Python extension,
517 you can take advantage of the fact that header files are installed in a
518 consistent way by the Distutils
\command{install
\_header} command. For
519 example, the Numerical Python header files are installed (on a standard
520 Unix installation) to
\file{/usr/local/include/python1.5/Numerical
}.
521 (The exact location will differ according to your platform and Python
522 installation.) Since the Python include
523 directory---
\file{/usr/local/include/python1.5
} in this case---is always
524 included in the search path when building Python extensions, the best
525 approach is to write C code like
527 #include <Numerical/arrayobject.h>
529 If you must put the
\file{Numerical
} include directory right into your
530 header search path, though, you can find that directory using the
531 Distutils
\module{sysconfig
} module:
534 from distutils.sysconfig import get_python_inc
535 incdir = os.path.join(get_python_inc(plat_specific=
1), "Numerical")
537 Extension(..., include_dirs=
[incdir
]))
540 Even though this is quite portable---it will work on any Python
541 installation, regardless of platform---it's probably easier to just
542 write your C code in the sensible way.
544 You can define and undefine pre-processor macros with the
545 \code{define
\_macros} and
\code{undef
\_macros} options.
546 \code{define
\_macros} takes a list of
\code{(name, value)
} tuples, where
547 \code{name
} is the name of the macro to define (a string) and
548 \code{value
} is its value: either a string or
\code{None
}. (Defining a
549 macro
\code{FOO
} to
\code{None
} is the equivalent of a bare
550 \code{\#define FOO
} in your C source: with most compilers, this sets
551 \code{FOO
} to the string
\code{1}.)
\code{undef
\_macros} is just
552 a list of macros to undefine.
558 define_macros=
[('NDEBUG', '
1'),
559 ('HAVE_STRFTIME', None)
],
560 undef_macros=
['HAVE_FOO', 'HAVE_BAR'
])
563 is the equivalent of having this at the top of every C source file:
567 #define HAVE_STRFTIME
573 \subsubsection{Library options
}
575 You can also specify the libraries to link against when building your
576 extension, and the directories to search for those libraries. The
577 \code{libraries
} option is a list of libraries to link against,
578 \code{library
\_dirs} is a list of directories to search for libraries at
579 link-time, and
\code{runtime
\_library\_dirs} is a list of directories to
580 search for shared (dynamically loaded) libraries at run-time.
582 For example, if you need to link against libraries known to be in the
583 standard library search path on target systems
587 libraries=
["gdbm", "readline"
])
590 If you need to link with libraries in a non-standard location, you'll
591 have to include the location in
\code{library
\_dirs}:
595 library_dirs=
["/usr/X11R6/lib"
],
596 libraries=
["X11", "Xt"
])
599 (Again, this sort of non-portable construct should be avoided if you
600 intend to distribute your code.)
602 \XXX{Should mention clib libraries here or somewhere else!
}
604 \subsubsection{Other options
}
606 There are still some other options which can be used to handle special
609 The
\option{extra
\_objects} option is a list of object files to be passed
610 to the linker. These files must not have extensions, as the default
611 extension for the compiler is used.
613 \option{extra
\_compile\_args} and
\option{extra
\_link\_args} can be used
614 to specify additional command line options for the respective compiler and
615 linker command lines.
617 \option{export
\_symbols} is only useful on Windows. It can contain a list
618 of symbols (functions or variables) to be exported. This option
619 is not needed when building compiled extensions: Distutils
620 will automatically add
\code{initmodule
}
621 to the list of exported symbols.
623 \subsection{Installing Scripts
}
624 So far we have been dealing with pure and non-pure Python modules,
625 which are usually not run by themselves but imported by scripts.
627 Scripts are files containing Python source code, intended to be
628 started from the command line. Scripts don't require Distutils to do
629 anything very complicated. The only clever feature is that if the
630 first line of the script starts with
\code{\#!
} and contains the word
631 ``python'', the Distutils will adjust the first line to refer to the
632 current interpreter location.
634 The
\option{scripts
} option simply is a list of files to be handled
635 in this way. From the PyXML setup script:
639 scripts =
['scripts/xmlproc_parse', 'scripts/xmlproc_val'
]
644 \subsection{Installing Additional Files
}
646 The
\option{data
\_files} option can be used to specify additional
647 files needed by the module distribution: configuration files, message
648 catalogs, data files, anything which doesn't fit in the previous
651 \option{data
\_files} specifies a sequence of (
\var{directory
},
652 \var{files
}) pairs in the following way:
656 data_files=
[('bitmaps',
['bm/b1.gif', 'bm/b2.gif'
]),
657 ('config',
['cfg/data.cfg'
]),
658 ('/etc/init.d',
['init-script'
])
]
662 Note that you can specify the directory names where the data files
663 will be installed, but you cannot rename the data files themselves.
665 Each (
\var{directory
},
\var{files
}) pair in the sequence specifies the
666 installation directory and the files to install there. If
667 \var{directory
} is a relative path, it is interpreted relative to the
668 installation prefix (Python's
\code{sys.prefix
} for pure-Python
669 packages,
\code{sys.exec_prefix
} for packages that contain extension
670 modules). Each file name in
\var{files
} is interpreted relative to
671 the
\file{setup.py
} script at the top of the package source
672 distribution. No directory information from
\var{files
} is used to
673 determine the final location of the installed file; only the name of
676 You can specify the
\option{data
\_files} options as a simple sequence
677 of files without specifying a target directory, but this is not recommended,
678 and the
\command{install
} command will print a warning in this case.
679 To install data files directly in the target directory, an empty
680 string should be given as the directory.
682 \subsection{Additional meta-data
}
685 The setup script may include additional meta-data beyond the name and
686 version. This information includes:
688 \begin{tableiv
}{l|l|l|c
}{code
}%
689 {Meta-Data
}{Description
}{Value
}{Notes
}
690 \lineiv{name
}{name of the package
}
692 \lineiv{version
}{version of this release
}
693 {short string
}{(
1)(
2)
}
694 \lineiv{author
}{package author's name
}
696 \lineiv{author_email
}{email address of the package author
}
698 \lineiv{maintainer
}{package maintainer's name
}
700 \lineiv{maintainer_email
}{email address of the package maintainer
}
702 \lineiv{url
}{home page for the package
}
704 \lineiv{description
}{short, summary description of the package
}
706 \lineiv{long_description
}{longer description of the package
}
708 \lineiv{download_url
}{location where the package may be downloaded
}
710 \lineiv{classifiers
}{a list of Trove classifiers
}
711 {list of strings
}{(
4)
}
716 \item[(
1)
] These fields are required.
717 \item[(
2)
] It is recommended that versions take the form
718 \emph{major.minor
\optional{.patch
\optional{.sub
}}}.
719 \item[(
3)
] Either the author or the maintainer must be identified.
720 \item[(
4)
] These fields should not be used if your package is to be
721 compatible with Python versions prior to
2.2.3 or
2.3. The list is
722 available from the
\ulink{PyPI website
}{http://www.python.org/pypi
}.
724 \item["short string"
] A single line of text, not more than
200 characters.
725 \item["long string"
] Multiple lines of plain text in ReStructuredText
726 format (see
\url{http://docutils.sf.net/
}).
727 \item["list of strings"
] See below.
730 None of the string values may be Unicode.
732 Encoding the version information is an art in itself. Python packages
733 generally adhere to the version format
734 \emph{major.minor
\optional{.patch
}\optional{sub
}}. The major number is
736 initial, experimental releases of software. It is incremented for
737 releases that represent major milestones in a package. The minor
738 number is incremented when important new features are added to the
739 package. The patch number increments when bug-fix releases are
740 made. Additional trailing version information is sometimes used to
741 indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
742 where functionality and API may change), "b1,b2,...,bN" (for beta
743 releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
744 pre-release release testing). Some examples:
747 \item[0.1.0] the first, experimental release of a package
748 \item[1.0.1a2
] the second alpha release of the first patch version of
1.0
751 \option{classifiers
} are specified in a python list:
756 'Development Status ::
4 - Beta',
757 'Environment :: Console',
758 'Environment :: Web Environment',
759 'Intended Audience :: End Users/Desktop',
760 'Intended Audience :: Developers',
761 'Intended Audience :: System Administrators',
762 'License :: OSI Approved :: Python Software Foundation License',
763 'Operating System :: MacOS :: MacOS X',
764 'Operating System :: Microsoft :: Windows',
765 'Operating System :: POSIX',
766 'Programming Language :: Python',
767 'Topic :: Communications :: Email',
768 'Topic :: Office/Business',
769 'Topic :: Software Development :: Bug Tracking',
774 If you wish to include classifiers in your
\file{setup.py
} file and also
775 wish to remain backwards-compatible with Python releases prior to
2.2.3,
776 then you can include the following code fragment in your
\file{setup.py
}
777 before the
\code{setup()
} call.
780 # patch distutils if it can't cope with the "classifiers" or
781 # "download_url" keywords
782 if sys.version < '
2.2.3':
783 from distutils.dist import DistributionMetadata
784 DistributionMetadata.classifiers = None
785 DistributionMetadata.download_url = None
789 \subsection{Debugging the setup script
}
792 Sometimes things go wrong, and the setup script doesn't do what the
795 Distutils catches any exceptions when running the setup script, and
796 print a simple error message before the script is terminated. The
797 motivation for this behaviour is to not confuse administrators who
798 don't know much about Python and are trying to install a package. If
799 they get a big long traceback from deep inside the guts of Distutils,
800 they may think the package or the Python installation is broken
801 because they don't read all the way down to the bottom and see that
802 it's a permission problem.
804 On the other hand, this doesn't help the developer to find the cause
805 of the failure. For this purpose, the DISTUTILS_DEBUG environment
806 variable can be set to anything except an empty string, and distutils
807 will now print detailed information what it is doing, and prints the
808 full traceback in case an exception occurs.
810 \section{Writing the Setup Configuration File
}
813 Often, it's not possible to write down everything needed to build a
814 distribution
\emph{a priori
}: you may need to get some information from
815 the user, or from the user's system, in order to proceed. As long as
816 that information is fairly simple---a list of directories to search for
817 C header files or libraries, for example---then providing a
818 configuration file,
\file{setup.cfg
}, for users to edit is a cheap and
819 easy way to solicit it. Configuration files also let you provide
820 default values for any command option, which the installer can then
821 override either on the command-line or by editing the config file.
823 % (If you have more advanced needs, such as determining which extensions
824 % to build based on what capabilities are present on the target system,
825 % then you need the Distutils ``auto-configuration'' facility. This
826 % started to appear in Distutils 0.9 but, as of this writing, isn't mature
827 % or stable enough yet for real-world use.)
829 The setup configuration file is a useful middle-ground between the setup
830 script---which, ideally, would be opaque to installers
\footnote{This
831 ideal probably won't be achieved until auto-configuration is fully
832 supported by the Distutils.
}---and the command-line to the setup
833 script, which is outside of your control and entirely up to the
834 installer. In fact,
\file{setup.cfg
} (and any other Distutils
835 configuration files present on the target system) are processed after
836 the contents of the setup script, but before the command-line. This has
837 several useful consequences:
839 \item installers can override some of what you put in
\file{setup.py
} by
840 editing
\file{setup.cfg
}
841 \item you can provide non-standard defaults for options that are not
842 easily set in
\file{setup.py
}
843 \item installers can override anything in
\file{setup.cfg
} using the
844 command-line options to
\file{setup.py
}
847 The basic syntax of the configuration file is simple:
855 where
\var{command
} is one of the Distutils commands (e.g.
856 \command{build
\_py},
\command{install
}), and
\var{option
} is one of
857 the options that command supports. Any number of options can be
858 supplied for each command, and any number of command sections can be
859 included in the file. Blank lines are ignored, as are comments, which
860 run from a
\character{\#
} character until the end of the line. Long
861 option values can be split across multiple lines simply by indenting
862 the continuation lines.
864 You can find out the list of options supported by a particular command
865 with the universal
\longprogramopt{help
} option, e.g.
868 > python setup.py --help build_ext
870 Options for 'build_ext' command:
871 --build-lib (-b) directory for compiled extension modules
872 --build-temp (-t) directory for temporary files (build by-products)
873 --inplace (-i) ignore build-lib and put compiled extensions into the
874 source directory alongside your pure Python modules
875 --include-dirs (-I) list of directories to search for header files
876 --define (-D) C preprocessor macros to define
877 --undef (-U) C preprocessor macros to undefine
881 Note that an option spelled
\longprogramopt{foo-bar
} on the command-line
882 is spelled
\option{foo
\_bar} in configuration files.
884 For example, say you want your extensions to be built
885 ``in-place''---that is, you have an extension
\module{pkg.ext
}, and you
886 want the compiled extension file (
\file{ext.so
} on
\UNIX, say) to be put
887 in the same source directory as your pure Python modules
888 \module{pkg.mod1
} and
\module{pkg.mod2
}. You can always use the
889 \longprogramopt{inplace
} option on the command-line to ensure this:
892 python setup.py build_ext --inplace
895 But this requires that you always specify the
\command{build
\_ext}
896 command explicitly, and remember to provide
\longprogramopt{inplace
}.
897 An easier way is to ``set and forget'' this option, by encoding it in
898 \file{setup.cfg
}, the configuration file for this distribution:
905 This will affect all builds of this module distribution, whether or not
906 you explcitly specify
\command{build
\_ext}. If you include
907 \file{setup.cfg
} in your source distribution, it will also affect
908 end-user builds---which is probably a bad idea for this option, since
909 always building extensions in-place would break installation of the
910 module distribution. In certain peculiar cases, though, modules are
911 built right in their installation directory, so this is conceivably a
912 useful ability. (Distributing extensions that expect to be built in
913 their installation directory is almost always a bad idea, though.)
915 Another example: certain commands take a lot of options that don't
916 change from run to run; for example,
\command{bdist
\_rpm} needs to know
917 everything required to generate a ``spec'' file for creating an RPM
918 distribution. Some of this information comes from the setup script, and
919 some is automatically generated by the Distutils (such as the list of
920 files installed). But some of it has to be supplied as options to
921 \command{bdist
\_rpm}, which would be very tedious to do on the
922 command-line for every run. Hence, here is a snippet from the
923 Distutils' own
\file{setup.cfg
}:
928 packager = Greg Ward <gward@python.net>
929 doc_files = CHANGES.txt
936 Note that the
\option{doc
\_files} option is simply a
937 whitespace-separated string split across multiple lines for readability.
941 \seetitle[../inst/config-syntax.html
]{Installing Python
942 Modules
}{More information on the configuration files is
943 available in the manual for system administrators.
}
947 \section{Creating a Source Distribution
}
950 As shown in section~
\ref{simple-example
}, you use the
951 \command{sdist
} command to create a source distribution. In the
955 python setup.py sdist
958 (assuming you haven't specified any
\command{sdist
} options in the setup
959 script or config file),
\command{sdist
} creates the archive of the
960 default format for the current platform. The default format is a gzip'ed
961 tar file (
\file{.tar.gz
}) on
\UNIX, and ZIP file on Windows.
962 \XXX{no MacOS support here
}
964 You can specify as many formats as you like using the
965 \longprogramopt{formats
} option, for example:
968 python setup.py sdist --formats=gztar,zip
971 to create a gzipped tarball and a zip file. The available formats are:
972 \begin{tableiii
}{l|l|c
}{code
}%
973 {Format
}{Description
}{Notes
}
974 \lineiii{zip
}{zip file (
\file{.zip
})
}{(
1),(
3)
}
975 \lineiii{gztar
}{gzip'ed tar file (
\file{.tar.gz
})
}{(
2),(
4)
}
976 \lineiii{bztar
}{bzip2'ed tar file (
\file{.tar.bz2
})
}{(
4)
}
977 \lineiii{ztar
}{compressed tar file (
\file{.tar.Z
})
}{(
4)
}
978 \lineiii{tar
}{tar file (
\file{.tar
})
}{(
4)
}
983 \item[(
1)
] default on Windows
984 \item[(
2)
] default on
\UNIX
985 \item[(
3)
] requires either external
\program{zip
} utility or
986 \module{zipfile
} module (part of the standard Python library since
988 \item[(
4)
] requires external utilities:
\program{tar
} and possibly one
989 of
\program{gzip
},
\program{bzip2
}, or
\program{compress
}
994 \subsection{Specifying the files to distribute
}
997 If you don't supply an explicit list of files (or instructions on how to
998 generate one), the
\command{sdist
} command puts a minimal default set
999 into the source distribution:
1001 \item all Python source files implied by the
\option{py
\_modules} and
1002 \option{packages
} options
1003 \item all C source files mentioned in the
\option{ext
\_modules} or
1004 \option{libraries
} options (
\XXX{getting C library sources currently
1005 broken -- no get
\_source\_files() method in build
\_clib.py!
})
1006 \item scripts identified by the
\option{scripts
} option
1007 \item anything that looks like a test script:
\file{test/test*.py
}
1008 (currently, the Distutils don't do anything with test scripts except
1009 include them in source distributions, but in the future there will be
1010 a standard for testing Python module distributions)
1011 \item \file{README.txt
} (or
\file{README
}),
\file{setup.py
} (or whatever
1012 you called your setup script), and
\file{setup.cfg
}
1015 Sometimes this is enough, but usually you will want to specify
1016 additional files to distribute. The typical way to do this is to write
1017 a
\emph{manifest template
}, called
\file{MANIFEST.in
} by default. The
1018 manifest template is just a list of instructions for how to generate
1019 your manifest file,
\file{MANIFEST
}, which is the exact list of files to
1020 include in your source distribution. The
\command{sdist
} command
1021 processes this template and generates a manifest based on its
1022 instructions and what it finds in the filesystem.
1024 If you prefer to roll your own manifest file, the format is simple: one
1025 filename per line, regular files (or symlinks to them) only. If you do
1026 supply your own
\file{MANIFEST
}, you must specify everything: the
1027 default set of files described above does not apply in this case.
1029 The manifest template has one command per line, where each command
1030 specifies a set of files to include or exclude from the source
1031 distribution. For an example, again we turn to the Distutils' own
1036 recursive-include examples *.txt *.py
1037 prune examples/sample?/build
1040 The meanings should be fairly clear: include all files in the
1041 distribution root matching
\code{*.txt
}, all files anywhere under the
1042 \file{examples
} directory matching
\code{*.txt
} or
\code{*.py
}, and
1043 exclude all directories matching
\code{examples/sample?/build
}. All of
1044 this is done
\emph{after
} the standard include set, so you can exclude
1045 files from the standard set with explicit instructions in the manifest
1046 template. (Or, you can use the
\longprogramopt{no-defaults
} option to
1047 disable the standard set entirely.) There are several other commands
1048 available in the manifest template mini-language; see
1049 section~
\ref{sdist-cmd
}.
1051 The order of commands in the manifest template matters: initially, we
1052 have the list of default files as described above, and each command in
1053 the template adds to or removes from that list of files. Once we have
1054 fully processed the manifest template, we remove files that should not
1055 be included in the source distribution:
1057 \item all files in the Distutils ``build'' tree (default
\file{build/
})
1058 \item all files in directories named
\file{RCS
} or
\file{CVS
}
1060 Now we have our complete list of files, which is written to the manifest
1061 for future reference, and then used to build the source distribution
1064 You can disable the default set of included files with the
1065 \longprogramopt{no-defaults
} option, and you can disable the standard
1066 exclude set with
\longprogramopt{no-prune
}.
1068 Following the Distutils' own manifest template, let's trace how the
1069 \command{sdist
} command builds the list of files to include in the
1070 Distutils source distribution:
1072 \item include all Python source files in the
\file{distutils
} and
1073 \file{distutils/command
} subdirectories (because packages
1074 corresponding to those two directories were mentioned in the
1075 \option{packages
} option in the setup script---see
1076 section~
\ref{setup-script
})
1077 \item include
\file{README.txt
},
\file{setup.py
}, and
\file{setup.cfg
}
1079 \item include
\file{test/test*.py
} (standard files)
1080 \item include
\file{*.txt
} in the distribution root (this will find
1081 \file{README.txt
} a second time, but such redundancies are weeded out
1083 \item include anything matching
\file{*.txt
} or
\file{*.py
} in the
1084 sub-tree under
\file{examples
},
1085 \item exclude all files in the sub-trees starting at directories
1086 matching
\file{examples/sample?/build
}---this may exclude files
1087 included by the previous two steps, so it's important that the
1088 \code{prune
} command in the manifest template comes after the
1089 \code{recursive-include
} command
1090 \item exclude the entire
\file{build
} tree, and any
\file{RCS
} or
1091 \file{CVS
} directories
1093 Just like in the setup script, file and directory names in the manifest
1094 template should always be slash-separated; the Distutils will take care
1095 of converting them to the standard representation on your platform.
1096 That way, the manifest template is portable across operating systems.
1099 \subsection{Manifest-related options
}
1100 \label{manifest-options
}
1102 The normal course of operations for the
\command{sdist
} command is as
1105 \item if the manifest file,
\file{MANIFEST
} doesn't exist, read
1106 \file{MANIFEST.in
} and create the manifest
1107 \item if neither
\file{MANIFEST
} nor
\file{MANIFEST.in
} exist, create a
1108 manifest with just the default file set
1109 \item if either
\file{MANIFEST.in
} or the setup script (
\file{setup.py
})
1110 are more recent than
\file{MANIFEST
}, recreate
\file{MANIFEST
} by
1111 reading
\file{MANIFEST.in
}
1112 \item use the list of files now in
\file{MANIFEST
} (either just
1113 generated or read in) to create the source distribution archive(s)
1115 There are a couple of options that modify this behaviour. First, use
1116 the
\longprogramopt{no-defaults
} and
\longprogramopt{no-prune
} to
1117 disable the standard ``include'' and ``exclude'' sets.
1119 Second, you might want to force the manifest to be regenerated---for
1120 example, if you have added or removed files or directories that match an
1121 existing pattern in the manifest template, you should regenerate the
1125 python setup.py sdist --force-manifest
1128 Or, you might just want to (re)generate the manifest, but not create a
1129 source distribution:
1132 python setup.py sdist --manifest-only
1135 \longprogramopt{manifest-only
} implies
\longprogramopt{force-manifest
}.
1136 \programopt{-o
} is a shortcut for
\longprogramopt{manifest-only
}, and
1137 \programopt{-f
} for
\longprogramopt{force-manifest
}.
1140 \section{Creating Built Distributions
}
1143 A ``built distribution'' is what you're probably used to thinking of
1144 either as a ``binary package'' or an ``installer'' (depending on your
1145 background). It's not necessarily binary, though, because it might
1146 contain only Python source code and/or byte-code; and we don't call it a
1147 package, because that word is already spoken for in Python. (And
1148 ``installer'' is a term specific to the Windows world.
\XXX{do Mac
1151 A built distribution is how you make life as easy as possible for
1152 installers of your module distribution: for users of RPM-based Linux
1153 systems, it's a binary RPM; for Windows users, it's an executable
1154 installer; for Debian-based Linux users, it's a Debian package; and so
1155 forth. Obviously, no one person will be able to create built
1156 distributions for every platform under the sun, so the Distutils are
1157 designed to enable module developers to concentrate on their
1158 specialty---writing code and creating source distributions---while an
1159 intermediary species called
\emph{packagers
} springs up to turn source
1160 distributions into built distributions for as many platforms as there
1163 Of course, the module developer could be his own packager; or the
1164 packager could be a volunteer ``out there'' somewhere who has access to
1165 a platform which the original developer does not; or it could be
1166 software periodically grabbing new source distributions and turning them
1167 into built distributions for as many platforms as the software has
1168 access to. Regardless of who they are, a packager uses the
1169 setup script and the
\command{bdist
} command family to generate built
1172 As a simple example, if I run the following command in the Distutils
1176 python setup.py bdist
1179 then the Distutils builds my module distribution (the Distutils itself
1180 in this case), does a ``fake'' installation (also in the
\file{build
}
1181 directory), and creates the default type of built distribution for my
1182 platform. The default format for built distributions is a ``dumb'' tar
1183 file on
\UNIX, and a simple executable installer on Windows. (That tar
1184 file is considered ``dumb'' because it has to be unpacked in a specific
1187 Thus, the above command on a
\UNIX{} system creates
1188 \file{Distutils-
1.0.
\filevar{plat
}.tar.gz
}; unpacking this tarball
1189 from the right place installs the Distutils just as though you had
1190 downloaded the source distribution and run
\code{python setup.py
1191 install
}. (The ``right place'' is either the root of the filesystem or
1192 Python's
\filevar{prefix
} directory, depending on the options given to
1193 the
\command{bdist
\_dumb} command; the default is to make dumb
1194 distributions relative to
\filevar{prefix
}.)
1196 Obviously, for pure Python distributions, this isn't any simpler than
1197 just running
\code{python setup.py install
}---but for non-pure
1198 distributions, which include extensions that would need to be
1199 compiled, it can mean the difference between someone being able to use
1200 your extensions or not. And creating ``smart'' built distributions,
1201 such as an RPM package or an executable installer for Windows, is far
1202 more convenient for users even if your distribution doesn't include
1205 The
\command{bdist
} command has a
\longprogramopt{formats
} option,
1206 similar to the
\command{sdist
} command, which you can use to select the
1207 types of built distribution to generate: for example,
1210 python setup.py bdist --format=zip
1213 would, when run on a
\UNIX{} system, create
1214 \file{Distutils-
1.0.
\filevar{plat
}.zip
}---again, this archive would be
1215 unpacked from the root directory to install the Distutils.
1217 The available formats for built distributions are:
1218 \begin{tableiii
}{l|l|c
}{code
}%
1219 {Format
}{Description
}{Notes
}
1220 \lineiii{gztar
}{gzipped tar file (
\file{.tar.gz
})
}{(
1),(
3)
}
1221 \lineiii{ztar
}{compressed tar file (
\file{.tar.Z
})
}{(
3)
}
1222 \lineiii{tar
}{tar file (
\file{.tar
})
}{(
3)
}
1223 \lineiii{zip
}{zip file (
\file{.zip
})
}{(
4)
}
1224 \lineiii{rpm
}{RPM
}{(
5)
}
1225 \lineiii{pkgtool
}{Solaris
\program{pkgtool
}}{}
1226 \lineiii{sdux
}{HP-UX
\program{swinstall
}}{}
1227 \lineiii{rpm
}{RPM
}{(
5)
}
1228 % \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
1229 \lineiii{wininst
}{self-extracting ZIP file for Windows
}{(
2),(
4)
}
1234 \item[(
1)
] default on
\UNIX
1235 \item[(
2)
] default on Windows
\XXX{to-do!
}
1236 \item[(
3)
] requires external utilities:
\program{tar
} and possibly one
1237 of
\program{gzip
},
\program{bzip2
}, or
\program{compress
}
1238 \item[(
4)
] requires either external
\program{zip
} utility or
1239 \module{zipfile
} module (part of the standard Python library since
1241 \item[(
5)
] requires external
\program{rpm
} utility, version
3.0.4 or
1242 better (use
\code{rpm --version
} to find out which version you have)
1245 You don't have to use the
\command{bdist
} command with the
1246 \longprogramopt{formats
} option; you can also use the command that
1247 directly implements the format you're interested in. Some of these
1248 \command{bdist
} ``sub-commands'' actually generate several similar
1249 formats; for instance, the
\command{bdist
\_dumb} command generates all
1250 the ``dumb'' archive formats (
\code{tar
},
\code{ztar
},
\code{gztar
}, and
1251 \code{zip
}), and
\command{bdist
\_rpm} generates both binary and source
1252 RPMs. The
\command{bdist
} sub-commands, and the formats generated by
1254 \begin{tableii
}{l|l
}{command
}%
1256 \lineii{bdist
\_dumb}{tar, ztar, gztar, zip
}
1257 \lineii{bdist
\_rpm}{rpm, srpm
}
1258 \lineii{bdist
\_wininst}{wininst
}
1261 The following sections give details on the individual
\command{bdist
\_*
}
1265 \subsection{Creating dumb built distributions
}
1266 \label{creating-dumb
}
1268 \XXX{Need to
document absolute vs. prefix-relative packages here, but
1269 first I have to implement it!
}
1272 \subsection{Creating RPM packages
}
1273 \label{creating-rpms
}
1275 The RPM format is used by many popular Linux distributions, including
1276 Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1277 RPM-based Linux distributions) is your usual environment, creating RPM
1278 packages for other users of that same distribution is trivial.
1279 Depending on the complexity of your module distribution and differences
1280 between Linux distributions, you may also be able to create RPMs that
1281 work on different RPM-based distributions.
1283 The usual way to create an RPM of your module distribution is to run the
1284 \command{bdist
\_rpm} command:
1287 python setup.py bdist_rpm
1290 or the
\command{bdist
} command with the
\longprogramopt{format
} option:
1293 python setup.py bdist --formats=rpm
1296 The former allows you to specify RPM-specific options; the latter allows
1297 you to easily specify multiple formats in one run. If you need to do
1298 both, you can explicitly specify multiple
\command{bdist
\_*
} commands
1302 python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1303 bdist_wininst --target_version="
2.0"
1306 Creating RPM packages is driven by a
\file{.spec
} file, much as using
1307 the Distutils is driven by the setup script. To make your life easier,
1308 the
\command{bdist
\_rpm} command normally creates a
\file{.spec
} file
1309 based on the information you supply in the setup script, on the command
1310 line, and in any Distutils configuration files. Various options and
1311 sections in the
\file{.spec
} file are derived from options in the setup
1313 \begin{tableii
}{l|l
}{textrm
}%
1314 {RPM
\file{.spec
} file option or section
}{Distutils setup script option
}
1315 \lineii{Name
}{\option{name
}}
1316 \lineii{Summary (in preamble)
}{\option{description
}}
1317 \lineii{Version
}{\option{version
}}
1318 \lineii{Vendor
}{\option{author
} and
\option{author
\_email}, or \\&
1319 \option{maintainer
} and
\option{maintainer
\_email}}
1320 \lineii{Copyright
}{\option{licence
}}
1321 \lineii{Url
}{\option{url
}}
1322 \lineii{\%description (section)
}{\option{long
\_description}}
1325 Additionally, there many options in
\file{.spec
} files that don't have
1326 corresponding options in the setup script. Most of these are handled
1327 through options to the
\command{bdist
\_rpm} command as follows:
1328 \begin{tableiii
}{l|l|l
}{textrm
}%
1329 {RPM
\file{.spec
} file option or section
}%
1330 {\command{bdist
\_rpm} option
}%
1332 \lineiii{Release
}{\option{release
}}{``
1''
}
1333 \lineiii{Group
}{\option{group
}}{``Development/Libraries''
}
1334 \lineiii{Vendor
}{\option{vendor
}}{(see above)
}
1335 \lineiii{Packager
}{\option{packager
}}{(none)
}
1336 \lineiii{Provides
}{\option{provides
}}{(none)
}
1337 \lineiii{Requires
}{\option{requires
}}{(none)
}
1338 \lineiii{Conflicts
}{\option{conflicts
}}{(none)
}
1339 \lineiii{Obsoletes
}{\option{obsoletes
}}{(none)
}
1340 \lineiii{Distribution
}{\option{distribution
\_name}}{(none)
}
1341 \lineiii{BuildRequires
}{\option{build
\_requires}}{(none)
}
1342 \lineiii{Icon
}{\option{icon
}}{(none)
}
1344 Obviously, supplying even a few of these options on the command-line
1345 would be tedious and error-prone, so it's usually best to put them in
1346 the setup configuration file,
\file{setup.cfg
}---see
1347 section~
\ref{setup-config
}. If you distribute or package many Python
1348 module distributions, you might want to put options that apply to all of
1349 them in your personal Distutils configuration file
1350 (
\file{\textasciitilde/.pydistutils.cfg
}).
1352 There are three steps to building a binary RPM package, all of which are
1353 handled automatically by the Distutils:
1355 \item create a
\file{.spec
} file, which describes the package (analogous
1356 to the Distutils setup script; in fact, much of the information in the
1357 setup script winds up in the
\file{.spec
} file)
1358 \item create the source RPM
1359 \item create the ``binary'' RPM (which may or may not contain binary
1360 code, depending on whether your module distribution contains Python
1363 Normally, RPM bundles the last two steps together; when you use the
1364 Distutils, all three steps are typically bundled together.
1366 If you wish, you can separate these three steps. You can use the
1367 \longprogramopt{spec-only
} option to make
\command{bdist
\_rpm} just
1368 create the
\file{.spec
} file and exit; in this case, the
\file{.spec
}
1369 file will be written to the ``distribution directory''---normally
1370 \file{dist/
}, but customizable with the
\longprogramopt{dist-dir
}
1371 option. (Normally, the
\file{.spec
} file winds up deep in the ``build
1372 tree,'' in a temporary directory created by
\command{bdist
\_rpm}.)
1374 \XXX{this isn't implemented yet---is it needed?!
}
1375 You can also specify a custom
\file{.spec
} file with the
1376 \longprogramopt{spec-file
} option; used in conjunction with
1377 \longprogramopt{spec-only
}, this gives you an opportunity to customize
1378 the
\file{.spec
} file manually:
1381 > python setup.py bdist_rpm --spec-only
1382 # ...edit dist/FooBar-
1.0.spec
1383 > python setup.py bdist_rpm --spec-file=dist/FooBar-
1.0.spec
1386 (Although a better way to do this is probably to override the standard
1387 \command{bdist
\_rpm} command with one that writes whatever else you want
1388 to the
\file{.spec
} file.)
1391 \subsection{Creating Windows Installers
}
1392 \label{creating-wininst
}
1394 Executable installers are the natural format for binary distributions
1395 on Windows. They display a nice graphical user interface, display
1396 some information about the module distribution to be installed taken
1397 from the metadata in the setup script, let the user select a few
1398 options, and start or cancel the installation.
1400 Since the metadata is taken from the setup script, creating Windows
1401 installers is usually as easy as running:
1404 python setup.py bdist_wininst
1407 or the
\command{bdist
} command with the
\longprogramopt{formats
} option:
1410 python setup.py bdist --formats=wininst
1413 If you have a pure module distribution (only containing pure Python
1414 modules and packages), the resulting installer will be version
1415 independent and have a name like
\file{foo-
1.0.win32.exe
}. These
1416 installers can even be created on
\UNIX{} or MacOS platforms.
1418 If you have a non-pure distribution, the extensions can only be
1419 created on a Windows platform, and will be Python version dependent.
1420 The installer filename will reflect this and now has the form
1421 \file{foo-
1.0.win32-py2.0.exe
}. You have to create a separate installer
1422 for every Python version you want to support.
1424 The installer will try to compile pure modules into bytecode after
1425 installation on the target system in normal and optimizing mode. If
1426 you don't want this to happen for some reason, you can run the
1427 \command{bdist_wininst
} command with the
1428 \longprogramopt{no-target-compile
} and/or the
1429 \longprogramopt{no-target-optimize
} option.
1431 By default the installer will display the cool ``Python Powered'' logo
1432 when it is run, but you can also supply your own bitmap which must be
1433 a Windows
\file{.bmp
} file with the
\longprogramopt{bitmap
} option.
1435 The installer will also display a large title on the desktop
1436 background window when it is run, which is constructed from the name
1437 of your distribution and the version number. This can be changed to
1438 another text by using the
\longprogramopt{title
} option.
1440 The installer file will be written to the ``distribution directory''
1441 --- normally
\file{dist/
}, but customizable with the
1442 \longprogramopt{dist-dir
} option.
1444 \subsubsection{The Postinstallation script
}
1445 \label{postinstallation-script
}
1447 Starting with Python
2.3, a postinstallation script can be specified
1448 which the
\longprogramopt{install-script
} option. The basename of the
1449 script must be specified, and the script filename must also be listed
1450 in the scripts argument to the setup function.
1452 This script will be run at installation time on the target system
1453 after all the files have been copied, with argv
[1] set to '-install',
1454 and again at uninstallation time before the files are removed with argv
[1]
1457 The installation script runs embedded in the windows installer, every
1458 output (sys.stdout, sys.stderr) is redirected into a buffer and will
1459 be displayed in the GUI after the script has finished.
1461 Some functions especially useful in this context are available in the
1462 installation script.
1465 directory_created(pathname)
1466 file_created(pathname)
1469 These functions should be called when a directory or file is created
1470 by the postinstall script at installation time. It will register the
1471 pathname with the uninstaller, so that it will be removed when the
1472 distribution is uninstalled. To be safe, directories are only removed
1476 get_special_folder_path(csidl_string)
1479 This function can be used to retrieve special folder locations on
1480 Windows like the Start Menu or the Desktop. It returns the full path
1481 to the folder. 'csidl_string' must be one of the following strings:
1486 "CSIDL_COMMON_STARTMENU"
1489 "CSIDL_COMMON_DESKTOPDIRECTORY"
1490 "CSIDL_DESKTOPDIRECTORY"
1492 "CSIDL_COMMON_STARTUP"
1495 "CSIDL_COMMON_PROGRAMS"
1501 If the folder cannot be retrieved, OSError is raised.
1503 Which folders are available depends on the exact Windows version, and probably
1504 also the configuration. For details refer to Microsoft's documentation of the
1505 \code{SHGetSpecialFolderPath
} function.
1508 create_shortcut(target, description, filename
[, arguments
[,
1509 workdir
[, iconpath
[, iconindex
]]]])
1512 This function creates a shortcut.
1513 \var{target
} is the path to the program to be started by the shortcut.
1514 \var{description
} is the description of the sortcut.
1515 \var{filename
} is the title of the shortcut that the user will see.
1516 \var{arguments
} specifies the command line arguments, if any.
1517 \var{workdir
} is the working directory for the program.
1518 \var{iconpath
} is the file containing the icon for the shortcut,
1519 and
\var{iconindex
} is the index of the icon in the file
1520 \var{iconpath
}. Again, for details consult the Microsoft
1521 documentation for the
\code{IShellLink
} interface.
1523 \section{Registering with the Package Index
}
1524 \label{package-index
}
1526 The Python Package Index (PyPI) holds meta-data describing distributions
1527 packaged with distutils. The distutils command
\command{register
} is
1528 used to submit your distribution's meta-data to the index. It is invoked
1532 python setup.py register
1535 Distutils will respond with the following prompt:
1539 We need to know who you are, so please choose either:
1540 1. use your existing login,
1541 2. register as a new user,
1542 3. have the server generate a new password for you (and email it to you), or
1544 Your selection
[default
1]:
1547 \noindent Note: if your username and password are saved locally, you will
1550 If you have not registered with PyPI, then you will need to do so now. You
1551 should choose option
2, and enter your details as required. Soon after
1552 submitting your details, you will receive an email which will be used to
1553 confirm your registration.
1555 Once you are registered, you may choose option
1 from the menu. You will
1556 be prompted for your PyPI username and password, and
\command{register
}
1557 will then submit your meta-data to the index.
1559 You may submit any number of versions of your distribution to the index. If
1560 you alter the meta-data for a particular version, you may submit it again
1561 and the index will be updated.
1563 PyPI holds a record for each (name, version) combination submitted. The
1564 first user to submit information for a given name is designated the Owner
1565 of that name. They may submit changes through the
\command{register
}
1566 command or through the web interface. They may also designate other users
1567 as Owners or Maintainers. Maintainers may edit the package information, but
1568 not designate other Owners or Maintainers.
1570 By default PyPI will list all versions of a given package. To hide certain
1571 versions, the Hidden property should be set to yes. This must be edited
1572 through the web interface.
1579 \subsection{Pure Python distribution (by module)
}
1582 If you're just distributing a couple of modules, especially if they
1583 don't live in a particular package, you can specify them individually
1584 using the
\option{py
\_modules} option in the setup script.
1586 In the simplest case, you'll have two files to worry about: a setup
1587 script and the single module you're distributing,
\file{foo.py
} in this
1594 (In all diagrams in this section,
\verb|<root>| will refer to the
1595 distribution root directory.) A minimal setup script to describe this
1598 from distutils.core import setup
1599 setup(name = "foo", version = "
1.0",
1600 py_modules =
["foo"
])
1602 Note that the name of the distribution is specified independently with
1603 the
\option{name
} option, and there's no rule that says it has to be the
1604 same as the name of the sole module in the distribution (although that's
1605 probably a good convention to follow). However, the distribution name
1606 is used to generate filenames, so you should stick to letters, digits,
1607 underscores, and hyphens.
1609 Since
\option{py
\_modules} is a list, you can of course specify multiple
1610 modules, eg. if you're distributing modules
\module{foo
} and
1611 \module{bar
}, your setup might look like this:
1618 and the setup script might be
1620 from distutils.core import setup
1621 setup(name = "foobar", version = "
1.0",
1622 py_modules =
["foo", "bar"
])
1625 You can put module source files into another directory, but if you have
1626 enough modules to do that, it's probably easier to specify modules by
1627 package rather than listing them individually.
1630 \subsection{Pure Python distribution (by package)
}
1633 If you have more than a couple of modules to distribute, especially if
1634 they are in multiple packages, it's probably easier to specify whole
1635 packages rather than individual modules. This works even if your
1636 modules are not in a package; you can just tell the Distutils to process
1637 modules from the root package, and that works the same as any other
1638 package (except that you don't have to have an
\file{\_\_init\_\_.py
}
1641 The setup script from the last example could also be written as
1643 from distutils.core import setup
1644 setup(name = "foobar", version = "
1.0",
1647 (The empty string stands for the root package.)
1649 If those two files are moved into a subdirectory, but remain in the root
1657 then you would still specify the root package, but you have to tell the
1658 Distutils where source files in the root package live:
1660 from distutils.core import setup
1661 setup(name = "foobar", version = "
1.0",
1662 package_dir =
{"": "src"
},
1666 More typically, though, you will want to distribute multiple modules in
1667 the same package (or in sub-packages). For example, if the
\module{foo
}
1668 and
\module{bar
} modules belong in package
\module{foobar
}, one way to
1669 layout your source tree is
1678 This is in fact the default layout expected by the Distutils, and the
1679 one that requires the least work to describe in your setup script:
1681 from distutils.core import setup
1682 setup(name = "foobar", version = "
1.0",
1683 packages =
["foobar"
])
1686 If you want to put modules in directories not named for their package,
1687 then you need to use the
\option{package
\_dir} option again. For
1688 example, if the
\file{src
} directory holds modules in the
1689 \module{foobar
} package:
1698 an appropriate setup script would be
1700 from distutils.core import setup
1701 setup(name = "foobar", version = "
1.0",
1702 package_dir =
{"foobar" : "src"
},
1703 packages =
["foobar"
])
1706 Or, you might put modules from your main package right in the
1715 in which case your setup script would be
1717 from distutils.core import setup
1718 setup(name = "foobar", version = "
1.0",
1719 package_dir =
{"foobar" : ""
},
1720 packages =
["foobar"
])
1722 (The empty string also stands for the current directory.)
1724 If you have sub-packages, they must be explicitly listed in
1725 \option{packages
}, but any entries in
\option{package
\_dir}
1726 automatically extend to sub-packages. (In other words, the Distutils
1727 does
\emph{not
} scan your source tree, trying to figure out which
1728 directories correspond to Python packages by looking for
1729 \file{\_\_init\_\_.py
} files.) Thus, if the default layout grows a
1742 then the corresponding setup script would be
1744 from distutils.core import setup
1745 setup(name = "foobar", version = "
1.0",
1746 packages =
["foobar", "foobar.subfoo"
])
1748 (Again, the empty string in
\option{package
\_dir} stands for the current
1752 \subsection{Single extension module
}
1755 Extension modules are specified using the
\option{ext
\_modules} option.
1756 \option{package
\_dir} has no effect on where extension source files are
1757 found; it only affects the source for pure Python modules. The simplest
1758 case, a single extension module in a single C source file, is:
1764 If the
\module{foo
} extension belongs in the root package, the setup
1765 script for this could be
1767 from distutils.core import setup
1768 setup(name = "foobar", version = "
1.0",
1769 ext_modules =
[Extension("foo",
["foo.c"
])
])
1772 If the extension actually belongs in a package, say
\module{foopkg
},
1775 With exactly the same source tree layout, this extension can be put in
1776 the
\module{foopkg
} package simply by changing the name of the
1779 from distutils.core import setup
1780 setup(name = "foobar", version = "
1.0",
1781 ext_modules =
[Extension("foopkg.foo",
["foo.c"
])
])
1785 %\subsection{Multiple extension modules}
1786 %\label{multiple-ext}
1789 %\subsection{Putting it all together}
1792 %\section{Extending the Distutils}
1796 %\subsection{Extending existing commands}
1797 %\label{extend-existing}
1800 %\subsection{Writing new commands}
1801 %\label{new-commands}
1803 %\XXX{Would an uninstall command be a good example here?}
1811 %\subsection{Building modules: the \protect\command{build} command family}
1814 %\subsubsection{\protect\command{build}}
1817 %\subsubsection{\protect\command{build\_py}}
1818 %\label{build-py-cmd}
1820 %\subsubsection{\protect\command{build\_ext}}
1821 %\label{build-ext-cmd}
1823 %\subsubsection{\protect\command{build\_clib}}
1824 %\label{build-clib-cmd}
1827 \subsection{Installing modules: the
\protect\command{install
} command family
}
1830 The install command ensures that the build commands have been run and then
1831 runs the subcommands
\command{install
\_lib},
1832 \command{install
\_data} and
1833 \command{install
\_scripts}.
1835 %\subsubsection{\protect\command{install\_lib}}
1836 %\label{install-lib-cmd}
1838 \subsubsection{\protect\command{install
\_data}}
1839 \label{install-data-cmd
}
1840 This command installs all data files provided with the distribution.
1842 \subsubsection{\protect\command{install
\_scripts}}
1843 \label{install-scripts-cmd
}
1844 This command installs all (Python) scripts in the distribution.
1847 %\subsection{Cleaning up: the \protect\command{clean} command}
1851 \subsection{Creating a source distribution: the
1852 \protect\command{sdist
} command
}
1856 \XXX{fragment moved down from above: needs context!
}
1858 The manifest template commands are:
1859 \begin{tableii
}{ll
}{command
}{Command
}{Description
}
1860 \lineii{include
\var{pat1
} \var{pat2
} ...
}
1861 {include all files matching any of the listed patterns
}
1862 \lineii{exclude
\var{pat1
} \var{pat2
} ...
}
1863 {exclude all files matching any of the listed patterns
}
1864 \lineii{recursive-include
\var{dir
} \var{pat1
} \var{pat2
} ...
}
1865 {include all files under
\var{dir
} matching any of the listed patterns
}
1866 \lineii{recursive-exclude
\var{dir
} \var{pat1
} \var{pat2
} ...
}
1867 {exclude all files under
\var{dir
} matching any of the listed patterns
}
1868 \lineii{global-include
\var{pat1
} \var{pat2
} ...
}
1869 {include all files anywhere in the source tree matching\\&
1870 any of the listed patterns
}
1871 \lineii{global-exclude
\var{pat1
} \var{pat2
} ...
}
1872 {exclude all files anywhere in the source tree matching\\&
1873 any of the listed patterns
}
1874 \lineii{prune
\var{dir
}}{exclude all files under
\var{dir
}}
1875 \lineii{graft
\var{dir
}}{include all files under
\var{dir
}}
1877 The patterns here are
\UNIX-style ``glob'' patterns:
\code{*
} matches any
1878 sequence of regular filename characters,
\code{?
} matches any single
1879 regular filename character, and
\code{[\var{range
}]} matches any of the
1880 characters in
\var{range
} (e.g.,
\code{a-z
},
\code{a-zA-Z
},
1881 \code{a-f0-
9\_.
}). The definition of ``regular filename character'' is
1882 platform-specific: on
\UNIX{} it is anything except slash; on Windows
1883 anything except backslash or colon; on MacOS anything except colon.
1885 \XXX{Windows and MacOS support not there yet
}
1888 %\subsection{Creating a built distribution: the
1889 % \protect\command{bdist} command family}
1893 %\subsubsection{\protect\command{bdist}}
1895 %\subsubsection{\protect\command{bdist\_dumb}}
1897 %\subsubsection{\protect\command{bdist\_rpm}}
1899 %\subsubsection{\protect\command{bdist\_wininst}}