Skip to content

Commit adb8374

Browse files
authored
[Docs] Clarify usage of include_package_data/package_data/exclude_package_data on package data files (pypa#4643)
2 parents 89d3e94 + 1595318 commit adb8374

File tree

1 file changed

+68
-28
lines changed

1 file changed

+68
-28
lines changed

docs/userguide/datafiles.rst

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
Data Files Support
33
====================
44

5+
In the Python ecosystem, the term "data files" is used in various complex scenarios
6+
and can have nuanced meanings. For the purposes of this documentation,
7+
we define "data files" as non-Python files that are installed alongside Python
8+
modules and packages on the user's machine when they install a
9+
:term:`distribution <Distribution Package>` via :term:`wheel <Wheel>`.
10+
11+
These files are typically intended for use at **runtime** by the package itself or
12+
to influence the behavior of other packages or systems.
13+
514
Old packaging installation methods in the Python ecosystem
615
have traditionally allowed installation of "data files", which
716
are placed in a platform-specific location. However, the most common use case
@@ -19,10 +28,11 @@ Configuration Options
1928

2029
.. _include-package-data:
2130

22-
include_package_data
23-
--------------------
31+
1. ``include_package_data``
32+
---------------------------
2433

2534
First, you can use the ``include_package_data`` keyword.
35+
2636
For example, if the package tree looks like this::
2737

2838
project_root_directory
@@ -35,16 +45,34 @@ For example, if the package tree looks like this::
3545
├── data1.txt
3646
└── data2.txt
3747

38-
and you supply this configuration:
48+
When **at least one** of the following conditions are met:
49+
50+
1. These files are included via the :ref:`MANIFEST.in <Using MANIFEST.in>` file,
51+
like so::
52+
53+
include src/mypkg/*.txt
54+
include src/mypkg/*.rst
55+
56+
2. They are being tracked by a revision control system such as Git, Mercurial
57+
or SVN, **AND** you have configured an appropriate plugin such as
58+
:pypi:`setuptools-scm` or :pypi:`setuptools-svn`.
59+
(See the section below on :ref:`Adding Support for Revision
60+
Control Systems` for information on how to configure such plugins.)
61+
62+
then all the ``.txt`` and ``.rst`` files will be included into
63+
the source distribution.
64+
65+
To further include them into the ``wheels``, you can use the
66+
``include_package_data`` keyword:
3967

4068
.. tab:: pyproject.toml
4169

4270
.. code-block:: toml
4371
4472
[tool.setuptools]
4573
# ...
46-
# By default, include-package-data is true in pyproject.toml, so you do
47-
# NOT have to specify this line.
74+
# By default, include-package-data is true in pyproject.toml,
75+
# so you do NOT have to specify this line.
4876
include-package-data = true
4977
5078
[tool.setuptools.packages.find]
@@ -76,33 +104,18 @@ and you supply this configuration:
76104
include_package_data=True
77105
)
78106
79-
then all the ``.txt`` and ``.rst`` files will be automatically installed with
80-
your package, provided:
81-
82-
1. These files are included via the :ref:`MANIFEST.in <Using MANIFEST.in>` file,
83-
like so::
84-
85-
include src/mypkg/*.txt
86-
include src/mypkg/*.rst
87-
88-
2. OR, they are being tracked by a revision control system such as Git, Mercurial
89-
or SVN, and you have configured an appropriate plugin such as
90-
:pypi:`setuptools-scm` or :pypi:`setuptools-svn`.
91-
(See the section below on :ref:`Adding Support for Revision
92-
Control Systems` for information on how to write such plugins.)
93-
94107
.. note::
95108
.. versionadded:: v61.0.0
96-
The default value for ``tool.setuptools.include-package-data`` is ``True``
109+
The default value for ``tool.setuptools.include-package-data`` is ``true``
97110
when projects are configured via ``pyproject.toml``.
98111
This behaviour differs from ``setup.cfg`` and ``setup.py``
99-
(where ``include_package_data=False`` by default), which was not changed
112+
(where ``include_package_data`` is ``False`` by default), which was not changed
100113
to ensure backwards compatibility with existing projects.
101114

102115
.. _package-data:
103116

104-
package_data
105-
------------
117+
2. ``package_data``
118+
-------------------
106119

107120
By default, ``include_package_data`` considers **all** non ``.py`` files found inside
108121
the package directory (``src/mypkg`` in this case) as data files, and includes those that
@@ -172,7 +185,7 @@ file, nor require to be added by a revision control system plugin.
172185

173186
.. note::
174187
If your glob patterns use paths, you *must* use a forward slash (``/``) as
175-
the path separator, even if you are on Windows. Setuptools automatically
188+
the path separator, even if you are on Windows. ``setuptools`` automatically
176189
converts slashes to appropriate platform-specific separators at build time.
177190

178191
.. important::
@@ -271,8 +284,8 @@ we specify that ``data1.rst`` from ``mypkg1`` alone should be captured as well.
271284

272285
.. _exclude-package-data:
273286

274-
exclude_package_data
275-
--------------------
287+
3. ``exclude_package_data``
288+
---------------------------
276289

277290
Sometimes, the ``include_package_data`` or ``package_data`` options alone
278291
aren't sufficient to precisely define what files you want included. For example,
@@ -337,6 +350,33 @@ Any files that match these patterns will be *excluded* from installation,
337350
even if they were listed in ``package_data`` or were included as a result of using
338351
``include_package_data``.
339352

353+
.. _interplay_package_data_keywords:
354+
355+
Interplay between these keywords
356+
--------------------------------
357+
358+
Meanwhile, to further clarify the interplay between these three keywords,
359+
to include certain data file into the source distribution, the following
360+
logic condition has to be met::
361+
362+
MANIFEST.in or (package-data and not exclude-package-data)
363+
364+
In plain language, the file should be either:
365+
366+
1. included in ``MANIFEST.in``; or
367+
368+
2. selected by ``package-data`` AND not excluded by ``exclude-package-data``.
369+
370+
To include some data file into the ``.whl``::
371+
372+
(not exclude-package-data) and ((include-package-data and MANIFEST.in) or package-data)
373+
374+
In other words, the file should not be excluded by ``exclude-package-data``
375+
(highest priority), AND should be either:
376+
377+
1. selected by ``package-data``; or
378+
379+
2. selected by ``MANIFEST.in`` AND use ``include-package-data = true``.
340380

341381
Summary
342382
-------
@@ -450,7 +490,7 @@ With :ref:`package-data`, the configuration might look like this:
450490
}
451491
)
452492
453-
In other words, we allow Setuptools to scan for namespace packages in the ``src`` directory,
493+
In other words, we allow ``setuptools`` to scan for namespace packages in the ``src`` directory,
454494
which enables the ``data`` directory to be identified, and then, we separately specify data
455495
files for the root package ``mypkg``, and the namespace package ``data`` under the package
456496
``mypkg``.

0 commit comments

Comments
 (0)