Given this simple folder structure
/main.py
/project/a.py
/project/b.py
main.py
is executed by the python interpreter and contains a single line, import project.a
.
a
and b
are modules, they need to import each other. A way to achieve this would be
import project.[a|b]
When working with deeper nested folder structures you don't want to write the entire path everytime you use a module e.g.
import project.foo.bar
project.foo.bar.set_flag(project.foo.bar.SUPER)
Both from project import [a|b]
and import project.[a|b] as [a|b]
result in an import error (when used in both, a
and b
).
What is different between the standart import syntax and the from
or as
syntax? Why is only the standart syntax working for mutual imports?
And more importantly, is there a simple and clean way to import modules that allows mutual imports and assigning shorter names to them (ideally the modules basename e.g. bar
in the case of project.foo.bar
)?