So I saw these posts that somehow gave me an idea on how to structure my files. (Importing files from different folder) But somehow my structure has a bit more connections and I am not sure how to clean(organize them). This is my folder structure in brief:
Project
├── main.py (imports u1)
├── _init_.py
├── Functions1
├── _init_.py
├───u1.py (imports u2, gets data in subfolder data for u1)
├───Data for u1
| └── csv/xlsx files
├───u2.py ( gets data in subfolder data for u2)
├───Data for u2
| └── picture files
├───Data for u2
└── other files
├─── main2.py ( imports u1)
├── Functions2
├── _init_.py
├───u3.py
├───Data for u3
| └── csv/xlsx files
when I tried importing u1 in main.py
via from Functions1 import u1
I get an error module u2 not found
.
I tried sys.path.insert()
, but then another error comes along No such file or directory:(main's path +Data for u1)
. Now I know to fix this I just need to correct the path (main's path+Function1+Data for u1) but then My paths would no longer be relative and I would need to switch from time to time when copying the directory. How can I make that when I import u1 from main, the path where u1 get the files are from u1's path (not from main)? or somehow make the structure clean that even if I were to use u1 (from main2.py
) the import remains correct?
I use os.path.dirname(os.path.abspath("__file__"))
in u1
Edit: Added init files. My only main problem now is how to make the path relative to the file.
my u1 has pd.read_csv(os.path.dirname(os.path.abspath("__file__"))+Data for u1+file)
but when it gets imported in main.py the path used is in main (not in u1)