[英]Python - How do I import parent package from extended namespace
So I have two different packages. 所以我有两个不同的包裹。 The first one is my main package "bobzilla", and the other package extends the first "bobzilla.structure".
第一个是我的主软件包“ bobzilla”,另一个软件包扩展了第一个“ bobzilla.structure”。
Ie 即
bobzilla/
__init__.py
Where bobzilla/__init__.py contains: 其中bobzilla / __ init__.py包含:
class Foo(object):
def __init__(self, bar):
self.bar=bar
And: 和:
bobzilla/
__init__.py
structure/
__init__.py
Where bobzilla/structure/__init__.py contains: 其中bobzilla / structure / __ init__.py包含:
import bobzilla
foo=Foo("bar")
When executing the bobzilla/structure/__init__.py I get: 当执行bobzilla / structure / __ init__.py时,我得到:
AttributeError: 'module' object has no attribute 'Foo'
My question is, how can I reference the namespace "bobzilla" from "bobzilla.structure" without "bobzilla.structure" overwriting it. 我的问题是,如何从“ bobzilla.structure”引用命名空间“ bobzilla”,而不会覆盖“ bobzilla.structure”。
Note: 注意:
The one who set the "This question may already have an answer here" is wrong. 设置“此问题可能已经在这里有了答案”的人是错误的。 I have already tried that and it did not work.
我已经尝试过了,但是没有用。 This is two different packages, not the same one.
这是两个不同的程序包,而不是相同的程序包。
It looks like you're trying to create a namespace package without creating a namespace package, which… isn't going to work. 似乎您在尝试创建名称空间包而不创建名称空间包,这将无法正常工作。
To explicitly designate a package as a namespace package, you need to use the pkgutil
library (or the fancier stuff in setuptools
), something like this at the top of bobzilla/__init__.py
: 要明确指定一个包作为一个命名空间包,您需要使用
pkgutil
库(或票友东东setuptools
),像这样在顶部bobzilla/__init__.py
:
__path__ = pkgutil.extend_path(__path__, __name__)
If you want an implicit namespace package , you can do that in Python 3.3 and later… but only if bobzilla
is empty . 如果需要隐式命名空间包 ,则可以在Python 3.3和更高版本中执行此操作……但
bobzilla
是bobzilla
为空 。 Implicit namespace packages cannot contain an __init__.py
file, and will never have any contents but their modules. 隐式名称空间包不能包含
__init__.py
文件,并且除了其模块外,将永远没有任何内容。 (Well, you could create an implicit namespace package, then add a subpackage or external module that explicit adds things into it once it's created… but I'm not sure why you'd do that.) (好吧,您可以创建一个隐式名称空间包,然后添加一个子包或外部模块,以在创建后将其显式添加到其中……但是我不确定为什么要这么做。)
bobzilla/structure/__init__.py
should be: bobzilla/structure/__init__.py
应该是:
from bobzilla import Foo
foo=Foo("bar")
Testing the above with identical structure and code mention in the question. 用问题中提到的相同结构和代码测试以上内容。
bobzilla
being in /path/to/test
. bobzilla
在/path/to/test
。 Result: 结果:
>>> from site import addsitedir
>>> addsitedir("/path/to/test")
>>> import bobzilla.structure as struc
>>> struc.foo
<bobzilla.Foo object at 0x7f2f8c716810>
>>>
Try: 尝试:
import bozilla.structure as struct
import bozilla
A working example with the NLTK
library, http://nltk.org : NLTK
库http://nltk.org的工作示例:
>>> import nltk.corpus as corpus
>>> nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'nltk' is not defined
>>> import nltk
>>> nltk.corpus
<LazyModule 'nltk.corpus'>
>>> corpus
<LazyModule 'nltk.corpus'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.