Éric Araujo | 3a9f58f | 2011-06-01 20:42:49 +0200 | [diff] [blame] | 1 | .. _packaging-setup-script: |
| 2 | |
| 3 | ************************ |
| 4 | Writing the Setup Script |
| 5 | ************************ |
| 6 | |
| 7 | The setup script is the center of all activity in building, distributing, and |
| 8 | installing modules using Distutils. The main purpose of the setup script is |
| 9 | to describe your module distribution to Distutils, so that the various |
| 10 | commands that operate on your modules do the right thing. As we saw in section |
| 11 | :ref:`packaging-simple-example`, the setup script consists mainly of a |
Éric Araujo | 5da37be | 2011-06-01 20:44:40 +0200 | [diff] [blame] | 12 | call to :func:`setup` where the most information is supplied as |
Éric Araujo | 3a9f58f | 2011-06-01 20:42:49 +0200 | [diff] [blame] | 13 | keyword arguments to :func:`setup`. |
| 14 | |
| 15 | Here's a slightly more involved example, which we'll follow for the next couple |
| 16 | of sections: a setup script that could be used for Packaging itself:: |
| 17 | |
| 18 | #!/usr/bin/env python |
| 19 | |
| 20 | from packaging.core import setup, find_packages |
| 21 | |
| 22 | setup(name='Packaging', |
| 23 | version='1.0', |
| 24 | summary='Python Distribution Utilities', |
| 25 | keywords=['packaging', 'packaging'], |
| 26 | author=u'Tarek Ziadé', |
| 27 | author_email='tarek@ziade.org', |
| 28 | home_page='http://bitbucket.org/tarek/packaging/wiki/Home', |
| 29 | license='PSF', |
| 30 | packages=find_packages()) |
| 31 | |
| 32 | |
| 33 | There are only two differences between this and the trivial one-file |
| 34 | distribution presented in section :ref:`packaging-simple-example`: more |
| 35 | metadata and the specification of pure Python modules by package rather than |
| 36 | by module. This is important since Ristutils consist of a couple of dozen |
| 37 | modules split into (so far) two packages; an explicit list of every module |
| 38 | would be tedious to generate and difficult to maintain. For more information |
| 39 | on the additional metadata, see section :ref:`packaging-metadata`. |
| 40 | |
| 41 | Note that any pathnames (files or directories) supplied in the setup script |
| 42 | should be written using the Unix convention, i.e. slash-separated. The |
| 43 | Distutils will take care of converting this platform-neutral representation into |
| 44 | whatever is appropriate on your current platform before actually using the |
| 45 | pathname. This makes your setup script portable across operating systems, which |
| 46 | of course is one of the major goals of the Distutils. In this spirit, all |
| 47 | pathnames in this document are slash-separated. |
| 48 | |
| 49 | This, of course, only applies to pathnames given to Distutils functions. If |
| 50 | you, for example, use standard Python functions such as :func:`glob.glob` or |
| 51 | :func:`os.listdir` to specify files, you should be careful to write portable |
| 52 | code instead of hardcoding path separators:: |
| 53 | |
| 54 | glob.glob(os.path.join('mydir', 'subdir', '*.html')) |
| 55 | os.listdir(os.path.join('mydir', 'subdir')) |
| 56 | |
| 57 | |
| 58 | .. _packaging-listing-packages: |
| 59 | |
| 60 | Listing whole packages |
| 61 | ====================== |
| 62 | |
| 63 | The :option:`packages` option tells the Distutils to process (build, distribute, |
| 64 | install, etc.) all pure Python modules found in each package mentioned in the |
| 65 | :option:`packages` list. In order to do this, of course, there has to be a |
| 66 | correspondence between package names and directories in the filesystem. The |
| 67 | default correspondence is the most obvious one, i.e. package :mod:`packaging` is |
| 68 | found in the directory :file:`packaging` relative to the distribution root. |
| 69 | Thus, when you say ``packages = ['foo']`` in your setup script, you are |
| 70 | promising that the Distutils will find a file :file:`foo/__init__.py` (which |
| 71 | might be spelled differently on your system, but you get the idea) relative to |
| 72 | the directory where your setup script lives. If you break this promise, the |
| 73 | Distutils will issue a warning but still process the broken package anyways. |
| 74 | |
| 75 | If you use a different convention to lay out your source directory, that's no |
| 76 | problem: you just have to supply the :option:`package_dir` option to tell the |
| 77 | Distutils about your convention. For example, say you keep all Python source |
| 78 | under :file:`lib`, so that modules in the "root package" (i.e., not in any |
| 79 | package at all) are in :file:`lib`, modules in the :mod:`foo` package are in |
| 80 | :file:`lib/foo`, and so forth. Then you would put :: |
| 81 | |
| 82 | package_dir = {'': 'lib'} |
| 83 | |
| 84 | in your setup script. The keys to this dictionary are package names, and an |
| 85 | empty package name stands for the root package. The values are directory names |
| 86 | relative to your distribution root. In this case, when you say ``packages = |
| 87 | ['foo']``, you are promising that the file :file:`lib/foo/__init__.py` exists. |
| 88 | |
| 89 | Another possible convention is to put the :mod:`foo` package right in |
| 90 | :file:`lib`, the :mod:`foo.bar` package in :file:`lib/bar`, etc. This would be |
| 91 | written in the setup script as :: |
| 92 | |
| 93 | package_dir = {'foo': 'lib'} |
| 94 | |
| 95 | A ``package: dir`` entry in the :option:`package_dir` dictionary implicitly |
| 96 | applies to all packages below *package*, so the :mod:`foo.bar` case is |
| 97 | automatically handled here. In this example, having ``packages = ['foo', |
| 98 | 'foo.bar']`` tells the Distutils to look for :file:`lib/__init__.py` and |
| 99 | :file:`lib/bar/__init__.py`. (Keep in mind that although :option:`package_dir` |
| 100 | applies recursively, you must explicitly list all packages in |
| 101 | :option:`packages`: the Distutils will *not* recursively scan your source tree |
| 102 | looking for any directory with an :file:`__init__.py` file.) |
| 103 | |
| 104 | |
| 105 | .. _packaging-listing-modules: |
| 106 | |
| 107 | Listing individual modules |
| 108 | ========================== |
| 109 | |
| 110 | For a small module distribution, you might prefer to list all modules rather |
| 111 | than listing packages---especially the case of a single module that goes in the |
| 112 | "root package" (i.e., no package at all). This simplest case was shown in |
| 113 | section :ref:`packaging-simple-example`; here is a slightly more involved |
| 114 | example:: |
| 115 | |
| 116 | py_modules = ['mod1', 'pkg.mod2'] |
| 117 | |
| 118 | This describes two modules, one of them in the "root" package, the other in the |
| 119 | :mod:`pkg` package. Again, the default package/directory layout implies that |
| 120 | these two modules can be found in :file:`mod1.py` and :file:`pkg/mod2.py`, and |
| 121 | that :file:`pkg/__init__.py` exists as well. And again, you can override the |
| 122 | package/directory correspondence using the :option:`package_dir` option. |
| 123 | |
| 124 | |
| 125 | .. _packaging-describing-extensions: |
| 126 | |
| 127 | Describing extension modules |
| 128 | ============================ |
| 129 | |
| 130 | Just as writing Python extension modules is a bit more complicated than writing |
| 131 | pure Python modules, describing them to the Distutils is a bit more complicated. |
| 132 | Unlike pure modules, it's not enough just to list modules or packages and expect |
| 133 | the Distutils to go out and find the right files; you have to specify the |
| 134 | extension name, source file(s), and any compile/link requirements (include |
| 135 | directories, libraries to link with, etc.). |
| 136 | |
| 137 | .. XXX read over this section |
| 138 | |
| 139 | All of this is done through another keyword argument to :func:`setup`, the |
| 140 | :option:`ext_modules` option. :option:`ext_modules` is just a list of |
| 141 | :class:`Extension` instances, each of which describes a single extension module. |
| 142 | Suppose your distribution includes a single extension, called :mod:`foo` and |
| 143 | implemented by :file:`foo.c`. If no additional instructions to the |
| 144 | compiler/linker are needed, describing this extension is quite simple:: |
| 145 | |
| 146 | Extension('foo', ['foo.c']) |
| 147 | |
| 148 | The :class:`Extension` class can be imported from :mod:`packaging.core` along |
| 149 | with :func:`setup`. Thus, the setup script for a module distribution that |
| 150 | contains only this one extension and nothing else might be:: |
| 151 | |
| 152 | from packaging.core import setup, Extension |
| 153 | setup(name='foo', |
| 154 | version='1.0', |
| 155 | ext_modules=[Extension('foo', ['foo.c'])]) |
| 156 | |
| 157 | The :class:`Extension` class (actually, the underlying extension-building |
| 158 | machinery implemented by the :command:`build_ext` command) supports a great deal |
| 159 | of flexibility in describing Python extensions, which is explained in the |
| 160 | following sections. |
| 161 | |
| 162 | |
| 163 | Extension names and packages |
| 164 | ---------------------------- |
| 165 | |
| 166 | The first argument to the :class:`Extension` constructor is always the name of |
| 167 | the extension, including any package names. For example, :: |
| 168 | |
| 169 | Extension('foo', ['src/foo1.c', 'src/foo2.c']) |
| 170 | |
| 171 | describes an extension that lives in the root package, while :: |
| 172 | |
| 173 | Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c']) |
| 174 | |
| 175 | describes the same extension in the :mod:`pkg` package. The source files and |
| 176 | resulting object code are identical in both cases; the only difference is where |
| 177 | in the filesystem (and therefore where in Python's namespace hierarchy) the |
| 178 | resulting extension lives. |
| 179 | |
| 180 | If you have a number of extensions all in the same package (or all under the |
| 181 | same base package), use the :option:`ext_package` keyword argument to |
| 182 | :func:`setup`. For example, :: |
| 183 | |
| 184 | setup(..., |
| 185 | ext_package='pkg', |
| 186 | ext_modules=[Extension('foo', ['foo.c']), |
| 187 | Extension('subpkg.bar', ['bar.c'])]) |
| 188 | |
| 189 | will compile :file:`foo.c` to the extension :mod:`pkg.foo`, and :file:`bar.c` to |
| 190 | :mod:`pkg.subpkg.bar`. |
| 191 | |
| 192 | |
| 193 | Extension source files |
| 194 | ---------------------- |
| 195 | |
| 196 | The second argument to the :class:`Extension` constructor is a list of source |
| 197 | files. Since the Distutils currently only support C, C++, and Objective-C |
| 198 | extensions, these are normally C/C++/Objective-C source files. (Be sure to use |
| 199 | appropriate extensions to distinguish C++\ source files: :file:`.cc` and |
| 200 | :file:`.cpp` seem to be recognized by both Unix and Windows compilers.) |
| 201 | |
| 202 | However, you can also include SWIG interface (:file:`.i`) files in the list; the |
| 203 | :command:`build_ext` command knows how to deal with SWIG extensions: it will run |
| 204 | SWIG on the interface file and compile the resulting C/C++ file into your |
| 205 | extension. |
| 206 | |
| 207 | .. XXX SWIG support is rough around the edges and largely untested! |
| 208 | |
| 209 | This warning notwithstanding, options to SWIG can be currently passed like |
| 210 | this:: |
| 211 | |
| 212 | setup(..., |
| 213 | ext_modules=[Extension('_foo', ['foo.i'], |
| 214 | swig_opts=['-modern', '-I../include'])], |
| 215 | py_modules=['foo']) |
| 216 | |
| 217 | Or on the command line like this:: |
| 218 | |
| 219 | > python setup.py build_ext --swig-opts="-modern -I../include" |
| 220 | |
| 221 | On some platforms, you can include non-source files that are processed by the |
| 222 | compiler and included in your extension. Currently, this just means Windows |
| 223 | message text (:file:`.mc`) files and resource definition (:file:`.rc`) files for |
| 224 | Visual C++. These will be compiled to binary resource (:file:`.res`) files and |
| 225 | linked into the executable. |
| 226 | |
| 227 | |
| 228 | Preprocessor options |
| 229 | -------------------- |
| 230 | |
| 231 | Three optional arguments to :class:`Extension` will help if you need to specify |
| 232 | include directories to search or preprocessor macros to define/undefine: |
| 233 | ``include_dirs``, ``define_macros``, and ``undef_macros``. |
| 234 | |
| 235 | For example, if your extension requires header files in the :file:`include` |
| 236 | directory under your distribution root, use the ``include_dirs`` option:: |
| 237 | |
| 238 | Extension('foo', ['foo.c'], include_dirs=['include']) |
| 239 | |
| 240 | You can specify absolute directories there; if you know that your extension will |
| 241 | only be built on Unix systems with X11R6 installed to :file:`/usr`, you can get |
| 242 | away with :: |
| 243 | |
| 244 | Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11']) |
| 245 | |
| 246 | You should avoid this sort of non-portable usage if you plan to distribute your |
| 247 | code: it's probably better to write C code like :: |
| 248 | |
| 249 | #include <X11/Xlib.h> |
| 250 | |
| 251 | If you need to include header files from some other Python extension, you can |
| 252 | take advantage of the fact that header files are installed in a consistent way |
| 253 | by the Distutils :command:`install_header` command. For example, the Numerical |
| 254 | Python header files are installed (on a standard Unix installation) to |
| 255 | :file:`/usr/local/include/python1.5/Numerical`. (The exact location will differ |
| 256 | according to your platform and Python installation.) Since the Python include |
| 257 | directory---\ :file:`/usr/local/include/python1.5` in this case---is always |
| 258 | included in the search path when building Python extensions, the best approach |
| 259 | is to write C code like :: |
| 260 | |
| 261 | #include <Numerical/arrayobject.h> |
| 262 | |
| 263 | .. TODO check if it's d2.sysconfig or the new sysconfig module now |
| 264 | |
| 265 | If you must put the :file:`Numerical` include directory right into your header |
| 266 | search path, though, you can find that directory using the Distutils |
| 267 | :mod:`packaging.sysconfig` module:: |
| 268 | |
| 269 | from packaging.sysconfig import get_python_inc |
| 270 | incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical') |
| 271 | setup(..., |
| 272 | Extension(..., include_dirs=[incdir])) |
| 273 | |
| 274 | Even though this is quite portable---it will work on any Python installation, |
| 275 | regardless of platform---it's probably easier to just write your C code in the |
| 276 | sensible way. |
| 277 | |
| 278 | You can define and undefine preprocessor macros with the ``define_macros`` and |
| 279 | ``undef_macros`` options. ``define_macros`` takes a list of ``(name, value)`` |
| 280 | tuples, where ``name`` is the name of the macro to define (a string) and |
| 281 | ``value`` is its value: either a string or ``None``. (Defining a macro ``FOO`` |
| 282 | to ``None`` is the equivalent of a bare ``#define FOO`` in your C source: with |
| 283 | most compilers, this sets ``FOO`` to the string ``1``.) ``undef_macros`` is |
| 284 | just a list of macros to undefine. |
| 285 | |
| 286 | For example:: |
| 287 | |
| 288 | Extension(..., |
| 289 | define_macros=[('NDEBUG', '1'), |
| 290 | ('HAVE_STRFTIME', None)], |
| 291 | undef_macros=['HAVE_FOO', 'HAVE_BAR']) |
| 292 | |
| 293 | is the equivalent of having this at the top of every C source file:: |
| 294 | |
| 295 | #define NDEBUG 1 |
| 296 | #define HAVE_STRFTIME |
| 297 | #undef HAVE_FOO |
| 298 | #undef HAVE_BAR |
| 299 | |
| 300 | |
| 301 | Library options |
| 302 | --------------- |
| 303 | |
| 304 | You can also specify the libraries to link against when building your extension, |
| 305 | and the directories to search for those libraries. The ``libraries`` option is |
| 306 | a list of libraries to link against, ``library_dirs`` is a list of directories |
| 307 | to search for libraries at link-time, and ``runtime_library_dirs`` is a list of |
| 308 | directories to search for shared (dynamically loaded) libraries at run-time. |
| 309 | |
| 310 | For example, if you need to link against libraries known to be in the standard |
| 311 | library search path on target systems :: |
| 312 | |
| 313 | Extension(..., |
| 314 | libraries=['gdbm', 'readline']) |
| 315 | |
| 316 | If you need to link with libraries in a non-standard location, you'll have to |
| 317 | include the location in ``library_dirs``:: |
| 318 | |
| 319 | Extension(..., |
| 320 | library_dirs=['/usr/X11R6/lib'], |
| 321 | libraries=['X11', 'Xt']) |
| 322 | |
| 323 | (Again, this sort of non-portable construct should be avoided if you intend to |
| 324 | distribute your code.) |
| 325 | |
| 326 | .. XXX Should mention clib libraries here or somewhere else! |
| 327 | |
| 328 | |
| 329 | Other options |
| 330 | ------------- |
| 331 | |
| 332 | There are still some other options which can be used to handle special cases. |
| 333 | |
| 334 | The :option:`optional` option is a boolean; if it is true, |
| 335 | a build failure in the extension will not abort the build process, but |
| 336 | instead simply not install the failing extension. |
| 337 | |
| 338 | The :option:`extra_objects` option is a list of object files to be passed to the |
| 339 | linker. These files must not have extensions, as the default extension for the |
| 340 | compiler is used. |
| 341 | |
| 342 | :option:`extra_compile_args` and :option:`extra_link_args` can be used to |
| 343 | specify additional command-line options for the respective compiler and linker |
| 344 | command lines. |
| 345 | |
| 346 | :option:`export_symbols` is only useful on Windows. It can contain a list of |
| 347 | symbols (functions or variables) to be exported. This option is not needed when |
| 348 | building compiled extensions: Distutils will automatically add ``initmodule`` |
| 349 | to the list of exported symbols. |
| 350 | |
| 351 | The :option:`depends` option is a list of files that the extension depends on |
| 352 | (for example header files). The build command will call the compiler on the |
| 353 | sources to rebuild extension if any on this files has been modified since the |
| 354 | previous build. |
| 355 | |
| 356 | Relationships between Distributions and Packages |
| 357 | ================================================ |
| 358 | |
| 359 | .. FIXME rewrite to update to PEP 345 (but without dist/release confusion) |
| 360 | |
| 361 | A distribution may relate to packages in three specific ways: |
| 362 | |
| 363 | #. It can require packages or modules. |
| 364 | |
| 365 | #. It can provide packages or modules. |
| 366 | |
| 367 | #. It can obsolete packages or modules. |
| 368 | |
| 369 | These relationships can be specified using keyword arguments to the |
| 370 | :func:`packaging.core.setup` function. |
| 371 | |
| 372 | Dependencies on other Python modules and packages can be specified by supplying |
| 373 | the *requires* keyword argument to :func:`setup`. The value must be a list of |
| 374 | strings. Each string specifies a package that is required, and optionally what |
| 375 | versions are sufficient. |
| 376 | |
| 377 | To specify that any version of a module or package is required, the string |
| 378 | should consist entirely of the module or package name. Examples include |
| 379 | ``'mymodule'`` and ``'xml.parsers.expat'``. |
| 380 | |
| 381 | If specific versions are required, a sequence of qualifiers can be supplied in |
| 382 | parentheses. Each qualifier may consist of a comparison operator and a version |
| 383 | number. The accepted comparison operators are:: |
| 384 | |
| 385 | < > == |
| 386 | <= >= != |
| 387 | |
| 388 | These can be combined by using multiple qualifiers separated by commas (and |
| 389 | optional whitespace). In this case, all of the qualifiers must be matched; a |
| 390 | logical AND is used to combine the evaluations. |
| 391 | |
| 392 | Let's look at a bunch of examples: |
| 393 | |
| 394 | +-------------------------+----------------------------------------------+ |
| 395 | | Requires Expression | Explanation | |
| 396 | +=========================+==============================================+ |
| 397 | | ``==1.0`` | Only version ``1.0`` is compatible | |
| 398 | +-------------------------+----------------------------------------------+ |
| 399 | | ``>1.0, !=1.5.1, <2.0`` | Any version after ``1.0`` and before ``2.0`` | |
| 400 | | | is compatible, except ``1.5.1`` | |
| 401 | +-------------------------+----------------------------------------------+ |
| 402 | |
| 403 | Now that we can specify dependencies, we also need to be able to specify what we |
| 404 | provide that other distributions can require. This is done using the *provides* |
| 405 | keyword argument to :func:`setup`. The value for this keyword is a list of |
| 406 | strings, each of which names a Python module or package, and optionally |
| 407 | identifies the version. If the version is not specified, it is assumed to match |
| 408 | that of the distribution. |
| 409 | |
| 410 | Some examples: |
| 411 | |
| 412 | +---------------------+----------------------------------------------+ |
| 413 | | Provides Expression | Explanation | |
| 414 | +=====================+==============================================+ |
| 415 | | ``mypkg`` | Provide ``mypkg``, using the distribution | |
| 416 | | | version | |
| 417 | +---------------------+----------------------------------------------+ |
| 418 | | ``mypkg (1.1)`` | Provide ``mypkg`` version 1.1, regardless of | |
| 419 | | | the distribution version | |
| 420 | +---------------------+----------------------------------------------+ |
| 421 | |
| 422 | A package can declare that it obsoletes other packages using the *obsoletes* |
| 423 | keyword argument. The value for this is similar to that of the *requires* |
| 424 | keyword: a list of strings giving module or package specifiers. Each specifier |
| 425 | consists of a module or package name optionally followed by one or more version |
| 426 | qualifiers. Version qualifiers are given in parentheses after the module or |
| 427 | package name. |
| 428 | |
| 429 | The versions identified by the qualifiers are those that are obsoleted by the |
| 430 | distribution being described. If no qualifiers are given, all versions of the |
| 431 | named module or package are understood to be obsoleted. |
| 432 | |
| 433 | .. _packaging-installing-scripts: |
| 434 | |
| 435 | Installing Scripts |
| 436 | ================== |
| 437 | |
| 438 | So far we have been dealing with pure and non-pure Python modules, which are |
| 439 | usually not run by themselves but imported by scripts. |
| 440 | |
| 441 | Scripts are files containing Python source code, intended to be started from the |
| 442 | command line. Scripts don't require Distutils to do anything very complicated. |
| 443 | The only clever feature is that if the first line of the script starts with |
| 444 | ``#!`` and contains the word "python", the Distutils will adjust the first line |
| 445 | to refer to the current interpreter location. By default, it is replaced with |
| 446 | the current interpreter location. The :option:`--executable` (or :option:`-e`) |
| 447 | option will allow the interpreter path to be explicitly overridden. |
| 448 | |
| 449 | The :option:`scripts` option simply is a list of files to be handled in this |
| 450 | way. From the PyXML setup script:: |
| 451 | |
| 452 | setup(..., |
| 453 | scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']) |
| 454 | |
| 455 | All the scripts will also be added to the ``MANIFEST`` file if no template is |
| 456 | provided. See :ref:`packaging-manifest`. |
| 457 | |
| 458 | .. _packaging-installing-package-data: |
| 459 | |
| 460 | Installing Package Data |
| 461 | ======================= |
| 462 | |
| 463 | Often, additional files need to be installed into a package. These files are |
| 464 | often data that's closely related to the package's implementation, or text files |
| 465 | containing documentation that might be of interest to programmers using the |
| 466 | package. These files are called :dfn:`package data`. |
| 467 | |
| 468 | Package data can be added to packages using the ``package_data`` keyword |
| 469 | argument to the :func:`setup` function. The value must be a mapping from |
| 470 | package name to a list of relative path names that should be copied into the |
| 471 | package. The paths are interpreted as relative to the directory containing the |
| 472 | package (information from the ``package_dir`` mapping is used if appropriate); |
| 473 | that is, the files are expected to be part of the package in the source |
| 474 | directories. They may contain glob patterns as well. |
| 475 | |
| 476 | The path names may contain directory portions; any necessary directories will be |
| 477 | created in the installation. |
| 478 | |
| 479 | For example, if a package should contain a subdirectory with several data files, |
| 480 | the files can be arranged like this in the source tree:: |
| 481 | |
| 482 | setup.py |
| 483 | src/ |
| 484 | mypkg/ |
| 485 | __init__.py |
| 486 | module.py |
| 487 | data/ |
| 488 | tables.dat |
| 489 | spoons.dat |
| 490 | forks.dat |
| 491 | |
| 492 | The corresponding call to :func:`setup` might be:: |
| 493 | |
| 494 | setup(..., |
| 495 | packages=['mypkg'], |
| 496 | package_dir={'mypkg': 'src/mypkg'}, |
| 497 | package_data={'mypkg': ['data/*.dat']}) |
| 498 | |
| 499 | |
| 500 | All the files that match ``package_data`` will be added to the ``MANIFEST`` |
| 501 | file if no template is provided. See :ref:`packaging-manifest`. |
| 502 | |
| 503 | |
| 504 | .. _packaging-additional-files: |
| 505 | |
| 506 | Installing Additional Files |
| 507 | =========================== |
| 508 | |
| 509 | The :option:`data_files` option can be used to specify additional files needed |
| 510 | by the module distribution: configuration files, message catalogs, data files, |
| 511 | anything which doesn't fit in the previous categories. |
| 512 | |
| 513 | :option:`data_files` specifies a sequence of (*directory*, *files*) pairs in the |
| 514 | following way:: |
| 515 | |
| 516 | setup(..., |
| 517 | data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']), |
| 518 | ('config', ['cfg/data.cfg']), |
| 519 | ('/etc/init.d', ['init-script'])]) |
| 520 | |
| 521 | Note that you can specify the directory names where the data files will be |
| 522 | installed, but you cannot rename the data files themselves. |
| 523 | |
| 524 | Each (*directory*, *files*) pair in the sequence specifies the installation |
| 525 | directory and the files to install there. If *directory* is a relative path, it |
| 526 | is interpreted relative to the installation prefix (Python's ``sys.prefix`` for |
| 527 | pure-Python packages, ``sys.exec_prefix`` for packages that contain extension |
| 528 | modules). Each file name in *files* is interpreted relative to the |
| 529 | :file:`setup.py` script at the top of the package source distribution. No |
| 530 | directory information from *files* is used to determine the final location of |
| 531 | the installed file; only the name of the file is used. |
| 532 | |
| 533 | You can specify the :option:`data_files` options as a simple sequence of files |
| 534 | without specifying a target directory, but this is not recommended, and the |
| 535 | :command:`install_dist` command will print a warning in this case. To install data |
| 536 | files directly in the target directory, an empty string should be given as the |
| 537 | directory. |
| 538 | |
| 539 | All the files that match ``data_files`` will be added to the ``MANIFEST`` file |
| 540 | if no template is provided. See :ref:`packaging-manifest`. |
| 541 | |
| 542 | |
| 543 | |
| 544 | .. _packaging-metadata: |
| 545 | |
| 546 | Metadata reference |
| 547 | ================== |
| 548 | |
| 549 | The setup script may include additional metadata beyond the name and version. |
| 550 | This table describes required and additional information: |
| 551 | |
| 552 | .. TODO synchronize with setupcfg; link to it (but don't remove it, it's a |
| 553 | useful summary) |
| 554 | |
| 555 | +----------------------+---------------------------+-----------------+--------+ |
| 556 | | Meta-Data | Description | Value | Notes | |
| 557 | +======================+===========================+=================+========+ |
| 558 | | ``name`` | name of the project | short string | \(1) | |
| 559 | +----------------------+---------------------------+-----------------+--------+ |
| 560 | | ``version`` | version of this release | short string | (1)(2) | |
| 561 | +----------------------+---------------------------+-----------------+--------+ |
| 562 | | ``author`` | project author's name | short string | \(3) | |
| 563 | +----------------------+---------------------------+-----------------+--------+ |
| 564 | | ``author_email`` | email address of the | email address | \(3) | |
| 565 | | | project author | | | |
| 566 | +----------------------+---------------------------+-----------------+--------+ |
| 567 | | ``maintainer`` | project maintainer's name | short string | \(3) | |
| 568 | +----------------------+---------------------------+-----------------+--------+ |
| 569 | | ``maintainer_email`` | email address of the | email address | \(3) | |
| 570 | | | project maintainer | | | |
| 571 | +----------------------+---------------------------+-----------------+--------+ |
| 572 | | ``home_page`` | home page for the project | URL | \(1) | |
| 573 | +----------------------+---------------------------+-----------------+--------+ |
| 574 | | ``summary`` | short description of the | short string | | |
| 575 | | | project | | | |
| 576 | +----------------------+---------------------------+-----------------+--------+ |
| 577 | | ``description`` | longer description of the | long string | \(5) | |
| 578 | | | project | | | |
| 579 | +----------------------+---------------------------+-----------------+--------+ |
| 580 | | ``download_url`` | location where the | URL | | |
| 581 | | | project may be downloaded | | | |
| 582 | +----------------------+---------------------------+-----------------+--------+ |
| 583 | | ``classifiers`` | a list of classifiers | list of strings | \(4) | |
| 584 | +----------------------+---------------------------+-----------------+--------+ |
| 585 | | ``platforms`` | a list of platforms | list of strings | | |
| 586 | +----------------------+---------------------------+-----------------+--------+ |
| 587 | | ``license`` | license for the release | short string | \(6) | |
| 588 | +----------------------+---------------------------+-----------------+--------+ |
| 589 | |
| 590 | Notes: |
| 591 | |
| 592 | (1) |
| 593 | These fields are required. |
| 594 | |
| 595 | (2) |
| 596 | It is recommended that versions take the form *major.minor[.patch[.sub]]*. |
| 597 | |
| 598 | (3) |
| 599 | Either the author or the maintainer must be identified. |
| 600 | |
| 601 | (4) |
| 602 | The list of classifiers is available from the `PyPI website |
| 603 | <http://pypi.python.org/pypi>`_. See also :mod:`packaging.create`. |
| 604 | |
| 605 | (5) |
| 606 | The ``description`` field is used by PyPI when you are registering a |
| 607 | release, to build its PyPI page. |
| 608 | |
| 609 | (6) |
| 610 | The ``license`` field is a text indicating the license covering the |
| 611 | distribution where the license is not a selection from the "License" Trove |
| 612 | classifiers. See the ``Classifier`` field. Notice that |
| 613 | there's a ``licence`` distribution option which is deprecated but still |
| 614 | acts as an alias for ``license``. |
| 615 | |
| 616 | 'short string' |
| 617 | A single line of text, not more than 200 characters. |
| 618 | |
| 619 | 'long string' |
| 620 | Multiple lines of plain text in reStructuredText format (see |
| 621 | http://docutils.sf.net/). |
| 622 | |
| 623 | 'list of strings' |
| 624 | See below. |
| 625 | |
| 626 | In Python 2.x, "string value" means a unicode object. If a byte string (str or |
| 627 | bytes) is given, it has to be valid ASCII. |
| 628 | |
| 629 | .. TODO move this section to the version document, keep a summary, add a link |
| 630 | |
| 631 | Encoding the version information is an art in itself. Python projects generally |
| 632 | adhere to the version format *major.minor[.patch][sub]*. The major number is 0 |
| 633 | for initial, experimental releases of software. It is incremented for releases |
| 634 | that represent major milestones in a project. The minor number is incremented |
| 635 | when important new features are added to the project. The patch number |
| 636 | increments when bug-fix releases are made. Additional trailing version |
| 637 | information is sometimes used to indicate sub-releases. These are |
| 638 | "a1,a2,...,aN" (for alpha releases, where functionality and API may change), |
| 639 | "b1,b2,...,bN" (for beta releases, which only fix bugs) and "pr1,pr2,...,prN" |
| 640 | (for final pre-release release testing). Some examples: |
| 641 | |
| 642 | 0.1.0 |
| 643 | the first, experimental release of a project |
| 644 | |
| 645 | 1.0.1a2 |
| 646 | the second alpha release of the first patch version of 1.0 |
| 647 | |
| 648 | :option:`classifiers` are specified in a Python list:: |
| 649 | |
| 650 | setup(..., |
| 651 | classifiers=[ |
| 652 | 'Development Status :: 4 - Beta', |
| 653 | 'Environment :: Console', |
| 654 | 'Environment :: Web Environment', |
| 655 | 'Intended Audience :: End Users/Desktop', |
| 656 | 'Intended Audience :: Developers', |
| 657 | 'Intended Audience :: System Administrators', |
| 658 | 'License :: OSI Approved :: Python Software Foundation License', |
| 659 | 'Operating System :: MacOS :: MacOS X', |
| 660 | 'Operating System :: Microsoft :: Windows', |
| 661 | 'Operating System :: POSIX', |
| 662 | 'Programming Language :: Python', |
| 663 | 'Topic :: Communications :: Email', |
| 664 | 'Topic :: Office/Business', |
| 665 | 'Topic :: Software Development :: Bug Tracking', |
| 666 | ]) |
| 667 | |
| 668 | |
| 669 | Debugging the setup script |
| 670 | ========================== |
| 671 | |
| 672 | Sometimes things go wrong, and the setup script doesn't do what the developer |
| 673 | wants. |
| 674 | |
| 675 | Distutils catches any exceptions when running the setup script, and print a |
| 676 | simple error message before the script is terminated. The motivation for this |
| 677 | behaviour is to not confuse administrators who don't know much about Python and |
| 678 | are trying to install a project. If they get a big long traceback from deep |
| 679 | inside the guts of Distutils, they may think the project or the Python |
| 680 | installation is broken because they don't read all the way down to the bottom |
| 681 | and see that it's a permission problem. |
| 682 | |
| 683 | .. FIXME DISTUTILS_DEBUG is dead, document logging/warnings here |
| 684 | |
| 685 | On the other hand, this doesn't help the developer to find the cause of the |
| 686 | failure. For this purpose, the DISTUTILS_DEBUG environment variable can be set |
| 687 | to anything except an empty string, and Packaging will now print detailed |
| 688 | information about what it is doing, and prints the full traceback in case an |
| 689 | exception occurs. |