2
2
Data Files Support
3
3
====================
4
4
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
+
5
14
Old packaging installation methods in the Python ecosystem
6
15
have traditionally allowed installation of "data files", which
7
16
are placed in a platform-specific location. However, the most common use case
@@ -19,10 +28,11 @@ Configuration Options
19
28
20
29
.. _include-package-data :
21
30
22
- include_package_data
23
- --------------------
31
+ 1. `` include_package_data ``
32
+ ---------------------------
24
33
25
34
First, you can use the ``include_package_data `` keyword.
35
+
26
36
For example, if the package tree looks like this::
27
37
28
38
project_root_directory
@@ -35,16 +45,34 @@ For example, if the package tree looks like this::
35
45
├── data1.txt
36
46
└── data2.txt
37
47
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:
39
67
40
68
.. tab :: pyproject.toml
41
69
42
70
.. code-block :: toml
43
71
44
72
[tool.setuptools]
45
73
# ...
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.
48
76
include-package-data = true
49
77
50
78
[tool.setuptools.packages.find]
@@ -76,33 +104,18 @@ and you supply this configuration:
76
104
include_package_data = True
77
105
)
78
106
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
-
94
107
.. note ::
95
108
.. 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 ``
97
110
when projects are configured via ``pyproject.toml ``.
98
111
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
100
113
to ensure backwards compatibility with existing projects.
101
114
102
115
.. _package-data :
103
116
104
- package_data
105
- ------------
117
+ 2. `` package_data ``
118
+ -------------------
106
119
107
120
By default, ``include_package_data `` considers **all ** non ``.py `` files found inside
108
121
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.
172
185
173
186
.. note ::
174
187
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
176
189
converts slashes to appropriate platform-specific separators at build time.
177
190
178
191
.. important ::
@@ -271,8 +284,8 @@ we specify that ``data1.rst`` from ``mypkg1`` alone should be captured as well.
271
284
272
285
.. _exclude-package-data :
273
286
274
- exclude_package_data
275
- --------------------
287
+ 3. `` exclude_package_data ``
288
+ ---------------------------
276
289
277
290
Sometimes, the ``include_package_data `` or ``package_data `` options alone
278
291
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,
337
350
even if they were listed in ``package_data `` or were included as a result of using
338
351
``include_package_data ``.
339
352
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 ``.
340
380
341
381
Summary
342
382
-------
@@ -450,7 +490,7 @@ With :ref:`package-data`, the configuration might look like this:
450
490
}
451
491
)
452
492
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,
454
494
which enables the ``data `` directory to be identified, and then, we separately specify data
455
495
files for the root package ``mypkg ``, and the namespace package ``data `` under the package
456
496
``mypkg ``.
0 commit comments