blob: 4386a60b664bfb3581a8d554cdbf6278164a34dc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _setup-script:
2
3************************
4Writing the Setup Script
5************************
6
Nick Coghlandae12292019-05-14 22:04:30 +10007.. include:: ./_setuptools_disclaimer.rst
8
Georg Brandl116aa622007-08-15 14:28:22 +00009The setup script is the centre of all activity in building, distributing, and
10installing modules using the Distutils. The main purpose of the setup script is
11to describe your module distribution to the Distutils, so that the various
12commands that operate on your modules do the right thing. As we saw in section
13:ref:`distutils-simple-example` above, the setup script consists mainly of a call to
14:func:`setup`, and most information supplied to the Distutils by the module
15developer is supplied as keyword arguments to :func:`setup`.
16
17Here's a slightly more involved example, which we'll follow for the next couple
18of sections: the Distutils' own setup script. (Keep in mind that although the
19Distutils are included with Python 1.6 and later, they also have an independent
20existence so that Python 1.5.2 users can use them to install other module
21distributions. The Distutils' own setup script, shown here, is used to install
22the package into Python 1.5.2.) ::
23
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000024 #!/usr/bin/env python
Georg Brandl116aa622007-08-15 14:28:22 +000025
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000026 from distutils.core import setup
Georg Brandl116aa622007-08-15 14:28:22 +000027
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000028 setup(name='Distutils',
29 version='1.0',
30 description='Python Distribution Utilities',
31 author='Greg Ward',
32 author_email='gward@python.net',
Georg Brandle73778c2014-10-29 08:36:35 +010033 url='https://www.python.org/sigs/distutils-sig/',
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000034 packages=['distutils', 'distutils.command'],
35 )
Georg Brandl116aa622007-08-15 14:28:22 +000036
37There are only two differences between this and the trivial one-file
38distribution presented in section :ref:`distutils-simple-example`: more metadata, and the
39specification of pure Python modules by package, rather than by module. This is
40important since the Distutils consist of a couple of dozen modules split into
41(so far) two packages; an explicit list of every module would be tedious to
42generate and difficult to maintain. For more information on the additional
43meta-data, see section :ref:`meta-data`.
44
45Note that any pathnames (files or directories) supplied in the setup script
46should be written using the Unix convention, i.e. slash-separated. The
47Distutils will take care of converting this platform-neutral representation into
48whatever is appropriate on your current platform before actually using the
49pathname. This makes your setup script portable across operating systems, which
50of course is one of the major goals of the Distutils. In this spirit, all
Georg Brandlc575c902008-09-13 17:46:05 +000051pathnames in this document are slash-separated.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53This, of course, only applies to pathnames given to Distutils functions. If
54you, for example, use standard Python functions such as :func:`glob.glob` or
55:func:`os.listdir` to specify files, you should be careful to write portable
56code instead of hardcoding path separators::
57
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000058 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
59 os.listdir(os.path.join('mydir', 'subdir'))
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
62.. _listing-packages:
63
64Listing whole packages
65======================
66
Georg Brandl3f40c402014-09-21 00:35:08 +020067The ``packages`` option tells the Distutils to process (build, distribute,
Georg Brandl116aa622007-08-15 14:28:22 +000068install, etc.) all pure Python modules found in each package mentioned in the
Georg Brandl3f40c402014-09-21 00:35:08 +020069``packages`` list. In order to do this, of course, there has to be a
Georg Brandl116aa622007-08-15 14:28:22 +000070correspondence between package names and directories in the filesystem. The
71default correspondence is the most obvious one, i.e. package :mod:`distutils` is
72found in the directory :file:`distutils` relative to the distribution root.
73Thus, when you say ``packages = ['foo']`` in your setup script, you are
74promising that the Distutils will find a file :file:`foo/__init__.py` (which
75might be spelled differently on your system, but you get the idea) relative to
76the directory where your setup script lives. If you break this promise, the
R David Murrayb8990072011-07-18 12:38:03 -040077Distutils will issue a warning but still process the broken package anyway.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79If you use a different convention to lay out your source directory, that's no
Georg Brandl3f40c402014-09-21 00:35:08 +020080problem: you just have to supply the ``package_dir`` option to tell the
Georg Brandl116aa622007-08-15 14:28:22 +000081Distutils about your convention. For example, say you keep all Python source
82under :file:`lib`, so that modules in the "root package" (i.e., not in any
83package at all) are in :file:`lib`, modules in the :mod:`foo` package are in
84:file:`lib/foo`, and so forth. Then you would put ::
85
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000086 package_dir = {'': 'lib'}
Georg Brandl116aa622007-08-15 14:28:22 +000087
88in your setup script. The keys to this dictionary are package names, and an
89empty package name stands for the root package. The values are directory names
90relative to your distribution root. In this case, when you say ``packages =
91['foo']``, you are promising that the file :file:`lib/foo/__init__.py` exists.
92
93Another possible convention is to put the :mod:`foo` package right in
94:file:`lib`, the :mod:`foo.bar` package in :file:`lib/bar`, etc. This would be
95written in the setup script as ::
96
Tarek Ziadé3177f2f2009-02-27 02:22:25 +000097 package_dir = {'foo': 'lib'}
Georg Brandl116aa622007-08-15 14:28:22 +000098
Georg Brandl3f40c402014-09-21 00:35:08 +020099A ``package: dir`` entry in the ``package_dir`` dictionary implicitly
Georg Brandl116aa622007-08-15 14:28:22 +0000100applies to all packages below *package*, so the :mod:`foo.bar` case is
101automatically handled here. In this example, having ``packages = ['foo',
102'foo.bar']`` tells the Distutils to look for :file:`lib/__init__.py` and
Georg Brandl3f40c402014-09-21 00:35:08 +0200103:file:`lib/bar/__init__.py`. (Keep in mind that although ``package_dir``
Georg Brandl116aa622007-08-15 14:28:22 +0000104applies recursively, you must explicitly list all packages in
Georg Brandl3f40c402014-09-21 00:35:08 +0200105``packages``: the Distutils will *not* recursively scan your source tree
Georg Brandl116aa622007-08-15 14:28:22 +0000106looking for any directory with an :file:`__init__.py` file.)
107
108
109.. _listing-modules:
110
111Listing individual modules
112==========================
113
114For a small module distribution, you might prefer to list all modules rather
115than listing packages---especially the case of a single module that goes in the
116"root package" (i.e., no package at all). This simplest case was shown in
117section :ref:`distutils-simple-example`; here is a slightly more involved example::
118
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000119 py_modules = ['mod1', 'pkg.mod2']
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121This describes two modules, one of them in the "root" package, the other in the
122:mod:`pkg` package. Again, the default package/directory layout implies that
123these two modules can be found in :file:`mod1.py` and :file:`pkg/mod2.py`, and
124that :file:`pkg/__init__.py` exists as well. And again, you can override the
Georg Brandl3f40c402014-09-21 00:35:08 +0200125package/directory correspondence using the ``package_dir`` option.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. _describing-extensions:
129
130Describing extension modules
131============================
132
133Just as writing Python extension modules is a bit more complicated than writing
134pure Python modules, describing them to the Distutils is a bit more complicated.
135Unlike pure modules, it's not enough just to list modules or packages and expect
136the Distutils to go out and find the right files; you have to specify the
137extension name, source file(s), and any compile/link requirements (include
138directories, libraries to link with, etc.).
139
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000140.. XXX read over this section
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142All of this is done through another keyword argument to :func:`setup`, the
Georg Brandl3f40c402014-09-21 00:35:08 +0200143``ext_modules`` option. ``ext_modules`` is just a list of
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300144:class:`~distutils.core.Extension` instances, each of which describes a
145single extension module.
Georg Brandl116aa622007-08-15 14:28:22 +0000146Suppose your distribution includes a single extension, called :mod:`foo` and
147implemented by :file:`foo.c`. If no additional instructions to the
148compiler/linker are needed, describing this extension is quite simple::
149
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000150 Extension('foo', ['foo.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152The :class:`Extension` class can be imported from :mod:`distutils.core` along
153with :func:`setup`. Thus, the setup script for a module distribution that
154contains only this one extension and nothing else might be::
155
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000156 from distutils.core import setup, Extension
157 setup(name='foo',
158 version='1.0',
159 ext_modules=[Extension('foo', ['foo.c'])],
160 )
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162The :class:`Extension` class (actually, the underlying extension-building
163machinery implemented by the :command:`build_ext` command) supports a great deal
164of flexibility in describing Python extensions, which is explained in the
165following sections.
166
167
168Extension names and packages
169----------------------------
170
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300171The first argument to the :class:`~distutils.core.Extension` constructor is
172always the name of the extension, including any package names. For example, ::
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000174 Extension('foo', ['src/foo1.c', 'src/foo2.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176describes an extension that lives in the root package, while ::
177
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000178 Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])
Georg Brandl116aa622007-08-15 14:28:22 +0000179
180describes the same extension in the :mod:`pkg` package. The source files and
181resulting object code are identical in both cases; the only difference is where
182in the filesystem (and therefore where in Python's namespace hierarchy) the
183resulting extension lives.
184
185If you have a number of extensions all in the same package (or all under the
Georg Brandl3f40c402014-09-21 00:35:08 +0200186same base package), use the ``ext_package`` keyword argument to
Georg Brandl116aa622007-08-15 14:28:22 +0000187:func:`setup`. For example, ::
188
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000189 setup(...,
190 ext_package='pkg',
191 ext_modules=[Extension('foo', ['foo.c']),
192 Extension('subpkg.bar', ['bar.c'])],
193 )
Georg Brandl116aa622007-08-15 14:28:22 +0000194
195will compile :file:`foo.c` to the extension :mod:`pkg.foo`, and :file:`bar.c` to
196:mod:`pkg.subpkg.bar`.
197
198
199Extension source files
200----------------------
201
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300202The second argument to the :class:`~distutils.core.Extension` constructor is
203a list of source
Georg Brandl116aa622007-08-15 14:28:22 +0000204files. Since the Distutils currently only support C, C++, and Objective-C
205extensions, these are normally C/C++/Objective-C source files. (Be sure to use
Martin Panter04b3d8b2016-11-05 02:40:31 +0000206appropriate extensions to distinguish C++ source files: :file:`.cc` and
Georg Brandl116aa622007-08-15 14:28:22 +0000207:file:`.cpp` seem to be recognized by both Unix and Windows compilers.)
208
209However, you can also include SWIG interface (:file:`.i`) files in the list; the
210:command:`build_ext` command knows how to deal with SWIG extensions: it will run
211SWIG on the interface file and compile the resulting C/C++ file into your
212extension.
213
Georg Brandld5f2d6e2010-07-31 09:15:10 +0000214.. XXX SWIG support is rough around the edges and largely untested!
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216This warning notwithstanding, options to SWIG can be currently passed like
217this::
218
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000219 setup(...,
220 ext_modules=[Extension('_foo', ['foo.i'],
221 swig_opts=['-modern', '-I../include'])],
222 py_modules=['foo'],
223 )
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225Or on the commandline like this::
226
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000227 > python setup.py build_ext --swig-opts="-modern -I../include"
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229On some platforms, you can include non-source files that are processed by the
230compiler and included in your extension. Currently, this just means Windows
231message text (:file:`.mc`) files and resource definition (:file:`.rc`) files for
232Visual C++. These will be compiled to binary resource (:file:`.res`) files and
233linked into the executable.
234
235
236Preprocessor options
237--------------------
238
Serhiy Storchaka7880db62013-10-09 14:09:16 +0300239Three optional arguments to :class:`~distutils.core.Extension` will help if
240you need to specify include directories to search or preprocessor macros to
241define/undefine: ``include_dirs``, ``define_macros``, and ``undef_macros``.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243For example, if your extension requires header files in the :file:`include`
244directory under your distribution root, use the ``include_dirs`` option::
245
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000246 Extension('foo', ['foo.c'], include_dirs=['include'])
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248You can specify absolute directories there; if you know that your extension will
249only be built on Unix systems with X11R6 installed to :file:`/usr`, you can get
250away with ::
251
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000252 Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11'])
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254You should avoid this sort of non-portable usage if you plan to distribute your
255code: it's probably better to write C code like ::
256
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000257 #include <X11/Xlib.h>
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259If you need to include header files from some other Python extension, you can
260take advantage of the fact that header files are installed in a consistent way
Éric Araujo4d71a662011-08-19 03:44:36 +0200261by the Distutils :command:`install_headers` command. For example, the Numerical
Georg Brandl116aa622007-08-15 14:28:22 +0000262Python header files are installed (on a standard Unix installation) to
263:file:`/usr/local/include/python1.5/Numerical`. (The exact location will differ
264according to your platform and Python installation.) Since the Python include
265directory---\ :file:`/usr/local/include/python1.5` in this case---is always
266included in the search path when building Python extensions, the best approach
267is to write C code like ::
268
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000269 #include <Numerical/arrayobject.h>
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271If you must put the :file:`Numerical` include directory right into your header
272search path, though, you can find that directory using the Distutils
273:mod:`distutils.sysconfig` module::
274
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000275 from distutils.sysconfig import get_python_inc
276 incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical')
277 setup(...,
278 Extension(..., include_dirs=[incdir]),
279 )
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281Even though this is quite portable---it will work on any Python installation,
282regardless of platform---it's probably easier to just write your C code in the
283sensible way.
284
285You can define and undefine pre-processor macros with the ``define_macros`` and
286``undef_macros`` options. ``define_macros`` takes a list of ``(name, value)``
287tuples, where ``name`` is the name of the macro to define (a string) and
288``value`` is its value: either a string or ``None``. (Defining a macro ``FOO``
289to ``None`` is the equivalent of a bare ``#define FOO`` in your C source: with
290most compilers, this sets ``FOO`` to the string ``1``.) ``undef_macros`` is
291just a list of macros to undefine.
292
293For example::
294
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000295 Extension(...,
296 define_macros=[('NDEBUG', '1'),
297 ('HAVE_STRFTIME', None)],
298 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300is the equivalent of having this at the top of every C source file::
301
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000302 #define NDEBUG 1
303 #define HAVE_STRFTIME
304 #undef HAVE_FOO
305 #undef HAVE_BAR
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307
308Library options
309---------------
310
311You can also specify the libraries to link against when building your extension,
312and the directories to search for those libraries. The ``libraries`` option is
313a list of libraries to link against, ``library_dirs`` is a list of directories
314to search for libraries at link-time, and ``runtime_library_dirs`` is a list of
315directories to search for shared (dynamically loaded) libraries at run-time.
316
317For example, if you need to link against libraries known to be in the standard
318library search path on target systems ::
319
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000320 Extension(...,
321 libraries=['gdbm', 'readline'])
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323If you need to link with libraries in a non-standard location, you'll have to
324include the location in ``library_dirs``::
325
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000326 Extension(...,
327 library_dirs=['/usr/X11R6/lib'],
328 libraries=['X11', 'Xt'])
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330(Again, this sort of non-portable construct should be avoided if you intend to
331distribute your code.)
332
Georg Brandld5f2d6e2010-07-31 09:15:10 +0000333.. XXX Should mention clib libraries here or somewhere else!
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335
336Other options
337-------------
338
339There are still some other options which can be used to handle special cases.
340
Georg Brandl3f40c402014-09-21 00:35:08 +0200341The ``optional`` option is a boolean; if it is true,
Benjamin Petersonef3e4c22009-04-11 19:48:14 +0000342a build failure in the extension will not abort the build process, but
343instead simply not install the failing extension.
Tarek Ziadéb2e36f12009-03-31 22:37:55 +0000344
Georg Brandl3f40c402014-09-21 00:35:08 +0200345The ``extra_objects`` option is a list of object files to be passed to the
Georg Brandl116aa622007-08-15 14:28:22 +0000346linker. These files must not have extensions, as the default extension for the
347compiler is used.
348
Georg Brandl3f40c402014-09-21 00:35:08 +0200349``extra_compile_args`` and ``extra_link_args`` can be used to
Georg Brandl116aa622007-08-15 14:28:22 +0000350specify additional command line options for the respective compiler and linker
351command lines.
352
Georg Brandl3f40c402014-09-21 00:35:08 +0200353``export_symbols`` is only useful on Windows. It can contain a list of
Georg Brandl116aa622007-08-15 14:28:22 +0000354symbols (functions or variables) to be exported. This option is not needed when
355building compiled extensions: Distutils will automatically add ``initmodule``
356to the list of exported symbols.
357
Georg Brandl3f40c402014-09-21 00:35:08 +0200358The ``depends`` option is a list of files that the extension depends on
Tarek Ziadé76cb7ed2009-02-13 09:15:20 +0000359(for example header files). The build command will call the compiler on the
360sources to rebuild extension if any on this files has been modified since the
361previous build.
Georg Brandl116aa622007-08-15 14:28:22 +0000362
363Relationships between Distributions and Packages
364================================================
365
366A distribution may relate to packages in three specific ways:
367
368#. It can require packages or modules.
369
370#. It can provide packages or modules.
371
372#. It can obsolete packages or modules.
373
374These relationships can be specified using keyword arguments to the
375:func:`distutils.core.setup` function.
376
377Dependencies on other Python modules and packages can be specified by supplying
378the *requires* keyword argument to :func:`setup`. The value must be a list of
379strings. Each string specifies a package that is required, and optionally what
380versions are sufficient.
381
382To specify that any version of a module or package is required, the string
383should consist entirely of the module or package name. Examples include
384``'mymodule'`` and ``'xml.parsers.expat'``.
385
386If specific versions are required, a sequence of qualifiers can be supplied in
387parentheses. Each qualifier may consist of a comparison operator and a version
388number. The accepted comparison operators are::
389
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000390 < > ==
391 <= >= !=
Georg Brandl116aa622007-08-15 14:28:22 +0000392
393These can be combined by using multiple qualifiers separated by commas (and
394optional whitespace). In this case, all of the qualifiers must be matched; a
395logical AND is used to combine the evaluations.
396
397Let's look at a bunch of examples:
398
399+-------------------------+----------------------------------------------+
400| Requires Expression | Explanation |
401+=========================+==============================================+
402| ``==1.0`` | Only version ``1.0`` is compatible |
403+-------------------------+----------------------------------------------+
404| ``>1.0, !=1.5.1, <2.0`` | Any version after ``1.0`` and before ``2.0`` |
405| | is compatible, except ``1.5.1`` |
406+-------------------------+----------------------------------------------+
407
408Now that we can specify dependencies, we also need to be able to specify what we
409provide that other distributions can require. This is done using the *provides*
410keyword argument to :func:`setup`. The value for this keyword is a list of
411strings, each of which names a Python module or package, and optionally
412identifies the version. If the version is not specified, it is assumed to match
413that of the distribution.
414
415Some examples:
416
417+---------------------+----------------------------------------------+
418| Provides Expression | Explanation |
419+=====================+==============================================+
420| ``mypkg`` | Provide ``mypkg``, using the distribution |
421| | version |
422+---------------------+----------------------------------------------+
423| ``mypkg (1.1)`` | Provide ``mypkg`` version 1.1, regardless of |
424| | the distribution version |
425+---------------------+----------------------------------------------+
426
427A package can declare that it obsoletes other packages using the *obsoletes*
428keyword argument. The value for this is similar to that of the *requires*
429keyword: a list of strings giving module or package specifiers. Each specifier
430consists of a module or package name optionally followed by one or more version
431qualifiers. Version qualifiers are given in parentheses after the module or
432package name.
433
434The versions identified by the qualifiers are those that are obsoleted by the
435distribution being described. If no qualifiers are given, all versions of the
436named module or package are understood to be obsoleted.
437
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000438.. _distutils-installing-scripts:
Georg Brandl116aa622007-08-15 14:28:22 +0000439
440Installing Scripts
441==================
442
443So far we have been dealing with pure and non-pure Python modules, which are
444usually not run by themselves but imported by scripts.
445
446Scripts are files containing Python source code, intended to be started from the
447command line. Scripts don't require Distutils to do anything very complicated.
448The only clever feature is that if the first line of the script starts with
449``#!`` and contains the word "python", the Distutils will adjust the first line
450to refer to the current interpreter location. By default, it is replaced with
Martin Panter5c679332016-10-30 04:20:17 +0000451the current interpreter location. The :option:`!--executable` (or :option:`!-e`)
Georg Brandl116aa622007-08-15 14:28:22 +0000452option will allow the interpreter path to be explicitly overridden.
453
Georg Brandl3f40c402014-09-21 00:35:08 +0200454The ``scripts`` option simply is a list of files to be handled in this
Georg Brandl116aa622007-08-15 14:28:22 +0000455way. From the PyXML setup script::
456
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000457 setup(...,
458 scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
459 )
Georg Brandl116aa622007-08-15 14:28:22 +0000460
Georg Brandl56a59042009-04-27 08:58:15 +0000461.. versionchanged:: 3.1
462 All the scripts will also be added to the ``MANIFEST`` file if no template is
463 provided. See :ref:`manifest`.
464
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000465
466.. _distutils-installing-package-data:
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468Installing Package Data
469=======================
470
471Often, additional files need to be installed into a package. These files are
472often data that's closely related to the package's implementation, or text files
473containing documentation that might be of interest to programmers using the
474package. These files are called :dfn:`package data`.
475
476Package data can be added to packages using the ``package_data`` keyword
477argument to the :func:`setup` function. The value must be a mapping from
478package name to a list of relative path names that should be copied into the
479package. The paths are interpreted as relative to the directory containing the
480package (information from the ``package_dir`` mapping is used if appropriate);
481that is, the files are expected to be part of the package in the source
482directories. They may contain glob patterns as well.
483
484The path names may contain directory portions; any necessary directories will be
485created in the installation.
486
487For example, if a package should contain a subdirectory with several data files,
488the files can be arranged like this in the source tree::
489
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000490 setup.py
491 src/
492 mypkg/
493 __init__.py
494 module.py
495 data/
496 tables.dat
497 spoons.dat
498 forks.dat
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500The corresponding call to :func:`setup` might be::
501
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000502 setup(...,
503 packages=['mypkg'],
504 package_dir={'mypkg': 'src/mypkg'},
505 package_data={'mypkg': ['data/*.dat']},
506 )
Georg Brandl116aa622007-08-15 14:28:22 +0000507
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Georg Brandl56a59042009-04-27 08:58:15 +0000509.. versionchanged:: 3.1
510 All the files that match ``package_data`` will be added to the ``MANIFEST``
511 file if no template is provided. See :ref:`manifest`.
512
513
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000514.. _distutils-additional-files:
515
Georg Brandl116aa622007-08-15 14:28:22 +0000516Installing Additional Files
517===========================
518
Georg Brandl3f40c402014-09-21 00:35:08 +0200519The ``data_files`` option can be used to specify additional files needed
Georg Brandl116aa622007-08-15 14:28:22 +0000520by the module distribution: configuration files, message catalogs, data files,
521anything which doesn't fit in the previous categories.
522
Georg Brandl3f40c402014-09-21 00:35:08 +0200523``data_files`` specifies a sequence of (*directory*, *files*) pairs in the
Georg Brandl116aa622007-08-15 14:28:22 +0000524following way::
525
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000526 setup(...,
527 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Zhaorong Ma70b80542019-05-08 09:44:01 -0400528 ('config', ['cfg/data.cfg'])],
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000529 )
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Georg Brandl116aa622007-08-15 14:28:22 +0000531Each (*directory*, *files*) pair in the sequence specifies the installation
jdemeyer598e15d2019-01-30 16:49:39 +0100532directory and the files to install there.
533
534Each file name in *files* is interpreted relative to the :file:`setup.py`
535script at the top of the package source distribution. Note that you can
536specify the directory where the data files will be installed, but you cannot
537rename the data files themselves.
538
539The *directory* should be a relative path. It is interpreted relative to the
540installation prefix (Python's ``sys.prefix`` for system installations;
541``site.USER_BASE`` for user installations). Distutils allows *directory* to be
542an absolute installation path, but this is discouraged since it is
543incompatible with the wheel packaging format. No directory information from
544*files* is used to determine the final location of the installed file; only
545the name of the file is used.
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Georg Brandl3f40c402014-09-21 00:35:08 +0200547You can specify the ``data_files`` options as a simple sequence of files
Georg Brandl116aa622007-08-15 14:28:22 +0000548without specifying a target directory, but this is not recommended, and the
549:command:`install` command will print a warning in this case. To install data
550files directly in the target directory, an empty string should be given as the
551directory.
552
Georg Brandl56a59042009-04-27 08:58:15 +0000553.. versionchanged:: 3.1
554 All the files that match ``data_files`` will be added to the ``MANIFEST``
555 file if no template is provided. See :ref:`manifest`.
556
Georg Brandl116aa622007-08-15 14:28:22 +0000557
558.. _meta-data:
559
560Additional meta-data
561====================
562
563The setup script may include additional meta-data beyond the name and version.
564This information includes:
565
566+----------------------+---------------------------+-----------------+--------+
567| Meta-Data | Description | Value | Notes |
568+======================+===========================+=================+========+
569| ``name`` | name of the package | short string | \(1) |
570+----------------------+---------------------------+-----------------+--------+
571| ``version`` | version of this release | short string | (1)(2) |
572+----------------------+---------------------------+-----------------+--------+
573| ``author`` | package author's name | short string | \(3) |
574+----------------------+---------------------------+-----------------+--------+
575| ``author_email`` | email address of the | email address | \(3) |
576| | package author | | |
577+----------------------+---------------------------+-----------------+--------+
578| ``maintainer`` | package maintainer's name | short string | \(3) |
579+----------------------+---------------------------+-----------------+--------+
580| ``maintainer_email`` | email address of the | email address | \(3) |
581| | package maintainer | | |
582+----------------------+---------------------------+-----------------+--------+
583| ``url`` | home page for the package | URL | \(1) |
584+----------------------+---------------------------+-----------------+--------+
585| ``description`` | short, summary | short string | |
586| | description of the | | |
587| | package | | |
588+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300589| ``long_description`` | longer description of the | long string | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +0000590| | package | | |
591+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300592| ``download_url`` | location where the | URL | |
Georg Brandl116aa622007-08-15 14:28:22 +0000593| | package may be downloaded | | |
594+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300595| ``classifiers`` | a list of classifiers | list of strings | (6)(7) |
Georg Brandl116aa622007-08-15 14:28:22 +0000596+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300597| ``platforms`` | a list of platforms | list of strings | (6)(8) |
Benjamin Peterson6ebe78f2008-12-21 00:06:59 +0000598+----------------------+---------------------------+-----------------+--------+
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300599| ``keywords`` | a list of keywords | list of strings | (6)(8) |
600+----------------------+---------------------------+-----------------+--------+
601| ``license`` | license for the package | short string | \(5) |
Tarek Ziadé5e06a652009-06-16 07:43:42 +0000602+----------------------+---------------------------+-----------------+--------+
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604Notes:
605
606(1)
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000607 These fields are required.
Georg Brandl116aa622007-08-15 14:28:22 +0000608
609(2)
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000610 It is recommended that versions take the form *major.minor[.patch[.sub]]*.
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612(3)
Petri Lehtinen905b6482013-02-23 21:05:27 +0100613 Either the author or the maintainer must be identified. If maintainer is
614 provided, distutils lists it as the author in :file:`PKG-INFO`.
Georg Brandl116aa622007-08-15 14:28:22 +0000615
616(4)
Kojo Idrissa1b4abcf2019-05-10 03:45:09 -0500617 The ``long_description`` field is used by PyPI when you publish a package,
618 to build its project page.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300620(5)
Tarek Ziadé5e06a652009-06-16 07:43:42 +0000621 The ``license`` field is a text indicating the license covering the
622 package where the license is not a selection from the "License" Trove
623 classifiers. See the ``Classifier`` field. Notice that
624 there's a ``licence`` distribution option which is deprecated but still
625 acts as an alias for ``license``.
626
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300627(6)
628 This field must be a list.
629
630(7)
631 The valid classifiers are listed on
Stéphane Wirtel19177fb2018-05-15 20:58:35 +0200632 `PyPI <https://pypi.org/classifiers>`_.
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300633
634(8)
635 To preserve backward compatibility, this field also accepts a string. If
636 you pass a comma-separated string ``'foo, bar'``, it will be converted to
637 ``['foo', 'bar']``, Otherwise, it will be converted to a list of one
638 string.
639
Georg Brandl116aa622007-08-15 14:28:22 +0000640'short string'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000641 A single line of text, not more than 200 characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643'long string'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000644 Multiple lines of plain text in reStructuredText format (see
Georg Brandlb7354a62014-10-29 10:57:37 +0100645 http://docutils.sourceforge.net/).
Georg Brandl116aa622007-08-15 14:28:22 +0000646
647'list of strings'
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000648 See below.
Georg Brandl116aa622007-08-15 14:28:22 +0000649
Georg Brandl116aa622007-08-15 14:28:22 +0000650Encoding the version information is an art in itself. Python packages generally
651adhere to the version format *major.minor[.patch][sub]*. The major number is 0
652for initial, experimental releases of software. It is incremented for releases
653that represent major milestones in a package. The minor number is incremented
654when important new features are added to the package. The patch number
655increments when bug-fix releases are made. Additional trailing version
656information is sometimes used to indicate sub-releases. These are
657"a1,a2,...,aN" (for alpha releases, where functionality and API may change),
658"b1,b2,...,bN" (for beta releases, which only fix bugs) and "pr1,pr2,...,prN"
659(for final pre-release release testing). Some examples:
660
6610.1.0
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000662 the first, experimental release of a package
Georg Brandl116aa622007-08-15 14:28:22 +0000663
6641.0.1a2
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000665 the second alpha release of the first patch version of 1.0
Georg Brandl116aa622007-08-15 14:28:22 +0000666
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300667``classifiers`` must be specified in a list::
Georg Brandl116aa622007-08-15 14:28:22 +0000668
Tarek Ziadé3177f2f2009-02-27 02:22:25 +0000669 setup(...,
670 classifiers=[
671 'Development Status :: 4 - Beta',
672 'Environment :: Console',
673 'Environment :: Web Environment',
674 'Intended Audience :: End Users/Desktop',
675 'Intended Audience :: Developers',
676 'Intended Audience :: System Administrators',
677 'License :: OSI Approved :: Python Software Foundation License',
678 'Operating System :: MacOS :: MacOS X',
679 'Operating System :: Microsoft :: Windows',
680 'Operating System :: POSIX',
681 'Programming Language :: Python',
682 'Topic :: Communications :: Email',
683 'Topic :: Office/Business',
684 'Topic :: Software Development :: Bug Tracking',
685 ],
686 )
Georg Brandl116aa622007-08-15 14:28:22 +0000687
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300688.. versionchanged:: 3.7
TilmanKe80e77a2018-10-25 00:50:25 +0200689 :class:`~distutils.core.setup` now warns when ``classifiers``, ``keywords``
690 or ``platforms`` fields are not specified as a list or a string.
Berker Peksagdcaed6b2017-11-23 21:34:20 +0300691
Larry Hastings3732ed22014-03-15 21:13:56 -0700692.. _debug-setup-script:
693
Georg Brandl116aa622007-08-15 14:28:22 +0000694Debugging the setup script
695==========================
696
697Sometimes things go wrong, and the setup script doesn't do what the developer
698wants.
699
700Distutils catches any exceptions when running the setup script, and print a
701simple error message before the script is terminated. The motivation for this
702behaviour is to not confuse administrators who don't know much about Python and
703are trying to install a package. If they get a big long traceback from deep
704inside the guts of Distutils, they may think the package or the Python
705installation is broken because they don't read all the way down to the bottom
706and see that it's a permission problem.
707
708On the other hand, this doesn't help the developer to find the cause of the
Larry Hastings3732ed22014-03-15 21:13:56 -0700709failure. For this purpose, the :envvar:`DISTUTILS_DEBUG` environment variable can be set
Georg Brandl116aa622007-08-15 14:28:22 +0000710to anything except an empty string, and distutils will now print detailed
Larry Hastings3732ed22014-03-15 21:13:56 -0700711information about what it is doing, dump the full traceback when an exception
712occurs, and print the whole command line when an external program (like a C
713compiler) fails.