【发布时间】:2014-01-21 14:40:46
【问题描述】:
所以我有两个不同的包。第一个是我的主包“bobzilla”,另一个包扩展了第一个“bobzilla.structure”。
即
bobzilla/
__init__.py
bobzilla/__init__.py 包含的位置:
class Foo(object):
def __init__(self, bar):
self.bar=bar
还有:
bobzilla/
__init__.py
structure/
__init__.py
bobzilla/structure/__init__.py 包含的位置:
import bobzilla
foo=Foo("bar")
执行 bobzilla/structure/__init__.py 时,我得到:
AttributeError: 'module' object has no attribute 'Foo'
我的问题是,如何在不覆盖“bobzilla.structure”的情况下从“bobzilla.structure”引用命名空间“bobzilla”。
注意:
设置“这个问题在这里可能已经有答案”的人是错误的。我已经尝试过了,但它没有用。这是两个不同的包,不是同一个。
【问题讨论】:
-
你不能。命名空间包必须为空。
-
@MartijnPieters 好的,所以我只需从 bobzilla.structure-package 中删除 bobzilla/__init__.py 然后使其成为命名空间包,它应该可以工作吗?
-
您使用的是哪个版本的 Python?您是在尝试创建经典的显式命名空间包,还是 PEP420 风格的隐式包?如果是前者,您能告诉我们您的相关
extend_path或declare_namespace代码吗?如果是后者……您使用的是 Python 3.3 或更高版本,对吗?
标签: python namespaces packages extend