1.. _Automatic Resource Extraction:
2
3Automatic Resource Extraction
4-----------------------------
5
6If you are using tools that expect your resources to be "real" files, or your
7project includes non-extension native libraries or other files that your C
8extensions expect to be able to access, you may need to list those files in
9the ``eager_resources`` argument to ``setup()``, so that the files will be
10extracted together, whenever a C extension in the project is imported.
11
12This is especially important if your project includes shared libraries *other*
13than distutils-built C extensions, and those shared libraries use file
14extensions other than ``.dll``, ``.so``, or ``.dylib``, which are the
15extensions that setuptools 0.6a8 and higher automatically detects as shared
16libraries and adds to the ``native_libs.txt`` file for you.  Any shared
17libraries whose names do not end with one of those extensions should be listed
18as ``eager_resources``, because they need to be present in the filesystem when
19he C extensions that link to them are used.
20
21The ``pkg_resources`` runtime for compressed packages will automatically
22extract *all* C extensions and ``eager_resources`` at the same time, whenever
23*any* C extension or eager resource is requested via the ``resource_filename()``
24API.  (C extensions are imported using ``resource_filename()`` internally.)
25This ensures that C extensions will see all of the "real" files that they
26expect to see.
27
28Note also that you can list directory resource names in ``eager_resources`` as
29well, in which case the directory's contents (including subdirectories) will be
30extracted whenever any C extension or eager resource is requested.
31
32Please note that if you're not sure whether you need to use this argument, you
33don't!  It's really intended to support projects with lots of non-Python
34dependencies and as a last resort for crufty projects that can't otherwise
35handle being compressed.  If your package is pure Python, Python plus data
36files, or Python plus C, you really don't need this.  You've got to be using
37either C or an external program that needs "real" files in your project before
38there's any possibility of ``eager_resources`` being relevant to your project.
39
40Defining Additional Metadata
41----------------------------
42
43Some extensible applications and frameworks may need to define their own kinds
44of metadata to include in eggs, which they can then access using the
45``pkg_resources`` metadata APIs.  Ordinarily, this is done by having plugin
46developers include additional files in their ``ProjectName.egg-info``
47directory.  However, since it can be tedious to create such files by hand, you
48may want to create a distutils extension that will create the necessary files
49from arguments to ``setup()``, in much the same way that ``setuptools`` does
50for many of the ``setup()`` arguments it adds.  See the section below on
51:ref:`Creating ``distutils\`\` Extensions` for more details, especially the
52subsection on :ref:`Adding new EGG-INFO Files`.
53
54Setting the ``zip_safe`` flag
55-----------------------------
56
57For some use cases (such as bundling as part of a larger application), Python
58packages may be run directly from a zip file.
59Not all packages, however, are capable of running in compressed form, because
60they may expect to be able to access either source code or data files as
61normal operating system files.  So, ``setuptools`` can install your project
62as a zipfile or a directory, and its default choice is determined by the
63project's ``zip_safe`` flag.
64
65You can pass a True or False value for the ``zip_safe`` argument to the
66``setup()`` function, or you can omit it.  If you omit it, the ``bdist_egg``
67command will analyze your project's contents to see if it can detect any
68conditions that would prevent it from working in a zipfile.  It will output
69notices to the console about any such conditions that it finds.
70
71Currently, this analysis is extremely conservative: it will consider the
72project unsafe if it contains any C extensions or datafiles whatsoever.  This
73does *not* mean that the project can't or won't work as a zipfile!  It just
74means that the ``bdist_egg`` authors aren't yet comfortable asserting that
75the project *will* work.  If the project contains no C or data files, and does
76no ``__file__`` or ``__path__`` introspection or source code manipulation, then
77there is an extremely solid chance the project will work when installed as a
78zipfile.  (And if the project uses ``pkg_resources`` for all its data file
79access, then C extensions and other data files shouldn't be a problem at all.
80See the :ref:`Accessing Data Files at Runtime` section above for more information.)
81
82However, if ``bdist_egg`` can't be *sure* that your package will work, but
83you've checked over all the warnings it issued, and you are either satisfied it
84*will* work (or if you want to try it for yourself), then you should set
85``zip_safe`` to ``True`` in your ``setup()`` call.  If it turns out that it
86doesn't work, you can always change it to ``False``, which will force
87``setuptools`` to install your project as a directory rather than as a zipfile.
88
89In the future, as we gain more experience with different packages and become
90more satisfied with the robustness of the ``pkg_resources`` runtime, the
91"zip safety" analysis may become less conservative.  However, we strongly
92recommend that you determine for yourself whether your project functions
93correctly when installed as a zipfile, correct any problems if you can, and
94then make an explicit declaration of ``True`` or ``False`` for the ``zip_safe``
95flag, so that it will not be necessary for ``bdist_egg`` to try to guess
96whether your project can work as a zipfile.
97
98
99.. _Controlling files in the distribution:
100
101Controlling files in the distribution
102-------------------------------------
103
104For the most common use cases, ``setuptools`` will automatically find out which
105files are necessary for distributing the package.
106This includes all :term:`pure Python modules <Pure Module>` in the
107``py_modules`` or ``packages`` configuration, and the C sources (but not C
108headers) listed as part of extensions when creating a :term:`Source
109Distribution (or "sdist")`.
110
111However, when building more complex packages (e.g. packages that include
112non-Python files, or that need to use custom C headers), you might find that
113not all files present in your project folder are included in package
114:term:`distribution archive <Distribution Package>`.
115
116In these situations you can use a ``setuptools``
117:ref:`plugin <Adding Support for Revision Control Systems>`,
118such as :pypi:`setuptools-scm` or :pypi:`setuptools-svn` to automatically
119include all files tracked by your Revision Control System into the ``sdist``.
120
121.. _Using MANIFEST.in:
122
123Alternatively, if you need finer control, you can add a ``MANIFEST.in`` file at
124the root of your project.
125This file contains instructions that tell ``setuptools`` which files exactly
126should be part of the ``sdist`` (or not).
127A comprehensive guide to ``MANIFEST.in`` syntax is available at the
128:doc:`PyPA's Packaging User Guide <PyPUG:guides/using-manifest-in>`.
129
130Once the correct files are present in the ``sdist``, they can then be used by
131binary extensions during the build process, or included in the final
132:term:`wheel <Wheel>` [#build-process]_ if you configure ``setuptools`` with
133``include_package_data=True``.
134
135.. important::
136   Please note that, when using ``include_package_data=True``, only files **inside
137   the package directory** are included in the final ``wheel``, by default.
138
139   So for example, if you create a :term:`Python project <Project>` that uses
140   :pypi:`setuptools-scm` and have a ``tests`` directory outside of the package
141   folder, the ``tests`` directory will be present in the ``sdist`` but not in the
142   ``wheel`` [#wheel-vs-sdist]_.
143
144   See :doc:`/userguide/datafiles` for more information.
145
146----
147
148.. [#build-process]
149   You can think about the build process as two stages: first the ``sdist``
150   will be created and then the ``wheel`` will be produced from that ``sdist``.
151
152.. [#wheel-vs-sdist]
153   This happens because the ``sdist`` can contain files that are useful during
154   development or the build process itself, but not in runtime (e.g. tests,
155   docs, examples, etc...).
156   The ``wheel``, on the other hand, is a file format that has been optimized
157   and is ready to be unpacked into a running installation of Python or
158   :term:`Virtual Environment`.
159   Therefore it only contains items that are required during runtime.
160