I have a project in python that uses a file structure similar to this:
program/
main.py
SubFolder/
module1.py
module2.py
TestFolder/
test.py
In this structure main.py
imports module2.py
and module2.py
imports module1.py
. Also test.py
imports module2.py
.
I have two problems with the imports in this folder structure.
When running the main.py
the import in module2.py
is as follows:
from SubFolder.module1 import *
Here the the folder of main.py
is set as the root folder and all the imports are done from there. While when I run module2.py
directly the import should be as follows:
from module1 import *
Because now the root folder is the folder of module2.py
. This is a problem, because now the import statements constantly need to be changed when running from different files. Is there any solution to this?
The second problem is when I want to run test.py
, this file imports module2.py
. The way to import this should be:
from ..SubFolder.module2 import *
But this gives the following error message:
from ..SubFolder.module2 import *
ImportError: attempted relative import with no known parent package
I have looked at a lot of solutions for this, but could not find one that worked. Is there any way to fix this.
Is there someone that could explain to me how I could resolve these issues?
I have already looked at this: Relative imports in Python 3, but this does not resolve my issue.