I have a file structure as follows:
└── project
├── __init__.py
├── main.py
└── package1
├── __init__.py
├── module1
└── subpackage
├── __init__.py
└── module2.py
module2 contains a function called A, and module 1 imports module2 with the code:
from subpackage import module2
this works fine when running module1 directly, but when I then try to import module1 from main.py, I get an error. The code for the import statement in main.py is
from package1 import module1
this gives the error
Traceback (most recent call last):
File "C:\...\project\main.py", line 1, in <module>
from package1 import module1
File "C:\...\project\package1\module1.py", line 1, in <module>
from subpackage import module2
ModuleNotFoundError: No module named 'subpackage'
I don't really understand what is happening here, I tried changing the current working directory with os.chdir() to package2 in module1, but this had no effect. Upon researching the problem, the only thing I could find that might relate to my problem was absolute vs relative imports, but changing the import statements didn't effect the error.