简体   繁体   中英

Installed package stealing namespace

I'm developing a package "jw.data" in Python 2.7.9, with a namespace of jw and a package data in in. I have put the canonical

try:
    __import__('pkg_resources').declare_namespace(__name__)
except ImportError:
    from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

in jw/__init__.py . When I run ./setup.py develop (in setup.py I have put namespace_packages=['jw'] ), then I have "jw" in jw.data.egg-info/namespace_packages.txt . Doing a

import jw.data
import jw.data.model

just works fine. So I guess I have set up the namespace package correctly.

Now I have written a package "jw.util", also in namespace jw , with a package util in it. As soon as I install it, importing jw.data or anything below it fails:

>>> import jw.data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named data

After uninstalling jw.util it works again.

I used jw.util elsewhere, but not yet in a package within the jw namespace. It looks like jw.util is reserving the jw namespace.

I had the same problem with another namespace. There I noticed the package name in setup.py is just the same as the namespace plus package. I renamed all the packages from "namespace.package x " to "namespace-package x ", and astonishingly enough it worked. I tried the same with jw.util and jw.data , but here that trick doesn't work. And I don't really believe the package name in setup.py has anything to do with the package hierarchy it contains, or has it?

Anyway, anybody got an idea what's going on here?

Seems to be a long-known bug in Python.

But there's a solution in https://github.com/pypa/setuptools/issues/250 :

Put simply, append

;import pkg_resources; pkg_resources.fixup_namespace_packages('')

to the single line in module -nspkg.pth of the competing package in site-packages. The semicolon is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM