@@ -19,7 +19,7 @@ have the following package structure:
19
19
__init__.py
20
20
...
21
21
module_b.py
22
- setup.py
22
+ pyproject.toml
23
23
24
24
And you use this package in your code like so::
25
25
@@ -31,17 +31,19 @@ Then you can break these sub-packages into two separate distributions:
31
31
.. code-block :: text
32
32
33
33
mynamespace-subpackage-a/
34
- setup.py
35
- mynamespace/
36
- subpackage_a/
37
- __init__.py
34
+ pyproject.toml
35
+ src/
36
+ mynamespace/
37
+ subpackage_a/
38
+ __init__.py
38
39
39
40
mynamespace-subpackage-b/
40
- setup.py
41
- mynamespace/
42
- subpackage_b/
43
- __init__.py
44
- module_b.py
41
+ pyproject.toml
42
+ src/
43
+ mynamespace/
44
+ subpackage_b/
45
+ __init__.py
46
+ module_b.py
45
47
46
48
Each sub-package can now be separately installed, used, and versioned.
47
49
@@ -73,44 +75,29 @@ Native namespace packages
73
75
Python 3.3 added **implicit ** namespace packages from :pep: `420 `. All that is
74
76
required to create a native namespace package is that you just omit
75
77
:file: `__init__.py ` from the namespace package directory. An example file
76
- structure:
78
+ structure (following :ref: ` src-layout < setuptools:src-layout >`) :
77
79
78
80
.. code-block :: text
79
81
80
82
mynamespace-subpackage-a/
81
- setup.py # AND/OR pyproject.toml, setup.cfg
82
- mynamespace/ # namespace package
83
- # No __init__.py here.
84
- subpackage_a/
85
- # Sub-packages have an __init__.py.
86
- __init__.py
87
- module.py
83
+ pyproject.toml # AND/OR setup.py, setup.cfg
84
+ src/
85
+ mynamespace/ # namespace package
86
+ # No __init__.py here.
87
+ subpackage_a/
88
+ # Sub-packages have an __init__.py.
89
+ __init__.py
90
+ module.py
88
91
89
92
It is extremely important that every distribution that uses the namespace
90
93
package omits the :file: `__init__.py ` or uses a pkgutil-style
91
94
:file: `__init__.py `. If any distribution does not, it will cause the namespace
92
95
logic to fail and the other sub-packages will not be importable.
93
96
94
- Because ``mynamespace `` doesn't contain an :file: `__init__.py `,
95
- :func: `setuptools.find_packages ` won't find the sub-package. You must
96
- use :func: `setuptools.find_namespace_packages ` instead or explicitly
97
- list all packages in your :file: `setup.py `. For example:
98
-
99
- .. code-block :: python
100
-
101
- from setuptools import setup, find_namespace_packages
102
-
103
- setup(
104
- name = ' mynamespace-subpackage-a' ,
105
- ...
106
- packages = find_namespace_packages(include = [' mynamespace.*' ])
107
- # or list a single package explicitly:
108
- # packages=['mynamespace.subpackage_a'],
109
- )
110
-
111
- The same can be accomplished by replacing the :file: `setup.py ` in the
112
- namespace packages' parent directory with a :file: `pyproject.toml `,
113
- with the following contents:
97
+ The ``src-layout `` directory structure allows automatic discovery of packages
98
+ by most :term: `build backends <Build Backend> `. If however you want to manage
99
+ exclusions or inclusions of packages yourself, this is possible to be configured
100
+ in the top-level :file: `pyproject.toml `:
114
101
115
102
.. code-block :: toml
116
103
@@ -119,13 +106,38 @@ with the following contents:
119
106
build-backend = "setuptools.build_meta"
120
107
121
108
[tool.setuptools.packages.find]
122
- where = [". "]
123
- include = ["mynamespace.* "]
109
+ where = ["src/ "]
110
+ include = ["mynamespace.subpackage_a "]
124
111
125
112
[project]
126
113
name = "mynamespace-subpackage-a"
127
114
...
128
115
116
+ The same can be accomplished with a :file: `setup.cfg `:
117
+
118
+ .. code-block :: ini
119
+
120
+ [options]
121
+ package_dir =
122
+ =src
123
+ packages = find_namespace:
124
+
125
+ [options.packages.find]
126
+ where = src
127
+
128
+ Or :file: `setup.py `:
129
+
130
+ .. code-block :: python
131
+
132
+ from setuptools import setup, find_namespace_packages
133
+
134
+ setup(
135
+ name = ' mynamespace-subpackage-a' ,
136
+ ...
137
+ packages = find_namespace_packages(where = ' src/' , include = [' mynamespace.subpackage_a' ]),
138
+ package_dir = {" " : " src" },
139
+ )
140
+
129
141
:ref: `setuptools ` will search the directory structure for implicit namespace
130
142
packages by default.
131
143
@@ -170,12 +182,14 @@ To create a pkgutil-style namespace package, you need to provide an
170
182
171
183
.. code-block :: text
172
184
173
- setup.py # AND/OR pyproject.toml, setup.cfg
174
- mynamespace/
175
- __init__.py # Namespace package __init__.py
176
- subpackage_a/
177
- __init__.py # Sub-package __init__.py
178
- module.py
185
+ mynamespace-subpackage-a/
186
+ src/
187
+ pyproject.toml # AND/OR setup.cfg, setup.py
188
+ mynamespace/
189
+ __init__.py # Namespace package __init__.py
190
+ subpackage_a/
191
+ __init__.py # Sub-package __init__.py
192
+ module.py
179
193
180
194
The :file: `__init__.py ` file for the namespace package needs to contain
181
195
the following:
@@ -215,22 +229,24 @@ To create a pkg_resources-style namespace package, you need to provide an
215
229
216
230
.. code-block :: text
217
231
218
- setup.py # AND/OR pyproject.toml, setup.cfg
219
- mynamespace/
220
- __init__.py # Namespace package __init__.py
221
- subpackage_a/
222
- __init__.py # Sub-package __init__.py
223
- module.py
232
+ mynamespace-subpackage-a/
233
+ src/
234
+ pyproject.toml # AND/OR setup.cfg, setup.py
235
+ mynamespace/
236
+ __init__.py # Namespace package __init__.py
237
+ subpackage_a/
238
+ __init__.py # Sub-package __init__.py
239
+ module.py
224
240
225
241
The :file: `__init__.py ` file for the namespace package needs to contain
226
- ** only ** the following:
242
+ the following:
227
243
228
244
.. code-block :: python
229
245
230
246
__import__ (' pkg_resources' ).declare_namespace(__name__ )
231
247
232
- **Every ** distribution that uses the namespace package must include an
233
- identical :file: `__init__.py `. If any distribution does not, it will cause the
248
+ **Every ** distribution that uses the namespace package must include such an
249
+ :file: `__init__.py `. If any distribution does not, it will cause the
234
250
namespace logic to fail and the other sub-packages will not be importable. Any
235
251
additional code in :file: `__init__.py ` will be inaccessible.
236
252
0 commit comments