blob: 952607a4073b25428140862e558eb940254083d6 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _setup-script:
2
3************************
4Writing the Setup Script
5************************
6
7The setup script is the centre of all activity in building, distributing, and
8installing modules using the Distutils. The main purpose of the setup script is
9to describe your module distribution to the Distutils, so that the various
10commands that operate on your modules do the right thing. As we saw in section
11:ref:`distutils-simple-example` above, the setup script consists mainly of a call to
12:func:`setup`, and most information supplied to the Distutils by the module
13developer is supplied as keyword arguments to :func:`setup`.
14
15Here's a slightly more involved example, which we'll follow for the next couple
16of sections: the Distutils' own setup script. (Keep in mind that although the
17Distutils are included with Python 1.6 and later, they also have an independent
18existence so that Python 1.5.2 users can use them to install other module
19distributions. The Distutils' own setup script, shown here, is used to install
20the package into Python 1.5.2.) ::
21
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000022 #!/usr/bin/env python
Georg Brandl116aa622007-08-15 14:28:22 +000023
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000024 from distutils.core import setup
Georg Brandl116aa622007-08-15 14:28:22 +000025
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000026 setup(name='Distutils',
27 version='1.0',
28 description='Python Distribution Utilities',
29 author='Greg Ward',
30 author_email='gward@python.net',
Georg Brandle73778c2014-10-29 08:36:35 +010031 url='https://www.python.org/sigs/distutils-sig/',
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000032 packages=['distutils', 'distutils.command'],
33 )
Georg Brandl116aa622007-08-15 14:28:22 +000034
35There are only two differences between this and the trivial one-file
36distribution presented in section :ref:`distutils-simple-example`: more metadata, and the
37specification of pure Python modules by package, rather than by module. This is
38important since the Distutils consist of a couple of dozen modules split into
39(so far) two packages; an explicit list of every module would be tedious to
40generate and difficult to maintain. For more information on the additional
41meta-data, see section :ref:`meta-data`.
42
43Note that any pathnames (files or directories) supplied in the setup script
44should be written using the Unix convention, i.e. slash-separated. The
45Distutils will take care of converting this platform-neutral representation into
46whatever is appropriate on your current platform before actually using the
47pathname. This makes your setup script portable across operating systems, which
48of course is one of the major goals of the Distutils. In this spirit, all
Georg Brandlc575c902008-09-13 17:46:05 +000049pathnames in this document are slash-separated.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51This, of course, only applies to pathnames given to Distutils functions. If
52you, for example, use standard Python functions such as :func:`glob.glob` or
53:func:`os.listdir` to specify files, you should be careful to write portable
54code instead of hardcoding path separators::
55
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000056 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
57 os.listdir(os.path.join('mydir', 'subdir'))
Georg Brandl116aa622007-08-15 14:28:22 +000058
59
60.. _listing-packages:
61
62Listing whole packages
63======================
64
Georg Brandl3f40c402014-09-21 00:35:08 +020065The ``packages`` option tells the Distutils to process (build, distribute,
Georg Brandl116aa622007-08-15 14:28:22 +000066install, etc.) all pure Python modules found in each package mentioned in the
Georg Brandl3f40c402014-09-21 00:35:08 +020067``packages`` list. In order to do this, of course, there has to be a
Georg Brandl116aa622007-08-15 14:28:22 +000068correspondence between package names and directories in the filesystem. The
69default correspondence is the most obvious one, i.e. package :mod:`distutils` is
70found in the directory :file:`distutils` relative to the distribution root.
71Thus, when you say ``packages = ['foo']`` in your setup script, you are
72promising that the Distutils will find a file :file:`foo/__init__.py` (which
73might be spelled differently on your system, but you get the idea) relative to
74the directory where your setup script lives. If you break this promise, the
R David Murrayb8990072011-07-18 12:38:03 -040075Distutils will issue a warning but still process the broken package anyway.
Georg Brandl116aa622007-08-15 14:28:22 +000076
77If you use a different convention to lay out your source directory, that's no
Georg Brandl3f40c402014-09-21 00:35:08 +020078problem: you just have to supply the ``package_dir`` option to tell the
Georg Brandl116aa622007-08-15 14:28:22 +000079Distutils about your convention. For example, say you keep all Python source
80under :file:`lib`, so that modules in the "root package" (i.e., not in any
81package at all) are in :file:`lib`, modules in the :mod:`foo` package are in
82:file:`lib/foo`, and so forth. Then you would put ::
83
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000084 package_dir = {'': 'lib'}
Georg Brandl116aa622007-08-15 14:28:22 +000085
86in your setup script. The keys to this dictionary are package names, and an
87empty package name stands for the root package. The values are directory names
88relative to your distribution root. In this case, when you say ``packages =
89['foo']``, you are promising that the file :file:`lib/foo/__init__.py` exists.
90
91Another possible convention is to put the :mod:`foo` package right in
92:file:`lib`, the :mod:`foo.bar` package in :file:`lib/bar`, etc. This would be
93written in the setup script as ::
94
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000095 package_dir = {'foo': 'lib'}
Georg Brandl116aa622007-08-15 14:28:22 +000096
Georg Brandl3f40c402014-09-21 00:35:08 +020097A ``package: dir`` entry in the ``package_dir`` dictionary implicitly
Georg Brandl116aa622007-08-15 14:28:22 +000098applies to all packages below *package*, so the :mod:`foo.bar` case is
99automatically handled here. In this example, having ``packages = ['foo',
100'foo.bar']`` tells the Distutils to look for :file:`lib/__init__.py` and
Georg Brandl3f40c402014-09-21 00:35:08 +0200101:file:`lib/bar/__init__.py`. (Keep in mind that although ``package_dir``
Georg Brandl116aa622007-08-15 14:28:22 +0000102applies recursively, you must explicitly list all packages in
Georg Brandl3f40c402014-09-21 00:35:08 +0200103``packages``: the Distutils will *not* recursively scan your source tree
Georg Brandl116aa622007-08-15 14:28:22 +0000104looking for any directory with an :file:`__init__.py` file.)
105
106
107.. _listing-modules:
108
109Listing individual modules
110==========================
111
112For a small module distribution, you might prefer to list all modules rather
113than listing packages---especially the case of a single module that goes in the
114"root package" (i.e., no package at all). This simplest case was shown in
115section :ref:`distutils-simple-example`; here is a slightly more involved example::
116
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000117 py_modules = ['mod1', 'pkg.mod2']
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119This describes two modules, one of them in the "root" package, the other in the
120:mod:`pkg` package. Again, the default package/directory layout implies that
121these two modules can be found in :file:`mod1.py` and :file:`pkg/mod2.py`, and
122that :file:`pkg/__init__.py` exists as well. And again, you can override the
Georg Brandl3f40c402014-09-21 00:35:08 +0200123package/directory correspondence using the ``package_dir`` option.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. _describing-extensions:
127
128Describing extension modules
129============================
130
131Just as writing Python extension modules is a bit more complicated than writing
132pure Python modules, describing them to the Distutils is a bit more complicated.
133Unlike pure modules, it's not enough just to list modules or packages and expect
134the Distutils to go out and find the right files; you have to specify the
135extension name, source file(s), and any compile/link requirements (include
136directories, libraries to link with, etc.).
137
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000138.. XXX read over this section
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140All of this is done through another keyword argument to :func:`setup`, the
Georg Brandl3f40c402014-09-21 00:35:08 +0200141``ext_modules`` option. ``ext_modules`` is just a list of
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300142:class:`~distutils.core.Extension` instances, each of which describes a
143single extension module.
Georg Brandl116aa622007-08-15 14:28:22 +0000144Suppose your distribution includes a single extension, called :mod:`foo` and
145implemented by :file:`foo.c`. If no additional instructions to the
146compiler/linker are needed, describing this extension is quite simple::
147
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000148 Extension('foo', ['foo.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150The :class:`Extension` class can be imported from :mod:`distutils.core` along
151with :func:`setup`. Thus, the setup script for a module distribution that
152contains only this one extension and nothing else might be::
153
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000154 from distutils.core import setup, Extension
155 setup(name='foo',
156 version='1.0',
157 ext_modules=[Extension('foo', ['foo.c'])],
158 )
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160The :class:`Extension` class (actually, the underlying extension-building
161machinery implemented by the :command:`build_ext` command) supports a great deal
162of flexibility in describing Python extensions, which is explained in the
163following sections.
164
165
166Extension names and packages
167----------------------------
168
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300169The first argument to the :class:`~distutils.core.Extension` constructor is
170always the name of the extension, including any package names. For example, ::
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000172 Extension('foo', ['src/foo1.c', 'src/foo2.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174describes an extension that lives in the root package, while ::
175
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000176 Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178describes the same extension in the :mod:`pkg` package. The source files and
179resulting object code are identical in both cases; the only difference is where
180in the filesystem (and therefore where in Python's namespace hierarchy) the
181resulting extension lives.
182
183If you have a number of extensions all in the same package (or all under the
Georg Brandl3f40c402014-09-21 00:35:08 +0200184same base package), use the ``ext_package`` keyword argument to
Georg Brandl116aa622007-08-15 14:28:22 +0000185:func:`setup`. For example, ::
186
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000187 setup(...,
188 ext_package='pkg',
189 ext_modules=[Extension('foo', ['foo.c']),
190 Extension('subpkg.bar', ['bar.c'])],
191 )
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193will compile :file:`foo.c` to the extension :mod:`pkg.foo`, and :file:`bar.c` to
194:mod:`pkg.subpkg.bar`.
195
196
197Extension source files
198----------------------
199
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300200The second argument to the :class:`~distutils.core.Extension` constructor is
201a list of source
Georg Brandl116aa622007-08-15 14:28:22 +0000202files. Since the Distutils currently only support C, C++, and Objective-C
203extensions, these are normally C/C++/Objective-C source files. (Be sure to use
Martin Panter04b3d8b2016-11-05 02:40:31 +0000204appropriate extensions to distinguish C++ source files: :file:`.cc` and
Georg Brandl116aa622007-08-15 14:28:22 +0000205:file:`.cpp` seem to be recognized by both Unix and Windows compilers.)
206
207However, you can also include SWIG interface (:file:`.i`) files in the list; the
208:command:`build_ext` command knows how to deal with SWIG extensions: it will run
209SWIG on the interface file and compile the resulting C/C++ file into your
210extension.
211
Georg Brandld5f2d6e2010-07-31 09:15:10 +0000212.. XXX SWIG support is rough around the edges and largely untested!
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214This warning notwithstanding, options to SWIG can be currently passed like
215this::
216
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000217 setup(...,
218 ext_modules=[Extension('_foo', ['foo.i'],
219 swig_opts=['-modern', '-I../include'])],
220 py_modules=['foo'],
221 )
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223Or on the commandline like this::
224
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000225 > python setup.py build_ext --swig-opts="-modern -I../include"
Georg Brandl116aa622007-08-15 14:28:22 +0000226
227On some platforms, you can include non-source files that are processed by the
228compiler and included in your extension. Currently, this just means Windows
229message text (:file:`.mc`) files and resource definition (:file:`.rc`) files for
230Visual C++. These will be compiled to binary resource (:file:`.res`) files and
231linked into the executable.
232
233
234Preprocessor options
235--------------------
236
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300237Three optional arguments to :class:`~distutils.core.Extension` will help if
238you need to specify include directories to search or preprocessor macros to
239define/undefine: ``include_dirs``, ``define_macros``, and ``undef_macros``.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
241For example, if your extension requires header files in the :file:`include`
242directory under your distribution root, use the ``include_dirs`` option::
243
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000244 Extension('foo', ['foo.c'], include_dirs=['include'])
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246You can specify absolute directories there; if you know that your extension will
247only be built on Unix systems with X11R6 installed to :file:`/usr`, you can get
248away with ::
249
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000250 Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11'])
Georg Brandl116aa622007-08-15 14:28:22 +0000251
252You should avoid this sort of non-portable usage if you plan to distribute your
253code: it's probably better to write C code like ::
254
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000255 #include <X11/Xlib.h>
Georg Brandl116aa622007-08-15 14:28:22 +0000256
257If you need to include header files from some other Python extension, you can
258take advantage of the fact that header files are installed in a consistent way
Éric Araujo4d71a662011-08-19 03:44:36 +0200259by the Distutils :command:`install_headers` command. For example, the Numerical
Georg Brandl116aa622007-08-15 14:28:22 +0000260Python header files are installed (on a standard Unix installation) to
261:file:`/usr/local/include/python1.5/Numerical`. (The exact location will differ
262according to your platform and Python installation.) Since the Python include
263directory---\ :file:`/usr/local/include/python1.5` in this case---is always
264included in the search path when building Python extensions, the best approach
265is to write C code like ::
266
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000267 #include <Numerical/arrayobject.h>
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269If you must put the :file:`Numerical` include directory right into your header
270search path, though, you can find that directory using the Distutils
271:mod:`distutils.sysconfig` module::
272
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000273 from distutils.sysconfig import get_python_inc
274 incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical')
275 setup(...,
276 Extension(..., include_dirs=[incdir]),
277 )
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279Even though this is quite portable---it will work on any Python installation,
280regardless of platform---it's probably easier to just write your C code in the
281sensible way.
282
283You can define and undefine pre-processor macros with the ``define_macros`` and
284``undef_macros`` options. ``define_macros`` takes a list of ``(name, value)``
285tuples, where ``name`` is the name of the macro to define (a string) and
286``value`` is its value: either a string or ``None``. (Defining a macro ``FOO``
287to ``None`` is the equivalent of a bare ``#define FOO`` in your C source: with
288most compilers, this sets ``FOO`` to the string ``1``.) ``undef_macros`` is
289just a list of macros to undefine.
290
291For example::
292
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000293 Extension(...,
294 define_macros=[('NDEBUG', '1'),
295 ('HAVE_STRFTIME', None)],
296 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
Georg Brandl116aa622007-08-15 14:28:22 +0000297
298is the equivalent of having this at the top of every C source file::
299
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000300 #define NDEBUG 1
301 #define HAVE_STRFTIME
302 #undef HAVE_FOO
303 #undef HAVE_BAR
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305
306Library options
307---------------
308
309You can also specify the libraries to link against when building your extension,
310and the directories to search for those libraries. The ``libraries`` option is
311a list of libraries to link against, ``library_dirs`` is a list of directories
312to search for libraries at link-time, and ``runtime_library_dirs`` is a list of
313directories to search for shared (dynamically loaded) libraries at run-time.
314
315For example, if you need to link against libraries known to be in the standard
316library search path on target systems ::
317
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000318 Extension(...,
319 libraries=['gdbm', 'readline'])
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321If you need to link with libraries in a non-standard location, you'll have to
322include the location in ``library_dirs``::
323
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000324 Extension(...,
325 library_dirs=['/usr/X11R6/lib'],
326 libraries=['X11', 'Xt'])
Georg Brandl116aa622007-08-15 14:28:22 +0000327
328(Again, this sort of non-portable construct should be avoided if you intend to
329distribute your code.)
330
Georg Brandld5f2d6e2010-07-31 09:15:10 +0000331.. XXX Should mention clib libraries here or somewhere else!
Georg Brandl116aa622007-08-15 14:28:22 +0000332
333
334Other options
335-------------
336
337There are still some other options which can be used to handle special cases.
338
Georg Brandl3f40c402014-09-21 00:35:08 +0200339The ``optional`` option is a boolean; if it is true,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +0000340a build failure in the extension will not abort the build process, but
341instead simply not install the failing extension.
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000342
Georg Brandl3f40c402014-09-21 00:35:08 +0200343The ``extra_objects`` option is a list of object files to be passed to the
Georg Brandl116aa622007-08-15 14:28:22 +0000344linker. These files must not have extensions, as the default extension for the
345compiler is used.
346
Georg Brandl3f40c402014-09-21 00:35:08 +0200347``extra_compile_args`` and ``extra_link_args`` can be used to
Georg Brandl116aa622007-08-15 14:28:22 +0000348specify additional command line options for the respective compiler and linker
349command lines.
350
Georg Brandl3f40c402014-09-21 00:35:08 +0200351``export_symbols`` is only useful on Windows. It can contain a list of
Georg Brandl116aa622007-08-15 14:28:22 +0000352symbols (functions or variables) to be exported. This option is not needed when
353building compiled extensions: Distutils will automatically add ``initmodule``
354to the list of exported symbols.
355
Georg Brandl3f40c402014-09-21 00:35:08 +0200356The ``depends`` option is a list of files that the extension depends on
Tarek Ziadé76cb7ed2009-02-13 09:15:20 +0000357(for example header files). The build command will call the compiler on the
358sources to rebuild extension if any on this files has been modified since the
359previous build.
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361Relationships between Distributions and Packages
362================================================
363
364A distribution may relate to packages in three specific ways:
365
366#. It can require packages or modules.
367
368#. It can provide packages or modules.
369
370#. It can obsolete packages or modules.
371
372These relationships can be specified using keyword arguments to the
373:func:`distutils.core.setup` function.
374
375Dependencies on other Python modules and packages can be specified by supplying
376the *requires* keyword argument to :func:`setup`. The value must be a list of
377strings. Each string specifies a package that is required, and optionally what
378versions are sufficient.
379
380To specify that any version of a module or package is required, the string
381should consist entirely of the module or package name. Examples include
382``'mymodule'`` and ``'xml.parsers.expat'``.
383
384If specific versions are required, a sequence of qualifiers can be supplied in
385parentheses. Each qualifier may consist of a comparison operator and a version
386number. The accepted comparison operators are::
387
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000388 < > ==
389 <= >= !=
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391These can be combined by using multiple qualifiers separated by commas (and
392optional whitespace). In this case, all of the qualifiers must be matched; a
393logical AND is used to combine the evaluations.
394
395Let's look at a bunch of examples:
396
397+-------------------------+----------------------------------------------+
398| Requires Expression | Explanation |
399+=========================+==============================================+
400| ``==1.0`` | Only version ``1.0`` is compatible |
401+-------------------------+----------------------------------------------+
402| ``>1.0, !=1.5.1, <2.0`` | Any version after ``1.0`` and before ``2.0`` |
403| | is compatible, except ``1.5.1`` |
404+-------------------------+----------------------------------------------+
405
406Now that we can specify dependencies, we also need to be able to specify what we
407provide that other distributions can require. This is done using the *provides*
408keyword argument to :func:`setup`. The value for this keyword is a list of
409strings, each of which names a Python module or package, and optionally
410identifies the version. If the version is not specified, it is assumed to match
411that of the distribution.
412
413Some examples:
414
415+---------------------+----------------------------------------------+
416| Provides Expression | Explanation |
417+=====================+==============================================+
418| ``mypkg`` | Provide ``mypkg``, using the distribution |
419| | version |
420+---------------------+----------------------------------------------+
421| ``mypkg (1.1)`` | Provide ``mypkg`` version 1.1, regardless of |
422| | the distribution version |
423+---------------------+----------------------------------------------+
424
425A package can declare that it obsoletes other packages using the *obsoletes*
426keyword argument. The value for this is similar to that of the *requires*
427keyword: a list of strings giving module or package specifiers. Each specifier
428consists of a module or package name optionally followed by one or more version
429qualifiers. Version qualifiers are given in parentheses after the module or
430package name.
431
432The versions identified by the qualifiers are those that are obsoleted by the
433distribution being described. If no qualifiers are given, all versions of the
434named module or package are understood to be obsoleted.
435
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000436.. _distutils-installing-scripts:
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438Installing Scripts
439==================
440
441So far we have been dealing with pure and non-pure Python modules, which are
442usually not run by themselves but imported by scripts.
443
444Scripts are files containing Python source code, intended to be started from the
445command line. Scripts don't require Distutils to do anything very complicated.
446The only clever feature is that if the first line of the script starts with
447``#!`` and contains the word "python", the Distutils will adjust the first line
448to refer to the current interpreter location. By default, it is replaced with
Martin Panter5c679332016-10-30 04:20:17 +0000449the current interpreter location. The :option:`!--executable` (or :option:`!-e`)
Georg Brandl116aa622007-08-15 14:28:22 +0000450option will allow the interpreter path to be explicitly overridden.
451
Georg Brandl3f40c402014-09-21 00:35:08 +0200452The ``scripts`` option simply is a list of files to be handled in this
Georg Brandl116aa622007-08-15 14:28:22 +0000453way. From the PyXML setup script::
454
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000455 setup(...,
456 scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
457 )
Georg Brandl116aa622007-08-15 14:28:22 +0000458
Georg Brandl56a59042009-04-27 08:58:15 +0000459.. versionchanged:: 3.1
460 All the scripts will also be added to the ``MANIFEST`` file if no template is
461 provided. See :ref:`manifest`.
462
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000463
464.. _distutils-installing-package-data:
Georg Brandl116aa622007-08-15 14:28:22 +0000465
466Installing Package Data
467=======================
468
469Often, additional files need to be installed into a package. These files are
470often data that's closely related to the package's implementation, or text files
471containing documentation that might be of interest to programmers using the
472package. These files are called :dfn:`package data`.
473
474Package data can be added to packages using the ``package_data`` keyword
475argument to the :func:`setup` function. The value must be a mapping from
476package name to a list of relative path names that should be copied into the
477package. The paths are interpreted as relative to the directory containing the
478package (information from the ``package_dir`` mapping is used if appropriate);
479that is, the files are expected to be part of the package in the source
480directories. They may contain glob patterns as well.
481
482The path names may contain directory portions; any necessary directories will be
483created in the installation.
484
485For example, if a package should contain a subdirectory with several data files,
486the files can be arranged like this in the source tree::
487
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000488 setup.py
489 src/
490 mypkg/
491 __init__.py
492 module.py
493 data/
494 tables.dat
495 spoons.dat
496 forks.dat
Georg Brandl116aa622007-08-15 14:28:22 +0000497
498The corresponding call to :func:`setup` might be::
499
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000500 setup(...,
501 packages=['mypkg'],
502 package_dir={'mypkg': 'src/mypkg'},
503 package_data={'mypkg': ['data/*.dat']},
504 )
Georg Brandl116aa622007-08-15 14:28:22 +0000505
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Georg Brandl56a59042009-04-27 08:58:15 +0000507.. versionchanged:: 3.1
508 All the files that match ``package_data`` will be added to the ``MANIFEST``
509 file if no template is provided. See :ref:`manifest`.
510
511
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000512.. _distutils-additional-files:
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514Installing Additional Files
515===========================
516
Georg Brandl3f40c402014-09-21 00:35:08 +0200517The ``data_files`` option can be used to specify additional files needed
Georg Brandl116aa622007-08-15 14:28:22 +0000518by the module distribution: configuration files, message catalogs, data files,
519anything which doesn't fit in the previous categories.
520
Georg Brandl3f40c402014-09-21 00:35:08 +0200521``data_files`` specifies a sequence of (*directory*, *files*) pairs in the
Georg Brandl116aa622007-08-15 14:28:22 +0000522following way::
523
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000524 setup(...,
525 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
526 ('config', ['cfg/data.cfg']),
527 ('/etc/init.d', ['init-script'])]
528 )
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530Note that you can specify the directory names where the data files will be
531installed, but you cannot rename the data files themselves.
532
533Each (*directory*, *files*) pair in the sequence specifies the installation
534directory and the files to install there. If *directory* is a relative path, it
535is interpreted relative to the installation prefix (Python's ``sys.prefix`` for
536pure-Python packages, ``sys.exec_prefix`` for packages that contain extension
537modules). Each file name in *files* is interpreted relative to the
538:file:`setup.py` script at the top of the package source distribution. No
539directory information from *files* is used to determine the final location of
540the installed file; only the name of the file is used.
541
Georg Brandl3f40c402014-09-21 00:35:08 +0200542You can specify the ``data_files`` options as a simple sequence of files
Georg Brandl116aa622007-08-15 14:28:22 +0000543without specifying a target directory, but this is not recommended, and the
544:command:`install` command will print a warning in this case. To install data
545files directly in the target directory, an empty string should be given as the
546directory.
547
Georg Brandl56a59042009-04-27 08:58:15 +0000548.. versionchanged:: 3.1
549 All the files that match ``data_files`` will be added to the ``MANIFEST``
550 file if no template is provided. See :ref:`manifest`.
551
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553.. _meta-data:
554
555Additional meta-data
556====================
557
558The setup script may include additional meta-data beyond the name and version.
559This information includes:
560
561+----------------------+---------------------------+-----------------+--------+
562| Meta-Data | Description | Value | Notes |
563+======================+===========================+=================+========+
564| ``name`` | name of the package | short string | \(1) |
565+----------------------+---------------------------+-----------------+--------+
566| ``version`` | version of this release | short string | (1)(2) |
567+----------------------+---------------------------+-----------------+--------+
568| ``author`` | package author's name | short string | \(3) |
569+----------------------+---------------------------+-----------------+--------+
570| ``author_email`` | email address of the | email address | \(3) |
571| | package author | | |
572+----------------------+---------------------------+-----------------+--------+
573| ``maintainer`` | package maintainer's name | short string | \(3) |
574+----------------------+---------------------------+-----------------+--------+
575| ``maintainer_email`` | email address of the | email address | \(3) |
576| | package maintainer | | |
577+----------------------+---------------------------+-----------------+--------+
578| ``url`` | home page for the package | URL | \(1) |
579+----------------------+---------------------------+-----------------+--------+
580| ``description`` | short, summary | short string | |
581| | description of the | | |
582| | package | | |
583+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300584| ``long_description`` | longer description of the | long string | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000585| | package | | |
586+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300587| ``download_url`` | location where the | URL | |
Georg Brandl116aa622007-08-15 14:28:22 +0000588| | package may be downloaded | | |
589+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300590| ``classifiers`` | a list of classifiers | list of strings | (6)(7) |
Georg Brandl116aa622007-08-15 14:28:22 +0000591+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300592| ``platforms`` | a list of platforms | list of strings | (6)(8) |
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000593+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300594| ``keywords`` | a list of keywords | list of strings | (6)(8) |
595+----------------------+---------------------------+-----------------+--------+
596| ``license`` | license for the package | short string | \(5) |
Tarek Ziadé5e06a652009-06-16 07:43:42 +0000597+----------------------+---------------------------+-----------------+--------+
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599Notes:
600
601(1)
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000602 These fields are required.
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604(2)
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000605 It is recommended that versions take the form *major.minor[.patch[.sub]]*.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607(3)
Petri Lehtinen905b6482013-02-23 21:05:27 +0100608 Either the author or the maintainer must be identified. If maintainer is
609 provided, distutils lists it as the author in :file:`PKG-INFO`.
Georg Brandl116aa622007-08-15 14:28:22 +0000610
611(4)
Chris Jerdonek13fb9792013-02-27 10:00:20 -0800612 The ``long_description`` field is used by PyPI when you are
613 :ref:`registering <package-register>` a package, to
614 :ref:`build its home page <package-display>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000615
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300616(5)
Tarek Ziadé5e06a652009-06-16 07:43:42 +0000617 The ``license`` field is a text indicating the license covering the
618 package where the license is not a selection from the "License" Trove
619 classifiers. See the ``Classifier`` field. Notice that
620 there's a ``licence`` distribution option which is deprecated but still
621 acts as an alias for ``license``.
622
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300623(6)
624 This field must be a list.
625
626(7)
627 The valid classifiers are listed on
Sanyam Khurana338cd832018-01-20 05:55:37 +0530628 `PyPI <https://pypi.python.org/pypi?:action=list_classifiers>`_.
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300629
630(8)
631 To preserve backward compatibility, this field also accepts a string. If
632 you pass a comma-separated string ``'foo, bar'``, it will be converted to
633 ``['foo', 'bar']``, Otherwise, it will be converted to a list of one
634 string.
635
Georg Brandl116aa622007-08-15 14:28:22 +0000636'short string'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000637 A single line of text, not more than 200 characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639'long string'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000640 Multiple lines of plain text in reStructuredText format (see
Georg Brandlb7354a62014-10-29 10:57:37 +0100641 http://docutils.sourceforge.net/).
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643'list of strings'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000644 See below.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
Georg Brandl116aa622007-08-15 14:28:22 +0000646Encoding the version information is an art in itself. Python packages generally
647adhere to the version format *major.minor[.patch][sub]*. The major number is 0
648for initial, experimental releases of software. It is incremented for releases
649that represent major milestones in a package. The minor number is incremented
650when important new features are added to the package. The patch number
651increments when bug-fix releases are made. Additional trailing version
652information is sometimes used to indicate sub-releases. These are
653"a1,a2,...,aN" (for alpha releases, where functionality and API may change),
654"b1,b2,...,bN" (for beta releases, which only fix bugs) and "pr1,pr2,...,prN"
655(for final pre-release release testing). Some examples:
656
6570.1.0
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000658 the first, experimental release of a package
Georg Brandl116aa622007-08-15 14:28:22 +0000659
6601.0.1a2
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000661 the second alpha release of the first patch version of 1.0
Georg Brandl116aa622007-08-15 14:28:22 +0000662
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300663``classifiers`` must be specified in a list::
Georg Brandl116aa622007-08-15 14:28:22 +0000664
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000665 setup(...,
666 classifiers=[
667 'Development Status :: 4 - Beta',
668 'Environment :: Console',
669 'Environment :: Web Environment',
670 'Intended Audience :: End Users/Desktop',
671 'Intended Audience :: Developers',
672 'Intended Audience :: System Administrators',
673 'License :: OSI Approved :: Python Software Foundation License',
674 'Operating System :: MacOS :: MacOS X',
675 'Operating System :: Microsoft :: Windows',
676 'Operating System :: POSIX',
677 'Programming Language :: Python',
678 'Topic :: Communications :: Email',
679 'Topic :: Office/Business',
680 'Topic :: Software Development :: Bug Tracking',
681 ],
682 )
Georg Brandl116aa622007-08-15 14:28:22 +0000683
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300684.. versionchanged:: 3.7
685 :class:`~distutils.core.setup` now raises a :exc:`TypeError` if
686 ``classifiers``, ``keywords`` and ``platforms`` fields are not specified
687 as a list.
688
Larry Hastings3732ed22014-03-15 21:13:56 -0700689.. _debug-setup-script:
690
Georg Brandl116aa622007-08-15 14:28:22 +0000691Debugging the setup script
692==========================
693
694Sometimes things go wrong, and the setup script doesn't do what the developer
695wants.
696
697Distutils catches any exceptions when running the setup script, and print a
698simple error message before the script is terminated. The motivation for this
699behaviour is to not confuse administrators who don't know much about Python and
700are trying to install a package. If they get a big long traceback from deep
701inside the guts of Distutils, they may think the package or the Python
702installation is broken because they don't read all the way down to the bottom
703and see that it's a permission problem.
704
705On the other hand, this doesn't help the developer to find the cause of the
Larry Hastings3732ed22014-03-15 21:13:56 -0700706failure. For this purpose, the :envvar:`DISTUTILS_DEBUG` environment variable can be set
Georg Brandl116aa622007-08-15 14:28:22 +0000707to anything except an empty string, and distutils will now print detailed
Larry Hastings3732ed22014-03-15 21:13:56 -0700708information about what it is doing, dump the full traceback when an exception
709occurs, and print the whole command line when an external program (like a C
710compiler) fails.