-
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
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.pthfile to append a lineimport pkg_resources; pkg_resources.declare_namespace('ns')at the end of it. So it will scan
sys.pathto gather other directories intons.__path__.A tricky point is that we must ensure the
{pkg}-nspkg.pthfilename is alphabetically larger thaneasy-install.pth, so that we have source code directory installed bydevelopcommand insys.pathwhen runningdeclare_namespace. Is to rename tozzz-{pkg}-nspkg.ptha good idea? -
Fix
developcommand. Create a{pkg}-nspkg.pthin site-packages directory when a namespace package is installed usingdevelopcommand, like whatinstall --single-version-externally-manageddoes. The content of the pth file could be:/path/to/sourcecode import pkg_resources; pkg_resources.declare_namespace('ns')The
/path/to/sourcecodeline is duplicated with what is inserted in easy-install.pth. It is here to make suresys.pathcontains it beforedeclare_namespaceruns. It could be omitted if we have azzz-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.