0

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)

wiziruv
  • 55
  • 6
  • Importing with the from statement usually imports a function of your module and not the module itself.Try the following instead: import Functions1.u1or from From Functions1.u1 import your_module – J. P. Sep 22 '20 at 05:38
  • Paths are still not correct – wiziruv Sep 22 '20 at 05:56
  • You could use this: `import os` `cwd = os.getcwd()` This gives you your current working directory. From there you know your paths to your subfolders – J. P. Sep 22 '20 at 06:20
  • 1
    I think I got what I was looking for:https://stackoverflow.com/questions/11274040/os-getcwd-vs-os-path-abspathos-path-dirname-file#:~:text=path.-,abspath(os.,be%20changed%20using%20the%20os. I guess the abspath just needs to be corrected – wiziruv Sep 22 '20 at 06:29

2 Answers2

1

at each level of module you need an __ini__.py file per https://stackoverflow.com/a/448279/14306518

Pierre-Selim
  • 131
  • 2
  • I tried adding init files for Project|Functions1|Functions2, When I tried importing u1 from main, I still get module u2 not found – wiziruv Sep 22 '20 at 05:55
1

I just need to correct my abspath to os.path.abspath(os.path.dirname(__file__)) instead of the above and add some init.py as per Pierre-Selim. all working now. Thanks

(https://stackoverflow.com/questions/11274040/os-getcwd-vs-os-path-abspathos-path-dirname-file#:~:text=path.-,abspath(os.,be%20changed%20using%20the%20os.)

wiziruv
  • 55
  • 6