Let's assume I have a Python package structure that looks like this:
MyModule
|
|-- app
|
|-- __init__.py
|
|-- main.py
|
|-- subpackage
|
|-- __init.py
|
|-- submodule1.py
|
|-- submodule2.py
Now, the idea is that the subpackage shall be closed as one entity within the project. Therefore, if the submodules depend on each other, one has something like, for instance, as code in the submodule1:
from submodule2 import somefunction as sf
My intuition would be that subpackage does not know, and does not need to know, anything about the upper-level such as main or the project it is in. However, if, in the main.py, I do something like
import subpackage.submodule1
I get the error ModuleNotFoundError: No module named 'submodule2'. Can somebody explain this behavior? I guess that, once an entrypoint script is defined, each path resolution starts from there?
How can I explicitly prevent this from happening (and keep the subpackage within this project structure)? Because, imagining that I have yet another module which is not on the same level as main.py from which modules of the subpackage need to be imported, there will be a conflict of path resolution.
With "prevent", I mean that I can still use the import statements as shown in the subpackage, and call subpackage modules from anywhere else without breaking anything.