Import mutable Python behavior

I have a py file similar to this due to which there are no errors.

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()

But it 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? Behavior must be at least consistent both for acme.accountand acme.subscription.

Thank!

Update - acme folder folder structure:

acme
|-- __init__.py
|-- account.py
|-- catalog.py
|-- core.py
|-- proxy.py
|-- subscription.py
`-- utils.py

And __init__.pycompletely empty.

+3
source share
1 answer

Submodules are referenced in a file __init__.pyin the module folder. It subscriptiondoesn't seem to be mentioned in acme __init__.py.

However, when you do import world.acme.subscription, he knows what to dig into this folder without talking to __init__.py.

__init__.py , import subscription __init__.py.

. .

+6

Source: https://habr.com/ru/post/1756232/


All Articles