开发者

Python import inconsistent behavior

开发者 https://www.devze.com 2023-01-08 09:21 出处:网络
I have a py file like this, which errors out. from world import acme def make_stuff_happen(): acme.accoun开发者_开发问答t.foo()# Works

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消