I am struggling with nested __init__.py
in a Python package I am writting. The Package has the following architecture:
module/
├── __init__.py
├── submodule1
│ ├── __init__.py
│ └── source.py
└── submodule2
├── __init__.py
├── source.py
└── subsubmodule2
├── __init__.py
└── source.py
My intent is to be able to access functions defined in submodule2/source.py
through module.submodule2.function
and in subsubmodules2/source.py
through module.submodule2.subsubmodule2.function
.
The first thing I tried was to define __init__.py
in submodule2
this way:
from .subsubmodule2 import *
But doing so, I get the functions defined in subsubmodules2/source.py
through module.submodule2.function
(and module.function
).
If I do:
from . import subsubmodule2
I get these functions through module.subsubmodule2.function
.
I also tried to define __all__
keyword in __init__
, with no more success. If I understand well Python documentation, I guess I could leave empty __init__.py
files and it could work, but from my understanding that is not the best practice either.
What would be the best way to access these functions as intended in my module?