7 % Document extension.read_setup_file
8 % Document build_clib command
11 \title{Distributing Python Modules
}
14 \authoraddress{Email:
\email{gward@python.net
}}
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. If you need to
83 insert potentially expensive processing steps into the Distutils
84 chain, see section~
\ref{extending
} on extending the Distutils.
86 If all you want to do is distribute a module called
\module{foo
},
87 contained in a file
\file{foo.py
}, then your setup script can be as
91 from distutils.core import setup
99 \item most information that you supply to the Distutils is supplied as
100 keyword arguments to the
\function{setup()
} function
101 \item those keyword arguments fall into two categories: package
102 metadata (name, version number) and information about what's in the
103 package (a list of pure Python modules, in this case)
104 \item modules are specified by module name, not filename (the same will
105 hold true for packages and extensions)
106 \item it's recommended that you supply a little more metadata, in
107 particular your name, email address and a URL for the project
108 (see section~
\ref{setup-script
} for an example)
111 To create a source distribution for this module, you would create a
112 setup script,
\file{setup.py
}, containing the above code, and run:
115 python setup.py sdist
118 which will create an archive file (e.g., tarball on
\UNIX, ZIP file on
119 Windows) containing your setup script
\file{setup.py
}, and your module
120 \file{foo.py
}. The archive file will be named
\file{foo-
1.0.tar.gz
} (or
121 \file{.zip
}), and will unpack into a directory
\file{foo-
1.0}.
123 If an end-user wishes to install your
\module{foo
} module, all she has
124 to do is download
\file{foo-
1.0.tar.gz
} (or
\file{.zip
}), unpack it,
125 and---from the
\file{foo-
1.0} directory---run
128 python setup.py install
131 which will ultimately copy
\file{foo.py
} to the appropriate directory
132 for third-party modules in their Python installation.
134 This simple example demonstrates some fundamental concepts of the
135 Distutils. First, both developers and installers have the same basic
136 user interface, i.e. the setup script. The difference is which
137 Distutils
\emph{commands
} they use: the
\command{sdist
} command is
138 almost exclusively for module developers, while
\command{install
} is
139 more often for installers (although most developers will want to install
140 their own code occasionally).
142 If you want to make things really easy for your users, you can create
143 one or more built distributions for them. For instance, if you are
144 running on a Windows machine, and want to make things easy for other
145 Windows users, you can create an executable installer (the most
146 appropriate type of built distribution for this platform) with the
147 \command{bdist
\_wininst} command. For example:
150 python setup.py bdist_wininst
153 will create an executable installer,
\file{foo-
1.0.win32.exe
}, in the
156 Other useful built distribution formats are RPM, implemented by the
157 \command{bdist
\_rpm} command, Solaris
\program{pkgtool
}
158 (
\command{bdist
\_pkgtool}), and HP-UX
\program{swinstall
}
159 (
\command{bdist_sdux
}). For example, the following command will
160 create an RPM file called
\file{foo-
1.0.noarch.rpm
}:
163 python setup.py bdist_rpm
166 (The
\command{bdist
\_rpm} command uses the
\command{rpm
} executable,
167 therefore this has to be run on an RPM-based system such as Red Hat
168 Linux, SuSE Linux, or Mandrake Linux.)
170 You can find out what distribution formats are available at any time by
174 python setup.py bdist --help-formats
178 \subsection{General Python terminology
}
181 If you're reading this
document, you probably have a good idea of what
182 modules, extensions, and so forth are. Nevertheless, just to be sure
183 that everyone is operating from a common starting point, we offer the
184 following glossary of common Python terms:
186 \item[module
] the basic unit of code reusability in Python: a block of
187 code imported by some other code. Three types of modules concern us
188 here: pure Python modules, extension modules, and packages.
190 \item[pure Python module
] a module written in Python and contained in a
191 single
\file{.py
} file (and possibly associated
\file{.pyc
} and/or
192 \file{.pyo
} files). Sometimes referred to as a ``pure module.''
194 \item[extension module
] a module written in the low-level language of
195 the Python implementation: C/C++ for Python, Java for Jython.
196 Typically contained in a single dynamically loadable pre-compiled
197 file, e.g. a shared object (
\file{.so
}) file for Python extensions on
198 \UNIX, a DLL (given the
\file{.pyd
} extension) for Python extensions
199 on Windows, or a Java class file for Jython extensions. (Note that
200 currently, the Distutils only handles C/C++ extensions for Python.)
202 \item[package
] a module that contains other modules; typically contained
203 in a directory in the filesystem and distinguished from other
204 directories by the presence of a file
\file{\_\_init\_\_.py
}.
206 \item[root package
] the root of the hierarchy of packages. (This isn't
207 really a package, since it doesn't have an
\file{\_\_init\_\_.py
}
208 file. But we have to call it something.) The vast majority of the
209 standard library is in the root package, as are many small, standalone
210 third-party modules that don't belong to a larger module collection.
211 Unlike regular packages, modules in the root package can be found in
212 many directories: in fact, every directory listed in
\code{sys.path
}
213 contributes modules to the root package.
217 \subsection{Distutils-specific terminology
}
218 \label{distutils-term
}
220 The following terms apply more specifically to the domain of
221 distributing Python modules using the Distutils:
223 \item[module distribution
] a collection of Python modules distributed
224 together as a single downloadable resource and meant to be installed
225 \emph{en masse
}. Examples of some well-known module distributions are
226 Numeric Python, PyXML, PIL (the Python Imaging Library), or
227 mxBase. (This would be called a
\emph{package
}, except that term
228 is already taken in the Python context: a single module distribution
229 may contain zero, one, or many Python packages.)
231 \item[pure module distribution
] a module distribution that contains only
232 pure Python modules and packages. Sometimes referred to as a ``pure
235 \item[non-pure module distribution
] a module distribution that contains
236 at least one extension module. Sometimes referred to as a ``non-pure
239 \item[distribution root
] the top-level directory of your source tree (or
240 source distribution); the directory where
\file{setup.py
} exists. Generally
241 \file{setup.py
} will be run from this directory.
245 \section{Writing the Setup Script
}
248 The setup script is the centre of all activity in building,
249 distributing, and installing modules using the Distutils. The main
250 purpose of the setup script is to describe your module distribution to
251 the Distutils, so that the various commands that operate on your modules
252 do the right thing. As we saw in section~
\ref{simple-example
} above,
253 the setup script consists mainly of a call to
\function{setup()
}, and
254 most information supplied to the Distutils by the module developer is
255 supplied as keyword arguments to
\function{setup()
}.
257 Here's a slightly more involved example, which we'll follow for the next
258 couple of sections: the Distutils' own setup script. (Keep in mind that
259 although the Distutils are included with Python
1.6 and later, they also
260 have an independent existence so that Python
1.5.2 users can use them to
261 install other module distributions. The Distutils' own setup script,
262 shown here, is used to install the package into Python
1.5.2.)
265 #!/usr/bin/env python
267 from distutils.core import setup
269 setup(name="Distutils",
271 description="Python Distribution Utilities",
273 author_email="gward@python.net",
274 url="http://www.python.org/sigs/distutils-sig/",
275 packages=
['distutils', 'distutils.command'
],
279 There are only two differences between this and the trivial one-file
280 distribution presented in section~
\ref{simple-example
}: more
281 metadata, and the specification of pure Python modules by package,
282 rather than by module. This is important since the Distutils consist of
283 a couple of dozen modules split into (so far) two packages; an explicit
284 list of every module would be tedious to generate and difficult to
287 Note that any pathnames (files or directories) supplied in the setup
288 script should be written using the
\UNIX{} convention, i.e.
289 slash-separated. The Distutils will take care of converting this
290 platform-neutral representation into whatever is appropriate on your
291 current platform before actually using the pathname. This makes your
292 setup script portable across operating systems, which of course is one
293 of the major goals of the Distutils. In this spirit, all pathnames in
294 this
document are slash-separated. (MacOS programmers should keep in
295 mind that the
\emph{absence
} of a leading slash indicates a relative
296 path, the opposite of the MacOS convention with colons.)
298 This, of course, only applies to pathnames given to Distutils
299 functions. If you, for example, use standard python functions such as
300 \function{glob.glob
} or
\function{os.listdir
} to specify files, you
301 should be careful to write portable code instead of hardcoding path
305 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
306 os.listdir(os.path.join('mydir', 'subdir'))
310 \subsection{Listing whole packages
}
311 \label{listing-packages
}
313 The
\option{packages
} option tells the Distutils to process (build,
314 distribute, install, etc.) all pure Python modules found in each package
315 mentioned in the
\option{packages
} list. In order to do this, of
316 course, there has to be a correspondence between package names and
317 directories in the filesystem. The default correspondence is the most
318 obvious one, i.e. package
\module{distutils
} is found in the directory
319 \file{distutils
} relative to the distribution root. Thus, when you say
320 \code{packages =
['foo'
]} in your setup script, you are promising that
321 the Distutils will find a file
\file{foo/
\_\_init\_\_.py
} (which might
322 be spelled differently on your system, but you get the idea) relative to
323 the directory where your setup script lives. If you break this
324 promise, the Distutils will issue a warning but still process the broken
327 If you use a different convention to lay out your source directory,
328 that's no problem: you just have to supply the
\option{package
\_dir}
329 option to tell the Distutils about your convention. For example, say
330 you keep all Python source under
\file{lib
}, so that modules in the
331 ``root package'' (i.e., not in any package at all) are in
332 \file{lib
}, modules in the
\module{foo
} package are in
\file{lib/foo
},
333 and so forth. Then you would put
336 package_dir =
{'': 'lib'
}
339 in your setup script. The keys to this dictionary are package names,
340 and an empty package name stands for the root package. The values are
341 directory names relative to your distribution root. In this case, when
342 you say
\code{packages =
['foo'
]}, you are promising that the file
343 \file{lib/foo/
\_\_init\_\_.py
} exists.
345 Another possible convention is to put the
\module{foo
} package right in
346 \file{lib
}, the
\module{foo.bar
} package in
\file{lib/bar
}, etc. This
347 would be written in the setup script as
350 package_dir =
{'foo': 'lib'
}
353 A
\code{\var{package
}:
\var{dir
}} entry in the
\option{package
\_dir}
354 dictionary implicitly applies to all packages below
\var{package
}, so
355 the
\module{foo.bar
} case is automatically handled here. In this
356 example, having
\code{packages =
['foo', 'foo.bar'
]} tells the Distutils
357 to look for
\file{lib/
\_\_init\_\_.py
} and
358 \file{lib/bar/
\_\_init\_\_.py
}. (Keep in mind that although
359 \option{package
\_dir} applies recursively, you must explicitly list all
360 packages in
\option{packages
}: the Distutils will
\emph{not
} recursively
361 scan your source tree looking for any directory with an
362 \file{\_\_init\_\_.py
} file.)
365 \subsection{Listing individual modules
}
366 \label{listing-modules
}
368 For a small module distribution, you might prefer to list all modules
369 rather than listing packages---especially the case of a single module
370 that goes in the ``root package'' (i.e., no package at all). This
371 simplest case was shown in section~
\ref{simple-example
}; here is a
372 slightly more involved example:
375 py_modules =
['mod1', 'pkg.mod2'
]
378 This describes two modules, one of them in the ``root'' package, the
379 other in the
\module{pkg
} package. Again, the default package/directory
380 layout implies that these two modules can be found in
\file{mod1.py
} and
381 \file{pkg/mod2.py
}, and that
\file{pkg/
\_\_init\_\_.py
} exists as well.
382 And again, you can override the package/directory correspondence using
383 the
\option{package
\_dir} option.
386 \subsection{Describing extension modules
}
387 \label{describing-extensions
}
389 % XXX read over this section
390 Just as writing Python extension modules is a bit more complicated than
391 writing pure Python modules, describing them to the Distutils is a bit
392 more complicated. Unlike pure modules, it's not enough just to list
393 modules or packages and expect the Distutils to go out and find the
394 right files; you have to specify the extension name, source file(s), and
395 any compile/link requirements (include directories, libraries to link
398 All of this is done through another keyword argument to
399 \function{setup()
}, the
\option{extensions
} option.
\option{extensions
}
400 is just a list of
\class{Extension
} instances, each of which describes a
401 single extension module. Suppose your distribution includes a single
402 extension, called
\module{foo
} and implemented by
\file{foo.c
}. If no
403 additional instructions to the compiler/linker are needed, describing
404 this extension is quite simple:
407 uExtension("foo",
["foo.c"
])
410 The
\class{Extension
} class can be imported from
411 \module{distutils.core
} along with
\function{setup()
}. Thus, the setup
412 script for a module distribution that contains only this one extension
413 and nothing else might be:
416 from distutils.core import setup, Extension
417 setup(name="foo", version="
1.0",
418 ext_modules=
[Extension("foo",
["foo.c"
])
])
421 The
\class{Extension
} class (actually, the underlying extension-building
422 machinery implemented by the
\command{build
\_ext} command) supports a
423 great deal of flexibility in describing Python extensions, which is
424 explained in the following sections.
427 \subsubsection{Extension names and packages
}
429 The first argument to the
\class{Extension
} constructor is always the
430 name of the extension, including any package names. For example,
433 Extension("foo",
["src/foo1.c", "src/foo2.c"
])
436 describes an extension that lives in the root package, while
439 Extension("pkg.foo",
["src/foo1.c", "src/foo2.c"
])
442 describes the same extension in the
\module{pkg
} package. The source
443 files and resulting object code are identical in both cases; the only
444 difference is where in the filesystem (and therefore where in Python's
445 namespace hierarchy) the resulting extension lives.
447 If you have a number of extensions all in the same package (or all under
448 the same base package), use the
\option{ext
\_package} keyword argument
449 to
\function{setup()
}. For example,
454 ext_modules=
[Extension("foo",
["foo.c"
]),
455 Extension("subpkg.bar",
["bar.c"
])
]
459 will compile
\file{foo.c
} to the extension
\module{pkg.foo
}, and
460 \file{bar.c
} to
\module{pkg.subpkg.bar
}.
463 \subsubsection{Extension source files
}
465 The second argument to the
\class{Extension
} constructor is a list of
466 source files. Since the Distutils currently only support C,
\Cpp, and
467 Objective-C extensions, these are normally C/
\Cpp/Objective-C source
468 files. (Be sure to use appropriate extensions to distinguish
\Cpp\
469 source files:
\file{.cc
} and
\file{.cpp
} seem to be recognized by both
470 \UNIX{} and Windows compilers.)
472 However, you can also include SWIG interface (
\file{.i
}) files in the
473 list; the
\command{build
\_ext} command knows how to deal with SWIG
474 extensions: it will run SWIG on the interface file and compile the
475 resulting C/C++ file into your extension.
477 \XXX{SWIG support is rough around the edges and largely untested;
478 especially SWIG support of C++ extensions! Explain in more detail
479 here when the interface firms up.
}
481 On some platforms, you can include non-source files that are processed
482 by the compiler and included in your extension. Currently, this just
483 means Windows message text (
\file{.mc
}) files and resource definition
484 (
\file{.rc
}) files for Visual C++. These will be compiled to binary resource
485 (
\file{.res
}) files and linked into the executable.
488 \subsubsection{Preprocessor options
}
490 Three optional arguments to
\class{Extension
} will help if you need to
491 specify include directories to search or preprocessor macros to
492 define/undefine:
\code{include
\_dirs},
\code{define
\_macros}, and
493 \code{undef
\_macros}.
495 For example, if your extension requires header files in the
496 \file{include
} directory under your distribution root, use the
497 \code{include
\_dirs} option:
500 Extension("foo",
["foo.c"
], include_dirs=
["include"
])
503 You can specify absolute directories there; if you know that your
504 extension will only be built on
\UNIX{} systems with X11R6 installed to
505 \file{/usr
}, you can get away with
508 Extension("foo",
["foo.c"
], include_dirs=
["/usr/include/X11"
])
511 You should avoid this sort of non-portable usage if you plan to
512 distribute your code: it's probably better to write C code like
514 #include <X11/Xlib.h>
517 If you need to include header files from some other Python extension,
518 you can take advantage of the fact that header files are installed in a
519 consistent way by the Distutils
\command{install
\_header} command. For
520 example, the Numerical Python header files are installed (on a standard
521 Unix installation) to
\file{/usr/local/include/python1.5/Numerical
}.
522 (The exact location will differ according to your platform and Python
523 installation.) Since the Python include
524 directory---
\file{/usr/local/include/python1.5
} in this case---is always
525 included in the search path when building Python extensions, the best
526 approach is to write C code like
528 #include <Numerical/arrayobject.h>
530 If you must put the
\file{Numerical
} include directory right into your
531 header search path, though, you can find that directory using the
532 Distutils
\module{sysconfig
} module:
535 from distutils.sysconfig import get_python_inc
536 incdir = os.path.join(get_python_inc(plat_specific=
1), "Numerical")
538 Extension(..., include_dirs=
[incdir
]))
541 Even though this is quite portable---it will work on any Python
542 installation, regardless of platform---it's probably easier to just
543 write your C code in the sensible way.
545 You can define and undefine pre-processor macros with the
546 \code{define
\_macros} and
\code{undef
\_macros} options.
547 \code{define
\_macros} takes a list of
\code{(name, value)
} tuples, where
548 \code{name
} is the name of the macro to define (a string) and
549 \code{value
} is its value: either a string or
\code{None
}. (Defining a
550 macro
\code{FOO
} to
\code{None
} is the equivalent of a bare
551 \code{\#define FOO
} in your C source: with most compilers, this sets
552 \code{FOO
} to the string
\code{1}.)
\code{undef
\_macros} is just
553 a list of macros to undefine.
559 define_macros=
[('NDEBUG', '
1')
],
560 ('HAVE_STRFTIME', None),
561 undef_macros=
['HAVE_FOO', 'HAVE_BAR'
])
564 is the equivalent of having this at the top of every C source file:
568 #define HAVE_STRFTIME
574 \subsubsection{Library options
}
576 You can also specify the libraries to link against when building your
577 extension, and the directories to search for those libraries. The
578 \code{libraries
} option is a list of libraries to link against,
579 \code{library
\_dirs} is a list of directories to search for libraries at
580 link-time, and
\code{runtime
\_library\_dirs} is a list of directories to
581 search for shared (dynamically loaded) libraries at run-time.
583 For example, if you need to link against libraries known to be in the
584 standard library search path on target systems
588 libraries=
["gdbm", "readline"
])
591 If you need to link with libraries in a non-standard location, you'll
592 have to include the location in
\code{library
\_dirs}:
596 library_dirs=
["/usr/X11R6/lib"
],
597 libraries=
["X11", "Xt"
])
600 (Again, this sort of non-portable construct should be avoided if you
601 intend to distribute your code.)
603 \XXX{Should mention clib libraries here or somewhere else!
}
605 \subsubsection{Other options
}
607 There are still some other options which can be used to handle special
610 The
\option{extra
\_objects} option is a list of object files to be passed
611 to the linker. These files must not have extensions, as the default
612 extension for the compiler is used.
614 \option{extra
\_compile\_args} and
\option{extra
\_link\_args} can be used
615 to specify additional command line options for the respective compiler and
616 linker command lines.
618 \option{export
\_symbols} is only useful on Windows. It can contain a list
619 of symbols (functions or variables) to be exported. This option
620 is not needed when building compiled extensions: Distutils
621 will automatically add
\code{initmodule
}
622 to the list of exported symbols.
624 \subsection{Installing Scripts
}
625 So far we have been dealing with pure and non-pure Python modules,
626 which are usually not run by themselves but imported by scripts.
628 Scripts are files containing Python source code, intended to be
629 started from the command line. Scripts don't require Distutils to do
630 anything very complicated. The only clever feature is that if the
631 first line of the script starts with
\code{\#!
} and contains the word
632 ``python'', the Distutils will adjust the first line to refer to the
633 current interpreter location.
635 The
\option{scripts
} option simply is a list of files to be handled
636 in this way. From the PyXML setup script:
640 scripts =
['scripts/xmlproc_parse', 'scripts/xmlproc_val'
]
645 \subsection{Installing Additional Files
}
647 The
\option{data
\_files} option can be used to specify additional
648 files needed by the module distribution: configuration files, message
649 catalogs, data files, anything which doesn't fit in the previous
652 \option{data
\_files} specifies a sequence of (
\var{directory
},
653 \var{files
}) pairs in the following way:
657 data_files=
[('bitmaps',
['bm/b1.gif', 'bm/b2.gif'
]),
658 ('config',
['cfg/data.cfg'
]),
659 ('/etc/init.d',
['init-script'
])
]
663 Note that you can specify the directory names where the data files
664 will be installed, but you cannot rename the data files themselves.
666 Each (
\var{directory
},
\var{files
}) pair in the sequence specifies the
667 installation directory and the files to install there. If
668 \var{directory
} is a relative path, it is interpreted relative to the
669 installation prefix (Python's
\code{sys.prefix
} for pure-Python
670 packages,
\code{sys.exec_prefix
} for packages that contain extension
671 modules). Each file name in
\var{files
} is interpreted relative to
672 the
\file{setup.py
} script at the top of the package source
673 distribution. No directory information from
\var{files
} is used to
674 determine the final location of the installed file; only the name of
677 You can specify the
\option{data
\_files} options as a simple sequence
678 of files without specifying a target directory, but this is not recommended,
679 and the
\command{install
} command will print a warning in this case.
680 To install data files directly in the target directory, an empty
681 string should be given as the directory.
684 \section{Writing the Setup Configuration File
}
687 Often, it's not possible to write down everything needed to build a
688 distribution
\emph{a priori
}: you may need to get some information from
689 the user, or from the user's system, in order to proceed. As long as
690 that information is fairly simple---a list of directories to search for
691 C header files or libraries, for example---then providing a
692 configuration file,
\file{setup.cfg
}, for users to edit is a cheap and
693 easy way to solicit it. Configuration files also let you provide
694 default values for any command option, which the installer can then
695 override either on the command-line or by editing the config file.
697 % (If you have more advanced needs, such as determining which extensions
698 % to build based on what capabilities are present on the target system,
699 % then you need the Distutils ``auto-configuration'' facility. This
700 % started to appear in Distutils 0.9 but, as of this writing, isn't mature
701 % or stable enough yet for real-world use.)
703 The setup configuration file is a useful middle-ground between the setup
704 script---which, ideally, would be opaque to installers
\footnote{This
705 ideal probably won't be achieved until auto-configuration is fully
706 supported by the Distutils.
}---and the command-line to the setup
707 script, which is outside of your control and entirely up to the
708 installer. In fact,
\file{setup.cfg
} (and any other Distutils
709 configuration files present on the target system) are processed after
710 the contents of the setup script, but before the command-line. This has
711 several useful consequences:
713 \item installers can override some of what you put in
\file{setup.py
} by
714 editing
\file{setup.cfg
}
715 \item you can provide non-standard defaults for options that are not
716 easily set in
\file{setup.py
}
717 \item installers can override anything in
\file{setup.cfg
} using the
718 command-line options to
\file{setup.py
}
721 The basic syntax of the configuration file is simple:
729 where
\var{command
} is one of the Distutils commands (e.g.
730 \command{build
\_py},
\command{install
}), and
\var{option
} is one of
731 the options that command supports. Any number of options can be
732 supplied for each command, and any number of command sections can be
733 included in the file. Blank lines are ignored, as are comments, which
734 run from a
\character{\#
} character until the end of the line. Long
735 option values can be split across multiple lines simply by indenting
736 the continuation lines.
738 You can find out the list of options supported by a particular command
739 with the universal
\longprogramopt{help
} option, e.g.
742 > python setup.py --help build_ext
744 Options for 'build_ext' command:
745 --build-lib (-b) directory for compiled extension modules
746 --build-temp (-t) directory for temporary files (build by-products)
747 --inplace (-i) ignore build-lib and put compiled extensions into the
748 source directory alongside your pure Python modules
749 --include-dirs (-I) list of directories to search for header files
750 --define (-D) C preprocessor macros to define
751 --undef (-U) C preprocessor macros to undefine
755 Note that an option spelled
\longprogramopt{foo-bar
} on the command-line
756 is spelled
\option{foo
\_bar} in configuration files.
758 For example, say you want your extensions to be built
759 ``in-place''---that is, you have an extension
\module{pkg.ext
}, and you
760 want the compiled extension file (
\file{ext.so
} on
\UNIX, say) to be put
761 in the same source directory as your pure Python modules
762 \module{pkg.mod1
} and
\module{pkg.mod2
}. You can always use the
763 \longprogramopt{inplace
} option on the command-line to ensure this:
766 python setup.py build_ext --inplace
769 But this requires that you always specify the
\command{build
\_ext}
770 command explicitly, and remember to provide
\longprogramopt{inplace
}.
771 An easier way is to ``set and forget'' this option, by encoding it in
772 \file{setup.cfg
}, the configuration file for this distribution:
779 This will affect all builds of this module distribution, whether or not
780 you explcitly specify
\command{build
\_ext}. If you include
781 \file{setup.cfg
} in your source distribution, it will also affect
782 end-user builds---which is probably a bad idea for this option, since
783 always building extensions in-place would break installation of the
784 module distribution. In certain peculiar cases, though, modules are
785 built right in their installation directory, so this is conceivably a
786 useful ability. (Distributing extensions that expect to be built in
787 their installation directory is almost always a bad idea, though.)
789 Another example: certain commands take a lot of options that don't
790 change from run to run; for example,
\command{bdist
\_rpm} needs to know
791 everything required to generate a ``spec'' file for creating an RPM
792 distribution. Some of this information comes from the setup script, and
793 some is automatically generated by the Distutils (such as the list of
794 files installed). But some of it has to be supplied as options to
795 \command{bdist
\_rpm}, which would be very tedious to do on the
796 command-line for every run. Hence, here is a snippet from the
797 Distutils' own
\file{setup.cfg
}:
802 packager = Greg Ward <gward@python.net>
803 doc_files = CHANGES.txt
810 Note that the
\option{doc
\_files} option is simply a
811 whitespace-separated string split across multiple lines for readability.
815 \seetitle[../inst/config-syntax.html
]{Installing Python
816 Modules
}{More information on the configuration files is
817 available in the manual for system administrators.
}
821 \section{Creating a Source Distribution
}
824 As shown in section~
\ref{simple-example
}, you use the
825 \command{sdist
} command to create a source distribution. In the
829 python setup.py sdist
832 (assuming you haven't specified any
\command{sdist
} options in the setup
833 script or config file),
\command{sdist
} creates the archive of the
834 default format for the current platform. The default format is a gzip'ed
835 tar file (
\file{.tar.gz
}) on
\UNIX, and ZIP file on Windows.
836 \XXX{no MacOS support here
}
838 You can specify as many formats as you like using the
839 \longprogramopt{formats
} option, for example:
842 python setup.py sdist --formats=gztar,zip
845 to create a gzipped tarball and a zip file. The available formats are:
846 \begin{tableiii
}{l|l|c
}{code
}%
847 {Format
}{Description
}{Notes
}
848 \lineiii{zip
}{zip file (
\file{.zip
})
}{(
1),(
3)
}
849 \lineiii{gztar
}{gzip'ed tar file (
\file{.tar.gz
})
}{(
2),(
4)
}
850 \lineiii{bztar
}{bzip2'ed tar file (
\file{.tar.bz2
})
}{(
4)
}
851 \lineiii{ztar
}{compressed tar file (
\file{.tar.Z
})
}{(
4)
}
852 \lineiii{tar
}{tar file (
\file{.tar
})
}{(
4)
}
857 \item[(
1)
] default on Windows
858 \item[(
2)
] default on
\UNIX
859 \item[(
3)
] requires either external
\program{zip
} utility or
860 \module{zipfile
} module (part of the standard Python library since
862 \item[(
4)
] requires external utilities:
\program{tar
} and possibly one
863 of
\program{gzip
},
\program{bzip2
}, or
\program{compress
}
868 \subsection{Specifying the files to distribute
}
871 If you don't supply an explicit list of files (or instructions on how to
872 generate one), the
\command{sdist
} command puts a minimal default set
873 into the source distribution:
875 \item all Python source files implied by the
\option{py
\_modules} and
876 \option{packages
} options
877 \item all C source files mentioned in the
\option{ext
\_modules} or
878 \option{libraries
} options (
\XXX{getting C library sources currently
879 broken -- no get
\_source\_files() method in build
\_clib.py!
})
880 \item anything that looks like a test script:
\file{test/test*.py
}
881 (currently, the Distutils don't do anything with test scripts except
882 include them in source distributions, but in the future there will be
883 a standard for testing Python module distributions)
884 \item \file{README.txt
} (or
\file{README
}),
\file{setup.py
} (or whatever
885 you called your setup script), and
\file{setup.cfg
}
888 Sometimes this is enough, but usually you will want to specify
889 additional files to distribute. The typical way to do this is to write
890 a
\emph{manifest template
}, called
\file{MANIFEST.in
} by default. The
891 manifest template is just a list of instructions for how to generate
892 your manifest file,
\file{MANIFEST
}, which is the exact list of files to
893 include in your source distribution. The
\command{sdist
} command
894 processes this template and generates a manifest based on its
895 instructions and what it finds in the filesystem.
897 If you prefer to roll your own manifest file, the format is simple: one
898 filename per line, regular files (or symlinks to them) only. If you do
899 supply your own
\file{MANIFEST
}, you must specify everything: the
900 default set of files described above does not apply in this case.
902 The manifest template has one command per line, where each command
903 specifies a set of files to include or exclude from the source
904 distribution. For an example, again we turn to the Distutils' own
909 recursive-include examples *.txt *.py
910 prune examples/sample?/build
913 The meanings should be fairly clear: include all files in the
914 distribution root matching
\code{*.txt
}, all files anywhere under the
915 \file{examples
} directory matching
\code{*.txt
} or
\code{*.py
}, and
916 exclude all directories matching
\code{examples/sample?/build
}. All of
917 this is done
\emph{after
} the standard include set, so you can exclude
918 files from the standard set with explicit instructions in the manifest
919 template. (Or, you can use the
\longprogramopt{no-defaults
} option to
920 disable the standard set entirely.) There are several other commands
921 available in the manifest template mini-language; see
922 section~
\ref{sdist-cmd
}.
924 The order of commands in the manifest template matters: initially, we
925 have the list of default files as described above, and each command in
926 the template adds to or removes from that list of files. Once we have
927 fully processed the manifest template, we remove files that should not
928 be included in the source distribution:
930 \item all files in the Distutils ``build'' tree (default
\file{build/
})
931 \item all files in directories named
\file{RCS
} or
\file{CVS
}
933 Now we have our complete list of files, which is written to the manifest
934 for future reference, and then used to build the source distribution
937 You can disable the default set of included files with the
938 \longprogramopt{no-defaults
} option, and you can disable the standard
939 exclude set with
\longprogramopt{no-prune
}.
941 Following the Distutils' own manifest template, let's trace how the
942 \command{sdist
} command builds the list of files to include in the
943 Distutils source distribution:
945 \item include all Python source files in the
\file{distutils
} and
946 \file{distutils/command
} subdirectories (because packages
947 corresponding to those two directories were mentioned in the
948 \option{packages
} option in the setup script---see
949 section~
\ref{setup-script
})
950 \item include
\file{README.txt
},
\file{setup.py
}, and
\file{setup.cfg
}
952 \item include
\file{test/test*.py
} (standard files)
953 \item include
\file{*.txt
} in the distribution root (this will find
954 \file{README.txt
} a second time, but such redundancies are weeded out
956 \item include anything matching
\file{*.txt
} or
\file{*.py
} in the
957 sub-tree under
\file{examples
},
958 \item exclude all files in the sub-trees starting at directories
959 matching
\file{examples/sample?/build
}---this may exclude files
960 included by the previous two steps, so it's important that the
961 \code{prune
} command in the manifest template comes after the
962 \code{recursive-include
} command
963 \item exclude the entire
\file{build
} tree, and any
\file{RCS
} or
964 \file{CVS
} directories
966 Just like in the setup script, file and directory names in the manifest
967 template should always be slash-separated; the Distutils will take care
968 of converting them to the standard representation on your platform.
969 That way, the manifest template is portable across operating systems.
972 \subsection{Manifest-related options
}
973 \label{manifest-options
}
975 The normal course of operations for the
\command{sdist
} command is as
978 \item if the manifest file,
\file{MANIFEST
} doesn't exist, read
979 \file{MANIFEST.in
} and create the manifest
980 \item if neither
\file{MANIFEST
} nor
\file{MANIFEST.in
} exist, create a
981 manifest with just the default file set
982 \item if either
\file{MANIFEST.in
} or the setup script (
\file{setup.py
})
983 are more recent than
\file{MANIFEST
}, recreate
\file{MANIFEST
} by
984 reading
\file{MANIFEST.in
}
985 \item use the list of files now in
\file{MANIFEST
} (either just
986 generated or read in) to create the source distribution archive(s)
988 There are a couple of options that modify this behaviour. First, use
989 the
\longprogramopt{no-defaults
} and
\longprogramopt{no-prune
} to
990 disable the standard ``include'' and ``exclude'' sets.
992 Second, you might want to force the manifest to be regenerated---for
993 example, if you have added or removed files or directories that match an
994 existing pattern in the manifest template, you should regenerate the
998 python setup.py sdist --force-manifest
1001 Or, you might just want to (re)generate the manifest, but not create a
1002 source distribution:
1005 python setup.py sdist --manifest-only
1008 \longprogramopt{manifest-only
} implies
\longprogramopt{force-manifest
}.
1009 \programopt{-o
} is a shortcut for
\longprogramopt{manifest-only
}, and
1010 \programopt{-f
} for
\longprogramopt{force-manifest
}.
1013 \section{Creating Built Distributions
}
1016 A ``built distribution'' is what you're probably used to thinking of
1017 either as a ``binary package'' or an ``installer'' (depending on your
1018 background). It's not necessarily binary, though, because it might
1019 contain only Python source code and/or byte-code; and we don't call it a
1020 package, because that word is already spoken for in Python. (And
1021 ``installer'' is a term specific to the Windows world.
\XXX{do Mac
1024 A built distribution is how you make life as easy as possible for
1025 installers of your module distribution: for users of RPM-based Linux
1026 systems, it's a binary RPM; for Windows users, it's an executable
1027 installer; for Debian-based Linux users, it's a Debian package; and so
1028 forth. Obviously, no one person will be able to create built
1029 distributions for every platform under the sun, so the Distutils are
1030 designed to enable module developers to concentrate on their
1031 specialty---writing code and creating source distributions---while an
1032 intermediary species called
\emph{packagers
} springs up to turn source
1033 distributions into built distributions for as many platforms as there
1036 Of course, the module developer could be his own packager; or the
1037 packager could be a volunteer ``out there'' somewhere who has access to
1038 a platform which the original developer does not; or it could be
1039 software periodically grabbing new source distributions and turning them
1040 into built distributions for as many platforms as the software has
1041 access to. Regardless of who they are, a packager uses the
1042 setup script and the
\command{bdist
} command family to generate built
1045 As a simple example, if I run the following command in the Distutils
1049 python setup.py bdist
1052 then the Distutils builds my module distribution (the Distutils itself
1053 in this case), does a ``fake'' installation (also in the
\file{build
}
1054 directory), and creates the default type of built distribution for my
1055 platform. The default format for built distributions is a ``dumb'' tar
1056 file on
\UNIX, and a simple executable installer on Windows. (That tar
1057 file is considered ``dumb'' because it has to be unpacked in a specific
1060 Thus, the above command on a
\UNIX{} system creates
1061 \file{Distutils-
1.0.
\filevar{plat
}.tar.gz
}; unpacking this tarball
1062 from the right place installs the Distutils just as though you had
1063 downloaded the source distribution and run
\code{python setup.py
1064 install
}. (The ``right place'' is either the root of the filesystem or
1065 Python's
\filevar{prefix
} directory, depending on the options given to
1066 the
\command{bdist
\_dumb} command; the default is to make dumb
1067 distributions relative to
\filevar{prefix
}.)
1069 Obviously, for pure Python distributions, this isn't any simpler than
1070 just running
\code{python setup.py install
}---but for non-pure
1071 distributions, which include extensions that would need to be
1072 compiled, it can mean the difference between someone being able to use
1073 your extensions or not. And creating ``smart'' built distributions,
1074 such as an RPM package or an executable installer for Windows, is far
1075 more convenient for users even if your distribution doesn't include
1078 The
\command{bdist
} command has a
\longprogramopt{formats
} option,
1079 similar to the
\command{sdist
} command, which you can use to select the
1080 types of built distribution to generate: for example,
1083 python setup.py bdist --format=zip
1086 would, when run on a
\UNIX{} system, create
1087 \file{Distutils-
1.0.
\filevar{plat
}.zip
}---again, this archive would be
1088 unpacked from the root directory to install the Distutils.
1090 The available formats for built distributions are:
1091 \begin{tableiii
}{l|l|c
}{code
}%
1092 {Format
}{Description
}{Notes
}
1093 \lineiii{gztar
}{gzipped tar file (
\file{.tar.gz
})
}{(
1),(
3)
}
1094 \lineiii{ztar
}{compressed tar file (
\file{.tar.Z
})
}{(
3)
}
1095 \lineiii{tar
}{tar file (
\file{.tar
})
}{(
3)
}
1096 \lineiii{zip
}{zip file (
\file{.zip
})
}{(
4)
}
1097 \lineiii{rpm
}{RPM
}{(
5)
}
1098 \lineiii{pkgtool
}{Solaris
\program{pkgtool
}}{}
1099 \lineiii{sdux
}{HP-UX
\program{swinstall
}}{}
1100 \lineiii{rpm
}{RPM
}{(
5)
}
1101 % \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
1102 \lineiii{wininst
}{self-extracting ZIP file for Windows
}{(
2),(
4)
}
1107 \item[(
1)
] default on
\UNIX
1108 \item[(
2)
] default on Windows
\XXX{to-do!
}
1109 \item[(
3)
] requires external utilities:
\program{tar
} and possibly one
1110 of
\program{gzip
},
\program{bzip2
}, or
\program{compress
}
1111 \item[(
4)
] requires either external
\program{zip
} utility or
1112 \module{zipfile
} module (part of the standard Python library since
1114 \item[(
5)
] requires external
\program{rpm
} utility, version
3.0.4 or
1115 better (use
\code{rpm --version
} to find out which version you have)
1118 You don't have to use the
\command{bdist
} command with the
1119 \longprogramopt{formats
} option; you can also use the command that
1120 directly implements the format you're interested in. Some of these
1121 \command{bdist
} ``sub-commands'' actually generate several similar
1122 formats; for instance, the
\command{bdist
\_dumb} command generates all
1123 the ``dumb'' archive formats (
\code{tar
},
\code{ztar
},
\code{gztar
}, and
1124 \code{zip
}), and
\command{bdist
\_rpm} generates both binary and source
1125 RPMs. The
\command{bdist
} sub-commands, and the formats generated by
1127 \begin{tableii
}{l|l
}{command
}%
1129 \lineii{bdist
\_dumb}{tar, ztar, gztar, zip
}
1130 \lineii{bdist
\_rpm}{rpm, srpm
}
1131 \lineii{bdist
\_wininst}{wininst
}
1134 The following sections give details on the individual
\command{bdist
\_*
}
1138 \subsection{Creating dumb built distributions
}
1139 \label{creating-dumb
}
1141 \XXX{Need to
document absolute vs. prefix-relative packages here, but
1142 first I have to implement it!
}
1145 \subsection{Creating RPM packages
}
1146 \label{creating-rpms
}
1148 The RPM format is used by many popular Linux distributions, including
1149 Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1150 RPM-based Linux distributions) is your usual environment, creating RPM
1151 packages for other users of that same distribution is trivial.
1152 Depending on the complexity of your module distribution and differences
1153 between Linux distributions, you may also be able to create RPMs that
1154 work on different RPM-based distributions.
1156 The usual way to create an RPM of your module distribution is to run the
1157 \command{bdist
\_rpm} command:
1160 python setup.py bdist_rpm
1163 or the
\command{bdist
} command with the
\longprogramopt{format
} option:
1166 python setup.py bdist --formats=rpm
1169 The former allows you to specify RPM-specific options; the latter allows
1170 you to easily specify multiple formats in one run. If you need to do
1171 both, you can explicitly specify multiple
\command{bdist
\_*
} commands
1175 python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1176 bdist_wininst --target_version="
2.0"
1179 Creating RPM packages is driven by a
\file{.spec
} file, much as using
1180 the Distutils is driven by the setup script. To make your life easier,
1181 the
\command{bdist
\_rpm} command normally creates a
\file{.spec
} file
1182 based on the information you supply in the setup script, on the command
1183 line, and in any Distutils configuration files. Various options and
1184 sections in the
\file{.spec
} file are derived from options in the setup
1186 \begin{tableii
}{l|l
}{textrm
}%
1187 {RPM
\file{.spec
} file option or section
}{Distutils setup script option
}
1188 \lineii{Name
}{\option{name
}}
1189 \lineii{Summary (in preamble)
}{\option{description
}}
1190 \lineii{Version
}{\option{version
}}
1191 \lineii{Vendor
}{\option{author
} and
\option{author
\_email}, or \\&
1192 \option{maintainer
} and
\option{maintainer
\_email}}
1193 \lineii{Copyright
}{\option{licence
}}
1194 \lineii{Url
}{\option{url
}}
1195 \lineii{\%description (section)
}{\option{long
\_description}}
1198 Additionally, there many options in
\file{.spec
} files that don't have
1199 corresponding options in the setup script. Most of these are handled
1200 through options to the
\command{bdist
\_rpm} command as follows:
1201 \begin{tableiii
}{l|l|l
}{textrm
}%
1202 {RPM
\file{.spec
} file option or section
}%
1203 {\command{bdist
\_rpm} option
}%
1205 \lineiii{Release
}{\option{release
}}{``
1''
}
1206 \lineiii{Group
}{\option{group
}}{``Development/Libraries''
}
1207 \lineiii{Vendor
}{\option{vendor
}}{(see above)
}
1208 \lineiii{Packager
}{\option{packager
}}{(none)
}
1209 \lineiii{Provides
}{\option{provides
}}{(none)
}
1210 \lineiii{Requires
}{\option{requires
}}{(none)
}
1211 \lineiii{Conflicts
}{\option{conflicts
}}{(none)
}
1212 \lineiii{Obsoletes
}{\option{obsoletes
}}{(none)
}
1213 \lineiii{Distribution
}{\option{distribution
\_name}}{(none)
}
1214 \lineiii{BuildRequires
}{\option{build
\_requires}}{(none)
}
1215 \lineiii{Icon
}{\option{icon
}}{(none)
}
1217 Obviously, supplying even a few of these options on the command-line
1218 would be tedious and error-prone, so it's usually best to put them in
1219 the setup configuration file,
\file{setup.cfg
}---see
1220 section~
\ref{setup-config
}. If you distribute or package many Python
1221 module distributions, you might want to put options that apply to all of
1222 them in your personal Distutils configuration file
1223 (
\file{\textasciitilde/.pydistutils.cfg
}).
1225 There are three steps to building a binary RPM package, all of which are
1226 handled automatically by the Distutils:
1228 \item create a
\file{.spec
} file, which describes the package (analogous
1229 to the Distutils setup script; in fact, much of the information in the
1230 setup script winds up in the
\file{.spec
} file)
1231 \item create the source RPM
1232 \item create the ``binary'' RPM (which may or may not contain binary
1233 code, depending on whether your module distribution contains Python
1236 Normally, RPM bundles the last two steps together; when you use the
1237 Distutils, all three steps are typically bundled together.
1239 If you wish, you can separate these three steps. You can use the
1240 \longprogramopt{spec-only
} option to make
\command{bdist
\_rpm} just
1241 create the
\file{.spec
} file and exit; in this case, the
\file{.spec
}
1242 file will be written to the ``distribution directory''---normally
1243 \file{dist/
}, but customizable with the
\longprogramopt{dist-dir
}
1244 option. (Normally, the
\file{.spec
} file winds up deep in the ``build
1245 tree,'' in a temporary directory created by
\command{bdist
\_rpm}.)
1247 \XXX{this isn't implemented yet---is it needed?!
}
1248 You can also specify a custom
\file{.spec
} file with the
1249 \longprogramopt{spec-file
} option; used in conjunction with
1250 \longprogramopt{spec-only
}, this gives you an opportunity to customize
1251 the
\file{.spec
} file manually:
1254 > python setup.py bdist_rpm --spec-only
1255 # ...edit dist/FooBar-
1.0.spec
1256 > python setup.py bdist_rpm --spec-file=dist/FooBar-
1.0.spec
1259 (Although a better way to do this is probably to override the standard
1260 \command{bdist
\_rpm} command with one that writes whatever else you want
1261 to the
\file{.spec
} file; see section~
\ref{extending
} for information on
1262 extending the Distutils.)
1265 \subsection{Creating Windows Installers
}
1266 \label{creating-wininst
}
1268 Executable installers are the natural format for binary
1269 distributions on Windows. They display a nice graphical user interface,
1270 display some information about the module distribution to be installed taken
1271 from the metadata in the setup script, let the user select a few
1272 (currently maybe too few) options, and start or cancel the installation.
1274 Since the metadata is taken from the setup script, creating
1275 Windows installers is usually as easy as running:
1278 python setup.py bdist_wininst
1281 or the
\command{bdist
} command with the
\longprogramopt{format
} option:
1284 python setup.py bdist --formats=wininst
1287 If you have a pure module distribution (only containing pure
1288 Python modules and packages), the resulting installer will be
1289 version independent and have a name like
\file{foo-
1.0.win32.exe
}.
1290 These installers can even be created on
\UNIX{} or MacOS platforms.
1292 If you have a non-pure distribution, the extensions can only be
1293 created on a Windows platform, and will be Python version dependent.
1294 The installer filename will reflect this and now has the form
1295 \file{foo-
1.0.win32-py2.0.exe
}. You have to create a separate installer
1296 for every Python version you want to support.
1298 The installer will try to compile pure modules into bytecode after
1299 installation on the target system in normal and optimizing mode.
1300 If you don't want this to happen for some reason, you can run
1301 the bdist_wininst command with the
\longprogramopt{no-target-compile
} and/or
1302 the
\longprogramopt{no-target-optimize
} option.
1308 \subsection{Pure Python distribution (by module)
}
1311 If you're just distributing a couple of modules, especially if they
1312 don't live in a particular package, you can specify them individually
1313 using the
\option{py
\_modules} option in the setup script.
1315 In the simplest case, you'll have two files to worry about: a setup
1316 script and the single module you're distributing,
\file{foo.py
} in this
1323 (In all diagrams in this section,
\verb|<root>| will refer to the
1324 distribution root directory.) A minimal setup script to describe this
1327 from distutils.core import setup
1328 setup(name = "foo", version = "
1.0",
1329 py_modules =
["foo"
])
1331 Note that the name of the distribution is specified independently with
1332 the
\option{name
} option, and there's no rule that says it has to be the
1333 same as the name of the sole module in the distribution (although that's
1334 probably a good convention to follow). However, the distribution name
1335 is used to generate filenames, so you should stick to letters, digits,
1336 underscores, and hyphens.
1338 Since
\option{py
\_modules} is a list, you can of course specify multiple
1339 modules, eg. if you're distributing modules
\module{foo
} and
1340 \module{bar
}, your setup might look like this:
1347 and the setup script might be
1349 from distutils.core import setup
1350 setup(name = "foobar", version = "
1.0",
1351 py_modules =
["foo", "bar"
])
1354 You can put module source files into another directory, but if you have
1355 enough modules to do that, it's probably easier to specify modules by
1356 package rather than listing them individually.
1359 \subsection{Pure Python distribution (by package)
}
1362 If you have more than a couple of modules to distribute, especially if
1363 they are in multiple packages, it's probably easier to specify whole
1364 packages rather than individual modules. This works even if your
1365 modules are not in a package; you can just tell the Distutils to process
1366 modules from the root package, and that works the same as any other
1367 package (except that you don't have to have an
\file{\_\_init\_\_.py
}
1370 The setup script from the last example could also be written as
1372 from distutils.core import setup
1373 setup(name = "foobar", version = "
1.0",
1376 (The empty string stands for the root package.)
1378 If those two files are moved into a subdirectory, but remain in the root
1386 then you would still specify the root package, but you have to tell the
1387 Distutils where source files in the root package live:
1389 from distutils.core import setup
1390 setup(name = "foobar", version = "
1.0",
1391 package_dir =
{"": "src"
},
1395 More typically, though, you will want to distribute multiple modules in
1396 the same package (or in sub-packages). For example, if the
\module{foo
}
1397 and
\module{bar
} modules belong in package
\module{foobar
}, one way to
1398 layout your source tree is
1407 This is in fact the default layout expected by the Distutils, and the
1408 one that requires the least work to describe in your setup script:
1410 from distutils.core import setup
1411 setup(name = "foobar", version = "
1.0",
1412 packages =
["foobar"
])
1415 If you want to put modules in directories not named for their package,
1416 then you need to use the
\option{package
\_dir} option again. For
1417 example, if the
\file{src
} directory holds modules in the
1418 \module{foobar
} package:
1427 an appropriate setup script would be
1429 from distutils.core import setup
1430 setup(name = "foobar", version = "
1.0",
1431 package_dir =
{"foobar" : "src"
},
1432 packages =
["foobar"
])
1435 Or, you might put modules from your main package right in the
1444 in which case your setup script would be
1446 from distutils.core import setup
1447 setup(name = "foobar", version = "
1.0",
1448 package_dir =
{"foobar" : ""
},
1449 packages =
["foobar"
])
1451 (The empty string also stands for the current directory.)
1453 If you have sub-packages, they must be explicitly listed in
1454 \option{packages
}, but any entries in
\option{package
\_dir}
1455 automatically extend to sub-packages. (In other words, the Distutils
1456 does
\emph{not
} scan your source tree, trying to figure out which
1457 directories correspond to Python packages by looking for
1458 \file{\_\_init\_\_.py
} files.) Thus, if the default layout grows a
1471 then the corresponding setup script would be
1473 from distutils.core import setup
1474 setup(name = "foobar", version = "
1.0",
1475 packages =
["foobar", "foobar.subfoo"
])
1477 (Again, the empty string in
\option{package
\_dir} stands for the current
1481 \subsection{Single extension module
}
1484 Extension modules are specified using the
\option{ext
\_modules} option.
1485 \option{package
\_dir} has no effect on where extension source files are
1486 found; it only affects the source for pure Python modules. The simplest
1487 case, a single extension module in a single C source file, is:
1493 If the
\module{foo
} extension belongs in the root package, the setup
1494 script for this could be
1496 from distutils.core import setup
1497 setup(name = "foobar", version = "
1.0",
1498 ext_modules =
[Extension("foo",
["foo.c"
])
])
1501 If the extension actually belongs in a package, say
\module{foopkg
},
1504 With exactly the same source tree layout, this extension can be put in
1505 the
\module{foopkg
} package simply by changing the name of the
1508 from distutils.core import setup
1509 setup(name = "foobar", version = "
1.0",
1510 ext_modules =
[Extension("foopkg.foo",
["foo.c"
])
])
1514 %\subsection{Multiple extension modules}
1515 %\label{multiple-ext}
1518 %\subsection{Putting it all together}
1521 %\section{Extending the Distutils}
1525 %\subsection{Extending existing commands}
1526 %\label{extend-existing}
1529 %\subsection{Writing new commands}
1530 %\label{new-commands}
1532 %\XXX{Would an uninstall command be a good example here?}
1540 %\subsection{Building modules: the \protect\command{build} command family}
1543 %\subsubsection{\protect\command{build}}
1546 %\subsubsection{\protect\command{build\_py}}
1547 %\label{build-py-cmd}
1549 %\subsubsection{\protect\command{build\_ext}}
1550 %\label{build-ext-cmd}
1552 %\subsubsection{\protect\command{build\_clib}}
1553 %\label{build-clib-cmd}
1556 \subsection{Installing modules: the
\protect\command{install
} command family
}
1559 The install command ensures that the build commands have been run and then
1560 runs the subcommands
\command{install
\_lib},
1561 \command{install
\_data} and
1562 \command{install
\_scripts}.
1564 %\subsubsection{\protect\command{install\_lib}}
1565 %\label{install-lib-cmd}
1567 \subsubsection{\protect\command{install
\_data}}
1568 \label{install-data-cmd
}
1569 This command installs all data files provided with the distribution.
1571 \subsubsection{\protect\command{install
\_scripts}}
1572 \label{install-scripts-cmd
}
1573 This command installs all (Python) scripts in the distribution.
1576 %\subsection{Cleaning up: the \protect\command{clean} command}
1580 \subsection{Creating a source distribution: the
1581 \protect\command{sdist
} command
}
1585 \XXX{fragment moved down from above: needs context!
}
1587 The manifest template commands are:
1588 \begin{tableii
}{ll
}{command
}{Command
}{Description
}
1589 \lineii{include
\var{pat1
} \var{pat2
} ...
}
1590 {include all files matching any of the listed patterns
}
1591 \lineii{exclude
\var{pat1
} \var{pat2
} ...
}
1592 {exclude all files matching any of the listed patterns
}
1593 \lineii{recursive-include
\var{dir
} \var{pat1
} \var{pat2
} ...
}
1594 {include all files under
\var{dir
} matching any of the listed patterns
}
1595 \lineii{recursive-exclude
\var{dir
} \var{pat1
} \var{pat2
} ...
}
1596 {exclude all files under
\var{dir
} matching any of the listed patterns
}
1597 \lineii{global-include
\var{pat1
} \var{pat2
} ...
}
1598 {include all files anywhere in the source tree matching\\&
1599 any of the listed patterns
}
1600 \lineii{global-exclude
\var{pat1
} \var{pat2
} ...
}
1601 {exclude all files anywhere in the source tree matching\\&
1602 any of the listed patterns
}
1603 \lineii{prune
\var{dir
}}{exclude all files under
\var{dir
}}
1604 \lineii{graft
\var{dir
}}{include all files under
\var{dir
}}
1606 The patterns here are
\UNIX-style ``glob'' patterns:
\code{*
} matches any
1607 sequence of regular filename characters,
\code{?
} matches any single
1608 regular filename character, and
\code{[\var{range
}]} matches any of the
1609 characters in
\var{range
} (e.g.,
\code{a-z
},
\code{a-zA-Z
},
1610 \code{a-f0-
9\_.
}). The definition of ``regular filename character'' is
1611 platform-specific: on
\UNIX{} it is anything except slash; on Windows
1612 anything except backslash or colon; on MacOS anything except colon.
1614 \XXX{Windows and MacOS support not there yet
}
1617 %\subsection{Creating a built distribution: the
1618 % \protect\command{bdist} command family}
1622 %\subsubsection{\protect\command{bdist}}
1624 %\subsubsection{\protect\command{bdist\_dumb}}
1626 %\subsubsection{\protect\command{bdist\_rpm}}
1628 %\subsubsection{\protect\command{bdist\_wininst}}