Python import inconsistent behavior
I have a py file like this, which errors out.
from world import acme
def make_stuff_happen():
acme.accoun开发者_开发问答t.foo() # Works
acme.subscription.bar() # FAIL: "module 'object' has no attribute 'subscription'"
make_stuff_happen()
But this works!
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()
All I can say is WTF, What could be causing this?
The behavior should at least be consistent for both acme.account
and acme.subscription
.
Thanks!
Update- Folder structure of the acme folder:
acme
|-- __init__.py
|-- account.py
|-- catalog.py
|-- core.py
|-- proxy.py
|-- subscription.py
`-- utils.py
And __init__.py
is completely blank.
Submodules are referenced in the __init__.py
file in the module folder. It appears that subscription
is not referenced in acme
's __init__.py
.
However, when you do import world.acme.subscription
, it knows to go digging in that folder without talking to __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.
精彩评论