Consider this a more refined version of this question.
It seems python's module structure is tied to actual directory containing the files. While it is possible to re-export another module using __init__.py
, this does not alter the actual module hierachy as log as the import facility is concerned.
For instance:
some_dir
├ main.py
└ mod_a
├ __init__.py
└ mod_b
└ mod_c.py
mod_a/__init__.py
:
from .mod_b import mod_c
mod_a/mod_b/mod_c.py
:
def foo():
print("Foo!")
In this case, we can do this in main.py
:
from mod_a import mod_c
mod_c.foo()
but not this:
import mod_a.mod_c
mod_c.foo()
which fails with:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import mod_a.mod_c
ModuleNotFoundError: No module named 'mod_a.mod_c'
So init.py can't exactly alter module hierachy; And this is the closest thing to 'altering module hierachy' in python I know of.
So, is there a way to the alter the module hierachy? As in:
- a way to make
import mod_a.mod_c
a valid import statement? - a way to mount some python module in arbitrary path to some import-path, in general?