ada*_*Lev 3 python import module
我有一个这样的py文件,出错了.
from world import acme
def make_stuff_happen():
acme.account.foo() # Works
acme.subscription.bar() # FAIL: "module 'object' has no attribute 'subscription'"
make_stuff_happen()
Run Code Online (Sandbox Code Playgroud)
但这有效!
from world import acme
from world.acme import subscription
def make_stuff_happen():
acme.account.foo() # Works
subscription.bar() # Now this works.
make_stuff_happen()
Run Code Online (Sandbox Code Playgroud)
我只能说是WTF,这可能是什么造成的呢?这样的行为应该至少是两个一致的acme.account
和acme.subscription
.
谢谢!
更新 - acme文件夹的文件夹结构:
acme
|-- __init__.py
|-- account.py
|-- catalog.py
|-- core.py
|-- proxy.py
|-- subscription.py
`-- utils.py
Run Code Online (Sandbox Code Playgroud)
而且__init__.py
完全空白.
子模块__init__.py
在模块文件夹中的文件中引用.似乎subscription
没有在acme
's中引用__init__.py
.
但是,当你这样做时import world.acme.subscription
,它知道在没有交谈的情况下去挖掘那个文件夹__init__.py
.
According to your description of __init__.py
as being empty, you should import subscription
in __init__.py
.
More on how modules are set up can be seen in the documentation. There is a pretty good example setting up a sound module.