Alex G.P.
Alex G.P.

Reputation: 10028

Python module and __all__

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

Answers (1)

Martijn Pieters
Martijn Pieters

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

Related Questions