I have the following folder structure:
I want to access A.py from D.py (3 levels up). I have added an init.py to all the directories in this project to initialize them as packages. When I try the following in D.py:
from test1 import A
A.hi()
I get the following error:
ModuleNotFoundError: No module named 'test1'
I am using Windows 10, Visual Studio Code and python 3.9.6 and I even added all the test1, test2, test3 folders to path variable. I think it's worth mentioning that Visual Studio does seem to recognize these folders as packages as it shows them in the dropdown list when typing module names at import.
EDIT: I have managed to access a Z.py created in test1 from test3 D.py with this:
import sys
import os
sys.path.append(os.path.abspath("C:\\Users\\myusername\\Desktop\\testdbconnection"))
from test1 import Z
Z.hi()
This folder was already in Windows Path, I added it myself. Why did Python not recognize it before?
Also, I can't access anything else than what is in test1 with this.
If i want to access A.py and B.py i need to append to path C:\Users\myusername\Desktop,
if i want to access test2 C.py i need to append to path C:\Users\myusername\Desktop\testdbconnection\test1
and so on...