Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _source-dist: |
| 2 | |
| 3 | ****************************** |
| 4 | Creating a Source Distribution |
| 5 | ****************************** |
| 6 | |
Nick Coghlan | dae1229 | 2019-05-14 22:04:30 +1000 | [diff] [blame] | 7 | .. include:: ./_setuptools_disclaimer.rst |
| 8 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | As shown in section :ref:`distutils-simple-example`, you use the :command:`sdist` command |
| 10 | to create a source distribution. In the simplest case, :: |
| 11 | |
| 12 | python setup.py sdist |
| 13 | |
| 14 | (assuming you haven't specified any :command:`sdist` options in the setup script |
| 15 | or config file), :command:`sdist` creates the archive of the default format for |
| 16 | the current platform. The default format is a gzip'ed tar file |
| 17 | (:file:`.tar.gz`) on Unix, and ZIP file on Windows. |
| 18 | |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 19 | You can specify as many formats as you like using the :option:`!--formats` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | option, for example:: |
| 21 | |
| 22 | python setup.py sdist --formats=gztar,zip |
| 23 | |
| 24 | to create a gzipped tarball and a zip file. The available formats are: |
| 25 | |
Miss Islington (bot) | dbd1dc2 | 2021-12-17 15:32:02 -0800 | [diff] [blame^] | 26 | +-----------+-------------------------+-------------+ |
| 27 | | Format | Description | Notes | |
| 28 | +===========+=========================+=============+ |
| 29 | | ``zip`` | zip file (:file:`.zip`) | (1),(3) | |
| 30 | +-----------+-------------------------+-------------+ |
| 31 | | ``gztar`` | gzip'ed tar file | \(2) | |
| 32 | | | (:file:`.tar.gz`) | | |
| 33 | +-----------+-------------------------+-------------+ |
| 34 | | ``bztar`` | bzip2'ed tar file | \(5) | |
| 35 | | | (:file:`.tar.bz2`) | | |
| 36 | +-----------+-------------------------+-------------+ |
| 37 | | ``xztar`` | xz'ed tar file | \(5) | |
| 38 | | | (:file:`.tar.xz`) | | |
| 39 | +-----------+-------------------------+-------------+ |
| 40 | | ``ztar`` | compressed tar file | (4),(5) | |
| 41 | | | (:file:`.tar.Z`) | | |
| 42 | +-----------+-------------------------+-------------+ |
| 43 | | ``tar`` | tar file (:file:`.tar`) | \(5) | |
| 44 | +-----------+-------------------------+-------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
Zachary Ware | 7f142c7 | 2015-07-07 00:11:36 -0500 | [diff] [blame] | 46 | .. versionchanged:: 3.5 |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 47 | Added support for the ``xztar`` format. |
| 48 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | Notes: |
| 50 | |
| 51 | (1) |
| 52 | default on Windows |
| 53 | |
| 54 | (2) |
| 55 | default on Unix |
| 56 | |
| 57 | (3) |
| 58 | requires either external :program:`zip` utility or :mod:`zipfile` module (part |
| 59 | of the standard Python library since Python 1.6) |
| 60 | |
| 61 | (4) |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 62 | requires the :program:`compress` program. Notice that this format is now |
| 63 | pending for deprecation and will be removed in the future versions of Python. |
Miss Islington (bot) | dbd1dc2 | 2021-12-17 15:32:02 -0800 | [diff] [blame^] | 64 | (5) |
| 65 | deprecated by `PEP 527 <https://www.python.org/dev/peps/pep-0527/>`_; |
| 66 | `PyPI <https://pypi.org>`_ only accepts ``.zip`` and ``.tar.gz`` files. |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 67 | |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 68 | When using any ``tar`` format (``gztar``, ``bztar``, ``xztar``, ``ztar`` or |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 69 | ``tar``), under Unix you can specify the ``owner`` and ``group`` names |
| 70 | that will be set for each member of the archive. |
| 71 | |
| 72 | For example, if you want all files of the archive to be owned by root:: |
| 73 | |
| 74 | python setup.py sdist --owner=root --group=root |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | .. _manifest: |
| 78 | |
| 79 | Specifying the files to distribute |
| 80 | ================================== |
| 81 | |
| 82 | If you don't supply an explicit list of files (or instructions on how to |
| 83 | generate one), the :command:`sdist` command puts a minimal default set into the |
| 84 | source distribution: |
| 85 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 86 | * all Python source files implied by the ``py_modules`` and |
| 87 | ``packages`` options |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 89 | * all C source files mentioned in the ``ext_modules`` or |
| 90 | ``libraries`` options |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Georg Brandl | d5f2d6e | 2010-07-31 09:15:10 +0000 | [diff] [blame] | 92 | .. XXX getting C library sources currently broken---no |
| 93 | :meth:`get_source_files` method in :file:`build_clib.py`! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 95 | * scripts identified by the ``scripts`` option |
Tarek Ziadé | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 96 | See :ref:`distutils-installing-scripts`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | * anything that looks like a test script: :file:`test/test\*.py` (currently, the |
| 99 | Distutils don't do anything with test scripts except include them in source |
| 100 | distributions, but in the future there will be a standard for testing Python |
| 101 | module distributions) |
| 102 | |
Ryan Gonzalez | f9f87f0 | 2017-04-14 04:00:25 -0500 | [diff] [blame] | 103 | * Any of the standard README files (:file:`README`, :file:`README.txt`, |
| 104 | or :file:`README.rst`), :file:`setup.py` (or whatever you called your setup |
| 105 | script), and :file:`setup.cfg`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
Tarek Ziadé | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 107 | * all files that matches the ``package_data`` metadata. |
| 108 | See :ref:`distutils-installing-package-data`. |
| 109 | |
Tarek Ziadé | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 110 | * all files that matches the ``data_files`` metadata. |
| 111 | See :ref:`distutils-additional-files`. |
| 112 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | Sometimes this is enough, but usually you will want to specify additional files |
| 114 | to distribute. The typical way to do this is to write a *manifest template*, |
| 115 | called :file:`MANIFEST.in` by default. The manifest template is just a list of |
| 116 | instructions for how to generate your manifest file, :file:`MANIFEST`, which is |
| 117 | the exact list of files to include in your source distribution. The |
| 118 | :command:`sdist` command processes this template and generates a manifest based |
| 119 | on its instructions and what it finds in the filesystem. |
| 120 | |
| 121 | If you prefer to roll your own manifest file, the format is simple: one filename |
| 122 | per line, regular files (or symlinks to them) only. If you do supply your own |
| 123 | :file:`MANIFEST`, you must specify everything: the default set of files |
| 124 | described above does not apply in this case. |
| 125 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 126 | .. versionchanged:: 3.1 |
| 127 | An existing generated :file:`MANIFEST` will be regenerated without |
| 128 | :command:`sdist` comparing its modification time to the one of |
| 129 | :file:`MANIFEST.in` or :file:`setup.py`. |
| 130 | |
| 131 | .. versionchanged:: 3.1.3 |
Éric Araujo | a893927 | 2010-08-14 23:44:13 +0000 | [diff] [blame] | 132 | :file:`MANIFEST` files start with a comment indicating they are generated. |
Éric Araujo | da668ff | 2010-08-14 02:30:34 +0000 | [diff] [blame] | 133 | Files without this comment are not overwritten or removed. |
| 134 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 135 | .. versionchanged:: 3.2.2 |
| 136 | :command:`sdist` will read a :file:`MANIFEST` file if no :file:`MANIFEST.in` |
| 137 | exists, like it used to do. |
| 138 | |
Ryan Gonzalez | f9f87f0 | 2017-04-14 04:00:25 -0500 | [diff] [blame] | 139 | .. versionchanged:: 3.7 |
| 140 | :file:`README.rst` is now included in the list of distutils standard READMEs. |
| 141 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 142 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | The manifest template has one command per line, where each command specifies a |
| 144 | set of files to include or exclude from the source distribution. For an |
Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 145 | example, again we turn to the Distutils' own manifest template: |
| 146 | |
| 147 | .. code-block:: none |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
| 149 | include *.txt |
| 150 | recursive-include examples *.txt *.py |
| 151 | prune examples/sample?/build |
| 152 | |
| 153 | The meanings should be fairly clear: include all files in the distribution root |
| 154 | matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory |
| 155 | matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching |
| 156 | :file:`examples/sample?/build`. All of this is done *after* the standard |
| 157 | include set, so you can exclude files from the standard set with explicit |
| 158 | instructions in the manifest template. (Or, you can use the |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 159 | :option:`!--no-defaults` option to disable the standard set entirely.) There are |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 160 | several other commands available in the manifest template mini-language; see |
| 161 | section :ref:`sdist-cmd`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | The order of commands in the manifest template matters: initially, we have the |
| 164 | list of default files as described above, and each command in the template adds |
| 165 | to or removes from that list of files. Once we have fully processed the |
| 166 | manifest template, we remove files that should not be included in the source |
| 167 | distribution: |
| 168 | |
| 169 | * all files in the Distutils "build" tree (default :file:`build/`) |
| 170 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 171 | * all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`, |
| 172 | :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
| 174 | Now we have our complete list of files, which is written to the manifest for |
| 175 | future reference, and then used to build the source distribution archive(s). |
| 176 | |
| 177 | You can disable the default set of included files with the |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 178 | :option:`!--no-defaults` option, and you can disable the standard exclude set |
| 179 | with :option:`!--no-prune`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
| 181 | Following the Distutils' own manifest template, let's trace how the |
| 182 | :command:`sdist` command builds the list of files to include in the Distutils |
| 183 | source distribution: |
| 184 | |
| 185 | #. include all Python source files in the :file:`distutils` and |
| 186 | :file:`distutils/command` subdirectories (because packages corresponding to |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 187 | those two directories were mentioned in the ``packages`` option in the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | setup script---see section :ref:`setup-script`) |
| 189 | |
| 190 | #. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard |
| 191 | files) |
| 192 | |
| 193 | #. include :file:`test/test\*.py` (standard files) |
| 194 | |
| 195 | #. include :file:`\*.txt` in the distribution root (this will find |
| 196 | :file:`README.txt` a second time, but such redundancies are weeded out later) |
| 197 | |
| 198 | #. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree |
| 199 | under :file:`examples`, |
| 200 | |
| 201 | #. exclude all files in the sub-trees starting at directories matching |
| 202 | :file:`examples/sample?/build`\ ---this may exclude files included by the |
| 203 | previous two steps, so it's important that the ``prune`` command in the manifest |
| 204 | template comes after the ``recursive-include`` command |
| 205 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 206 | #. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`, |
| 207 | :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs` |
| 208 | directories |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
| 210 | Just like in the setup script, file and directory names in the manifest template |
| 211 | should always be slash-separated; the Distutils will take care of converting |
| 212 | them to the standard representation on your platform. That way, the manifest |
| 213 | template is portable across operating systems. |
| 214 | |
| 215 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 216 | .. _manifest-options: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 218 | Manifest-related options |
| 219 | ======================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 221 | The normal course of operations for the :command:`sdist` command is as follows: |
| 222 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 223 | * if the manifest file (:file:`MANIFEST` by default) exists and the first line |
| 224 | does not have a comment indicating it is generated from :file:`MANIFEST.in`, |
| 225 | then it is used as is, unaltered |
| 226 | |
| 227 | * if the manifest file doesn't exist or has been previously automatically |
| 228 | generated, read :file:`MANIFEST.in` and create the manifest |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 229 | |
| 230 | * if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest |
| 231 | with just the default file set |
| 232 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 233 | * use the list of files now in :file:`MANIFEST` (either just generated or read |
| 234 | in) to create the source distribution archive(s) |
| 235 | |
| 236 | There are a couple of options that modify this behaviour. First, use the |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 237 | :option:`!--no-defaults` and :option:`!--no-prune` to disable the standard |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 238 | "include" and "exclude" sets. |
| 239 | |
| 240 | Second, you might just want to (re)generate the manifest, but not create a source |
| 241 | distribution:: |
| 242 | |
| 243 | python setup.py sdist --manifest-only |
| 244 | |
Martin Panter | 5c67933 | 2016-10-30 04:20:17 +0000 | [diff] [blame] | 245 | :option:`!-o` is a shortcut for :option:`!--manifest-only`. |