问题
What is the purpose of the namespace_packages argument in setup.py when working with PEP420 namespace packages (the ones without __init__.py)?
I played with it and saw no difference whether I declared the namespace packages or not. "setup.py install" and "pip install ." worked in any case.
I am building an automatic setup.py code generator and would be happy not to handle this if this is not necessary.
回答1:
As long as you:
- aim for Python 3.3 and newer or Python 2.7 with
importlib2
dependency installed (a backport ofimportlib
for Python 2), - use a recent version of
setuptools
for packaging (I think it should be 28.8 or newer) - and use a recent
pip
version for installing (9.0 and newer will be fine, 8.1.2 will probably also work, but you should test that yourself),
you are on the safe side and can safely omit the namespace_packages
keyword arg in your setup scripts.
There is a PyPA's official repository named sample-namespace-packages on GitHub that contains a suite of tests for different possible scenarios of distributions installed that contain namespace packages of each kind. As you can see, the sample packages using the implicit namespace packages don't use namespace_packages
arg in their setup scripts (here is one of the scripts) and all of the tests of types pep420
and cross_pep420_pkgutil
pass on Python 3; here is the complete results table.
回答2:
Namespace packages are separate packages that are installed under one top-level name.
Usually two different packages (for example SQLObject and Cheetah3) install two (or more) different top-level packages (sqlobject
and Cheetah
in my examples).
But what if I have a library that I want to split into parts and allow to install these parts without the rest of the library? I use namespace packages. Example: these two packages are 2 parts of one library: m_lib and m_lib.defenc. One installs m_lib/defenc.py
which can be used separately, the other installs the rest of the m_lib
library. To install the entire library at once I also provide m_lib.full.
PS. All mentioned packages are mine. Source code is provided at Github or my personal git hosting.
来源:https://stackoverflow.com/questions/50280138/pep-420-namespace-packages-purpose-in-setup-py