Assume you have a package foo with the following __init__.py:
from bar import *
With bar being any python module installed with pip install bar.
Now what always works when you can import bar:
from bar import submodule #works
import bar.submodule #works, too
Now I would assume the following things would all work as well:
from foo import submodule # a) is possible
import foo.submodule # b) not possible ("no module named submodule")
from foo.bar import submodule # c) also impossible ("no module named submodule")
Why are they not possible? What would I need to do to make them possible, from the point of view of the foo maintainer?
submoduleandbarare members of thefoomodule object, not submodules of it. Therefore, they behave just like any other member attribute offoo. You can bring them into the module namespace of a third module with thefrom foo import ...form, but you can’t directlyimportthem relative tofoo. I guess you could if you hacked them in tosys.modulesunder the desired name manually, but you really shouldn’t be doing anything like that…To illustrate the issue:
foo.pybar.pybaz.py