Reputation: 10028
I trying to understand how to manage module with __all
. For example, I have following structured code:
main.py
|=> /database
|=> __init__.py
|=> engine (with variables engine, session, etc.)
now I want to be able to import session
and engine
instances directly from database
module like:
from database import session
I tried to add line __all__ = ['session']
or __all__ = ['engine.session']
to __init__py
but when I trying to do import I've got an exception AttributeError: 'modile' object has not attribute 'engine.session'
.
Is there any way to achieve wanted behavior?
Upvotes: 4
Views: 2469
Reputation: 1124020
Listing names in __all__
does not, by itself, import items into a module. All it does is list names to import from that module if you used from database import *
syntax.
Import session
into database/__init__.py
:
from .engine import session
Upvotes: 5