Skip to content

Commit b39639a

Browse files
authored
[Docs] Further clarify include_package_data (#4230)
2 parents 66dfd28 + 97aae46 commit b39639a

File tree

2 files changed

+80
-38
lines changed

2 files changed

+80
-38
lines changed

docs/userguide/datafiles.rst

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ by including the data files **inside the package directory**.
1010

1111
Setuptools focuses on this most common type of data files and offers three ways
1212
of specifying which files should be included in your packages, as described in
13-
the following sections.
13+
the following section.
14+
15+
16+
Configuration Options
17+
=====================
18+
19+
20+
.. _include-package-data:
1421

1522
include_package_data
16-
====================
23+
--------------------
1724

18-
First, you can simply use the ``include_package_data`` keyword.
25+
First, you can use the ``include_package_data`` keyword.
1926
For example, if the package tree looks like this::
2027

2128
project_root_directory
@@ -92,8 +99,10 @@ your package, provided:
9299
(where ``include_package_data=False`` by default), which was not changed
93100
to ensure backwards compatibility with existing projects.
94101

102+
.. _package-data:
103+
95104
package_data
96-
============
105+
------------
97106

98107
By default, ``include_package_data`` considers **all** non ``.py`` files found inside
99108
the package directory (``src/mypkg`` in this case) as data files, and includes those that
@@ -260,8 +269,10 @@ we specify that ``data1.rst`` from ``mypkg1`` alone should be captured as well.
260269
Please check :ref:`section subdirectories <subdir-data-files>` below.
261270

262271

272+
.. _exclude-package-data:
273+
263274
exclude_package_data
264-
====================
275+
--------------------
265276

266277
Sometimes, the ``include_package_data`` or ``package_data`` options alone
267278
aren't sufficient to precisely define what files you want included. For example,
@@ -327,6 +338,38 @@ even if they were listed in ``package_data`` or were included as a result of usi
327338
``include_package_data``.
328339

329340

341+
Summary
342+
-------
343+
344+
In summary, the three options allow you to:
345+
346+
``include_package_data``
347+
Accept all data files and directories matched by
348+
:ref:`MANIFEST.in <Using MANIFEST.in>` or added by
349+
a :ref:`plugin <Adding Support for Revision Control Systems>`.
350+
351+
``package_data``
352+
Specify additional patterns to match files that may or may
353+
not be matched by :ref:`MANIFEST.in <Using MANIFEST.in>`
354+
or added by a :ref:`plugin <Adding Support for Revision Control Systems>`.
355+
356+
``exclude_package_data``
357+
Specify patterns for data files and directories that should *not* be
358+
included when a package is installed, even if they would otherwise have
359+
been included due to the use of the preceding options.
360+
361+
.. note::
362+
Due to the way the build process works, a data file that you
363+
include in your project and then stop including may be "orphaned" in your
364+
project's build directories, requiring you to manually deleting them.
365+
This may also be important for your users and contributors
366+
if they track intermediate revisions of your project using Subversion; be sure
367+
to let them know when you make changes that remove files from inclusion so they
368+
can also manually delete them.
369+
370+
See also troubleshooting information in :ref:`Caching and Troubleshooting`.
371+
372+
330373
.. _subdir-data-files:
331374

332375
Subdirectory for Data Files
@@ -350,8 +393,13 @@ Here, the ``.rst`` files are placed under a ``data`` subdirectory inside ``mypkg
350393
while the ``.txt`` files are directly under ``mypkg``.
351394

352395
In this case, the recommended approach is to treat ``data`` as a namespace package
353-
(refer :pep:`420`). With ``package_data``,
354-
the configuration might look like this:
396+
(refer :pep:`420`). This way, you can rely on the same methods described above,
397+
using either :ref:`package-data` or :ref:`include-package-data`.
398+
For the sake of completeness, we include below configuration examples
399+
for the subdirectory structure, but please refer to the detailed
400+
information in the previous sections of this document.
401+
402+
With :ref:`package-data`, the configuration might look like this:
355403

356404
.. tab:: pyproject.toml
357405

@@ -407,8 +455,9 @@ which enables the ``data`` directory to be identified, and then, we separately s
407455
files for the root package ``mypkg``, and the namespace package ``data`` under the package
408456
``mypkg``.
409457

410-
With ``include_package_data`` the configuration is simpler: you simply need to enable
411-
scanning of namespace packages in the ``src`` directory and the rest is handled by Setuptools.
458+
Alternatively, you can also rely on :ref:`include-package-data`.
459+
Note that this is the default behaviour in ``pyproject.toml``, but you need to
460+
manually enable scanning of namespace packages in ``setup.cfg`` or ``setup.py``:
412461

413462
.. tab:: pyproject.toml
414463

@@ -422,7 +471,7 @@ scanning of namespace packages in the ``src`` directory and the rest is handled
422471
423472
[tool.setuptools.packages.find]
424473
# scanning for namespace packages is true by default in pyproject.toml, so
425-
# you need NOT include the following line.
474+
# you need NOT include this configuration.
426475
namespaces = true
427476
where = ["src"]
428477
@@ -451,34 +500,9 @@ scanning of namespace packages in the ``src`` directory and the rest is handled
451500
include_package_data=True,
452501
)
453502
454-
Summary
455-
=======
456-
457-
In summary, the three options allow you to:
458-
459-
``include_package_data``
460-
Accept all data files and directories matched by
461-
:ref:`MANIFEST.in <Using MANIFEST.in>` or added by
462-
a :ref:`plugin <Adding Support for Revision Control Systems>`.
463-
464-
``package_data``
465-
Specify additional patterns to match files that may or may
466-
not be matched by :ref:`MANIFEST.in <Using MANIFEST.in>`
467-
or added by a :ref:`plugin <Adding Support for Revision Control Systems>`.
468-
469-
``exclude_package_data``
470-
Specify patterns for data files and directories that should *not* be
471-
included when a package is installed, even if they would otherwise have
472-
been included due to the use of the preceding options.
473-
474-
.. note::
475-
Due to the way the build process works, a data file that you
476-
include in your project and then stop including may be "orphaned" in your
477-
project's build directories, requiring you to manually deleting them.
478-
This may also be important for your users and contributors
479-
if they track intermediate revisions of your project using Subversion; be sure
480-
to let them know when you make changes that remove files from inclusion so they
481-
can also manually delete them.
503+
To avoid common mistakes with :ref:`include-package-data`,
504+
please ensure :ref:`MANIFEST.in <Using MANIFEST.in>` is properly set
505+
or use a revision control system plugin (see :doc:`/userguide/miscellaneous`).
482506

