简体   繁体   English

已安装的程序包窃取名称空间

[英]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 我正在Python 2.7.9中开发一个名为“ jw.data”的程序包,其名称空间为jw,并且其中包含一个程序包数据

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

in jw/__init__.py . 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 . 当我运行./setup.py develop (在setup.py中,我将namespace_packages=['jw']放进去),然后在jw.data.egg-info/namespace_packages.txt有“ jw”。 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. 现在,我也在名称空间jw中编写了一个软件包“ jw.util”,其中包含一个软件包util As soon as I install it, importing jw.data or anything below it fails: 一旦安装它,导入jw.data或其下的任何内容都会失败:

>>> 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. 卸载jw.util后,它可以再次工作。

I used jw.util elsewhere, but not yet in a package within the jw namespace. 我在其他地方使用过jw.util ,但尚未在jw名称空间内的软件包中使用过。 It looks like jw.util is reserving the jw namespace. 看起来jw.util保留了jw命名空间。

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. 在那里,我注意到setup.py中的程序包名称与名称空间加程序包相同。 I renamed all the packages from "namespace.package x " to "namespace-package x ", and astonishingly enough it worked. 我将所有包从“ namespace.package x ”重命名为“ namespace-package x ”,并且足够有效。 I tried the same with jw.util and jw.data , but here that trick doesn't work. 我用jw.utiljw.data尝试了相同的方法 ,但是这里的技巧不起作用。 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? 而且我真的不相信setup.py中的包名称与它包含的包层次结构有关系吗?

Anyway, anybody got an idea what's going on here? 无论如何,有人知道这里发生了什么吗?

Seems to be a long-known bug in Python. 似乎是Python中一个众所周知的错误。

But there's a solution in https://github.com/pypa/setuptools/issues/250 : 但是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. 到site-packages中竞争程序包的-nspkg.pth 模块中的单行。 The semicolon is required. 必须使用分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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