Originally reported by: hongqn (Bitbucket: hongqn, GitHub: hongqn)
To reproduce this issue, do the following in a virtualenv:
prepare source code of two packages with same namespace package ns
:
mkdir -p pkg1/ns/pkg1 pkg2/ns/pkg2
echo "from setuptools import setup, find_packages; setup(name='pkg1', packages=find_packages(), namespace_packages=['ns'])" > pkg1/setup.py
echo "__import__('pkg_resources').declare_namespace(__name__)" > pkg1/ns/__init__.py
touch pkg1/ns/pkg1/__init__.py
echo "from setuptools import setup, find_packages; setup(name='pkg2', packages=find_packages(), namespace_packages=['ns'])" > pkg2/setup.py
echo "__import__('pkg_resources').declare_namespace(__name__)" > pkg2/ns/__init__.py
touch pkg2/ns/pkg2/__init__.py
install pkg1 using --single-version-externally-managed
cd pkg1
python setup.py install --single-version-externally-managed --record record.txt
cd ..
install pkg2 using develop
cd pkg2
python setup.py develop
cd ..
pkg1 is accessible
python -c 'import ns.pkg1'
pkg2 is unexpectedly inaccessible
python -c 'import ns.pkg2'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named pkg2
This was discussed in pip's issue tracker pypa/pip#3 years ago (because pip uses install --single-version-externally-managed
for pip install
and use develop
for pip install --editable
), but they still have not gotten a resolution.
I think this issue should be resolved in setuptools instead of pip. install --single-version-externally-managed
creates a {pkg}-nspkg.pth
in site-packages directory. It will create a ns module and assign its __path__
to the ns directory in site-packages. So pkg_resources.declare_namespace()
in pkg2's ns/init.py has no chance to run.
I have two proposals to fix it:
Fix install --single-version-exteranlly-managed
. Modify the {pkg}-nspkg.pth
file to append a line
import pkg_resources; pkg_resources.declare_namespace('ns')
at the end of it. So it will scan sys.path
to gather other directories into ns.__path__
.
A tricky point is that we must ensure the {pkg}-nspkg.pth
filename is alphabetically larger than easy-install.pth
, so that we have source code directory installed by develop
command in sys.path
when running declare_namespace
. Is to rename to zzz-{pkg}-nspkg.pth
a good idea?
Fix develop
command. Create a {pkg}-nspkg.pth
in site-packages directory when a namespace package is installed using develop
command, like what install --single-version-externally-managed
does. The content of the pth file could be:
/path/to/sourcecode
import pkg_resources; pkg_resources.declare_namespace('ns')
The /path/to/sourcecode
line is duplicated with what is inserted in easy-install.pth. It is here to make sure sys.path
contains it before declare_namespace
runs. It could be omitted if we have a zzz-
prefix for the pth filename.
What's the dev team's opinion? I prefer to the second proposal because it only affects develop
command which is mostly used in develop environment so it will not affect production systems.
I'd like to contribute code fix if you agree with either one of the proposals.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4