483507

484508
.. _Accessing Data Files at Runtime:

docs/userguide/miscellaneous.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ binary extensions during the build process, or included in the final
168168

169169
See :doc:`/userguide/datafiles` for more information.
170170

171+
172+
.. _Caching and Troubleshooting:
173+
174+
Caching and Troubleshooting
175+
===========================
176+
177+
Setuptools automatically creates a few directories to host build artefacts and
178+
cache files, such as ``build``, ``dist``, ``*.egg-info``. While cache is
179+
useful to speed up incremental builds, in some edge cases it might become
180+
stale. If you feel that caching is causing problems to your build, specially
181+
after changes in configuration or in the directory/file structure., consider
182+
removing ``build``, ``dist``, ``*.egg-info`` [#PKG-INFO]_ before rebuilding or
183+
reinstalling your project.
184+
171185
----
172186

173187
.. [#build-process]
@@ -183,5 +197,9 @@ binary extensions during the build process, or included in the final
183197
:term:`Virtual Environment`.
184198
Therefore it only contains items that are required during runtime.
185199
200+
.. [#PKG-INFO]
201+
When working from an extracted sdist (e.g. for patching), you might also consider removing
202+
the ``PKG-INFO`` file to force its recreation.
203+
186204
.. _git: https://git-scm.com
187205
.. _mercurial: https://www.mercurial-scm.org

0 commit comments

Comments
 (0)