Reputation: 2930
I often do this:
└─ mymodule
├─__init__.py
├─ MyClass1.py
...
└─ MyClass2.py
And each file contains a single class, named accordingly MyClass1, MyClass2.
I know, I know, not pythonic of me, but I believe that convention should not limit my possibilities.
But, I encounter problems that I can not solve on my own - python import rules defy my logic.
Could you please help me, if not understand, but at least to have a way to do what I want. I want to compose my __init__.py
so, that "mymodule" behaves as if all classes were in one file "mymodule.py".
I.e. from outside file, I want to be able to do:
from mymodule import MyClass1
And it would give me the class, and not the module, without having to do MyClass1.MyClass1.
Is there a way to do this in Python?
Upvotes: 0
Views: 55
Reputation: 12202
In __init__.py
:
from .MyClass1 import MyClass1
from .MyClass2 import MyClass2
Then you can use your import:
from mymodule import MyClass1
Upvotes: 1
Reputation: 2406
You can import it like this:
from mymodule.MyClass1 import MyClass1
Upvotes: 1