As documentation says:
Note that relative imports are based on the name of the current
module. Since the name of the main module is always "__main__"
,
modules intended for use as the main module of a Python application
must always use absolute imports.
So you're not supposed to use relative imports when running your script as the main module. Relative imports get resolved correctly when they get imported.
The simplest fix is:
- Be sure that your root folder is in
sys.path
so that interpreter always finds it. (If not, append it manually)
- Use absolute imports start from the root folder.
This way you lose portability of your sub-module/sub-packages but your imports work as expected no matter you run your sub-modules directly or not.
By losing portability, I mean if you decide to change the location of a sub package, you need to change all the imports in side that package even if the sob-modules point to each other inside that package.