Éric Araujo | 3a9f58f | 2011-06-01 20:42:49 +0200 | [diff] [blame] | 1 | .. _packaging-setup-config: |
| 2 | |
| 3 | ************************************ |
| 4 | Writing the Setup Configuration File |
| 5 | ************************************ |
| 6 | |
| 7 | Often, it's not possible to write down everything needed to build a distribution |
| 8 | *a priori*: you may need to get some information from the user, or from the |
| 9 | user's system, in order to proceed. As long as that information is fairly |
| 10 | simple---a list of directories to search for C header files or libraries, for |
| 11 | example---then providing a configuration file, :file:`setup.cfg`, for users to |
| 12 | edit is a cheap and easy way to solicit it. Configuration files also let you |
| 13 | provide default values for any command option, which the installer can then |
| 14 | override either on the command line or by editing the config file. |
| 15 | |
| 16 | The setup configuration file is a useful middle-ground between the setup script |
| 17 | ---which, ideally, would be opaque to installers [#]_---and the command line to |
| 18 | the setup script, which is outside of your control and entirely up to the |
| 19 | installer. In fact, :file:`setup.cfg` (and any other Distutils configuration |
| 20 | files present on the target system) are processed after the contents of the |
| 21 | setup script, but before the command line. This has several useful |
| 22 | consequences: |
| 23 | |
| 24 | .. If you have more advanced needs, such as determining which extensions to |
| 25 | build based on what capabilities are present on the target system, then you |
| 26 | need the Distutils auto-configuration facility. This started to appear in |
| 27 | Distutils 0.9 but, as of this writing, isn't mature or stable enough yet |
| 28 | for real-world use. |
| 29 | |
| 30 | * installers can override some of what you put in :file:`setup.py` by editing |
| 31 | :file:`setup.cfg` |
| 32 | |
| 33 | * you can provide non-standard defaults for options that are not easily set in |
| 34 | :file:`setup.py` |
| 35 | |
| 36 | * installers can override anything in :file:`setup.cfg` using the command-line |
| 37 | options to :file:`setup.py` |
| 38 | |
| 39 | The basic syntax of the configuration file is simple:: |
| 40 | |
| 41 | [command] |
| 42 | option = value |
| 43 | ... |
| 44 | |
| 45 | where *command* is one of the Distutils commands (e.g. :command:`build_py`, |
| 46 | :command:`install_dist`), and *option* is one of the options that command supports. |
| 47 | Any number of options can be supplied for each command, and any number of |
| 48 | command sections can be included in the file. Blank lines are ignored, as are |
| 49 | comments, which run from a ``'#'`` character until the end of the line. Long |
| 50 | option values can be split across multiple lines simply by indenting the |
| 51 | continuation lines. |
| 52 | |
| 53 | You can find out the list of options supported by a particular command with the |
| 54 | universal :option:`--help` option, e.g. :: |
| 55 | |
| 56 | > python setup.py --help build_ext |
| 57 | [...] |
| 58 | Options for 'build_ext' command: |
| 59 | --build-lib (-b) directory for compiled extension modules |
| 60 | --build-temp (-t) directory for temporary files (build by-products) |
| 61 | --inplace (-i) ignore build-lib and put compiled extensions into the |
| 62 | source directory alongside your pure Python modules |
| 63 | --include-dirs (-I) list of directories to search for header files |
| 64 | --define (-D) C preprocessor macros to define |
| 65 | --undef (-U) C preprocessor macros to undefine |
| 66 | --swig-opts list of SWIG command-line options |
| 67 | [...] |
| 68 | |
| 69 | .. XXX do we want to support ``setup.py --help metadata``? |
| 70 | |
| 71 | Note that an option spelled :option:`--foo-bar` on the command line is spelled |
| 72 | :option:`foo_bar` in configuration files. |
| 73 | |
| 74 | For example, say you want your extensions to be built "in-place"---that is, you |
| 75 | have an extension :mod:`pkg.ext`, and you want the compiled extension file |
| 76 | (:file:`ext.so` on Unix, say) to be put in the same source directory as your |
| 77 | pure Python modules :mod:`pkg.mod1` and :mod:`pkg.mod2`. You can always use the |
| 78 | :option:`--inplace` option on the command line to ensure this:: |
| 79 | |
| 80 | python setup.py build_ext --inplace |
| 81 | |
| 82 | But this requires that you always specify the :command:`build_ext` command |
| 83 | explicitly, and remember to provide :option:`--inplace`. An easier way is to |
| 84 | "set and forget" this option, by encoding it in :file:`setup.cfg`, the |
| 85 | configuration file for this distribution:: |
| 86 | |
| 87 | [build_ext] |
| 88 | inplace = 1 |
| 89 | |
| 90 | This will affect all builds of this module distribution, whether or not you |
| 91 | explicitly specify :command:`build_ext`. If you include :file:`setup.cfg` in |
| 92 | your source distribution, it will also affect end-user builds---which is |
| 93 | probably a bad idea for this option, since always building extensions in-place |
| 94 | would break installation of the module distribution. In certain peculiar cases, |
| 95 | though, modules are built right in their installation directory, so this is |
| 96 | conceivably a useful ability. (Distributing extensions that expect to be built |
| 97 | in their installation directory is almost always a bad idea, though.) |
| 98 | |
| 99 | Another example: certain commands take options that vary from project to |
| 100 | project but not depending on the installation system, for example, |
| 101 | :command:`test` needs to know where your test suite is located and what test |
| 102 | runner to use; likewise, :command:`upload_docs` can find HTML documentation in |
| 103 | a :file:`doc` or :file:`docs` directory, but needs an option to find files in |
| 104 | :file:`docs/build/html`. Instead of having to type out these options each |
| 105 | time you want to run the command, you can put them in the project's |
| 106 | :file:`setup.cfg`:: |
| 107 | |
| 108 | [test] |
| 109 | suite = packaging.tests |
| 110 | |
| 111 | [upload_docs] |
| 112 | upload-dir = docs/build/html |
| 113 | |
| 114 | |
| 115 | .. seealso:: |
| 116 | |
| 117 | :ref:`packaging-config-syntax` in "Installing Python Projects" |
| 118 | More information on the configuration files is available in the manual for |
| 119 | system administrators. |
| 120 | |
| 121 | |
| 122 | .. rubric:: Footnotes |
| 123 | |
| 124 | .. [#] This ideal probably won't be achieved until auto-configuration is fully |
| 125 | supported by the Distutils. |