@@ -57,24 +57,15 @@ import object short).
57
57
Creating a namespace package
58
58
============================
59
59
60
- There are currently three different approaches to creating namespace packages:
60
+ There are currently two different approaches to creating namespace packages,
61
+ from which the latter is discouraged:
61
62
62
63
#. Use `native namespace packages `_. This type of namespace package is defined
63
64
in :pep: `420 ` and is available in Python 3.3 and later. This is recommended if
64
65
packages in your namespace only ever need to support Python 3 and
65
66
installation via ``pip ``.
66
- #. Use `pkgutil-style namespace packages `_. This is recommended for new
67
- packages that need to support Python 2 and 3 and installation via both
68
- ``pip `` and ``python setup.py install ``.
69
- #. Use `pkg_resources-style namespace packages `_. This method is recommended if
70
- you need compatibility with packages already using this method or if your
71
- package needs to be zip-safe.
72
-
73
- .. warning :: While native namespace packages and pkgutil-style namespace
74
- packages are largely compatible, pkg_resources-style namespace packages
75
- are not compatible with the other methods. It's inadvisable to use
76
- different methods in different distributions that provide packages to the
77
- same namespace.
67
+ #. Use `legacy namespace packages `_. This comprises `pkgutil-style namespace packages `_
68
+ and `pkg_resources-style namespace packages `_.
78
69
79
70
Native namespace packages
80
71
-------------------------
@@ -86,13 +77,14 @@ structure:
86
77
87
78
.. code-block :: text
88
79
89
- setup.py
90
- mynamespace/
80
+ mynamespace-subpackage-a/
81
+ setup.py # AND/OR pyproject.toml, setup.cfg
82
+ mynamespace/ # namespace package
91
83
# No __init__.py here.
92
- subpackage_a/
93
- # Sub-packages have __init__.py.
94
- __init__.py
95
- module.py
84
+ subpackage_a/
85
+ # Sub-packages have an __init__.py.
86
+ __init__.py
87
+ module.py
96
88
97
89
It is extremely important that every distribution that uses the namespace
98
90
package omits the :file: `__init__.py ` or uses a pkgutil-style
@@ -112,8 +104,31 @@ list all packages in your :file:`setup.py`. For example:
112
104
name = ' mynamespace-subpackage-a' ,
113
105
...
114
106
packages = find_namespace_packages(include = [' mynamespace.*' ])
107
+ # or list a single package explicitly:
108
+ # packages=['mynamespace.subpackage_a'],
115
109
)
116
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:
114
+
115
+ .. code-block :: toml
116
+
117
+ [build-system]
118
+ requires = ["setuptools", "setuptools-scm"]
119
+ build-backend = "setuptools.build_meta"
120
+
121
+ [tool.setuptools.packages.find]
122
+ where = ["."]
123
+ include = ["mynamespace.*"]
124
+
125
+ [project]
126
+ name = "mynamespace-subpackage-a"
127
+ ...
128
+
129
+ :ref: `setuptools ` will search the directory structure for implicit namespace
130
+ packages by default.
131
+
117
132
A complete working example of two native namespace packages can be found in
118
133
the `native namespace package example project `_.
119
134
@@ -125,8 +140,25 @@ the `native namespace package example project`_.
125
140
only support Python 3 and pkgutil-style namespace packages in the
126
141
distributions that need to support Python 2 and 3.
127
142
143
+
144
+ Legacy namespace packages
145
+ -------------------------
146
+
147
+ These to methods, that were used to create namespace packages prior to :pep: `420 `,
148
+ are now considered to be obsolete and should not be used unless you need compatibility
149
+ with packages already using this method. Also, :doc: `pkg_resources <setuptools:pkg_resources >`
150
+ has been deprecated.
151
+
152
+ To migrate an existing package, all packages sharing the namespace must be migrated simultaneously.
153
+
154
+ .. warning :: While native namespace packages and pkgutil-style namespace
155
+ packages are largely compatible, pkg_resources-style namespace packages
156
+ are not compatible with the other methods. It's inadvisable to use
157
+ different methods in different distributions that provide packages to the
158
+ same namespace.
159
+
128
160
pkgutil-style namespace packages
129
- --------------------------------
161
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
130
162
131
163
Python 2.3 introduced the :doc: `pkgutil <python:library/pkgutil >` module and the
132
164
:py:func: `python:pkgutil.extend_path ` function. This can be used to declare namespace
@@ -138,22 +170,22 @@ To create a pkgutil-style namespace package, you need to provide an
138
170
139
171
.. code-block :: text
140
172
141
- setup.py
173
+ setup.py # AND/OR pyproject.toml, setup.cfg
142
174
mynamespace/
143
175
__init__.py # Namespace package __init__.py
144
176
subpackage_a/
145
177
__init__.py # Sub-package __init__.py
146
178
module.py
147
179
148
180
The :file: `__init__.py ` file for the namespace package needs to contain
149
- ** only ** the following:
181
+ the following:
150
182
151
183
.. code-block :: python
152
184
153
185
__path__ = __import__ (' pkgutil' ).extend_path(__path__ , __name__ )
154
186
155
- **Every ** distribution that uses the namespace package must include an
156
- identical :file: `__init__.py `. If any distribution does not, it will cause the
187
+ **Every ** distribution that uses the namespace package must include such
188
+ an :file: `__init__.py `. If any distribution does not, it will cause the
157
189
namespace logic to fail and the other sub-packages will not be importable. Any
158
190
additional code in :file: `__init__.py ` will be inaccessible.
159
191
@@ -167,7 +199,7 @@ in the `pkgutil namespace example project`_.
167
199
168
200
169
201
pkg_resources-style namespace packages
170
- --------------------------------------
202
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
171
203
172
204
:doc: `Setuptools <setuptools:index >` provides the `pkg_resources.declare_namespace `_ function and
173
205
the ``namespace_packages `` argument to :func: `~setuptools.setup `. Together
@@ -183,7 +215,7 @@ To create a pkg_resources-style namespace package, you need to provide an
183
215
184
216
.. code-block :: text
185
217
186
- setup.py
218
+ setup.py # AND/OR pyproject.toml, setup.cfg
187
219
mynamespace/
188
220
__init__.py # Namespace package __init__.py
189
221
subpackage_a/
0 commit comments