Ok so the directories are on my D drive.
MainApp/main.py
(The function which I want to import is in this file)
MainApp/subfolder/thirdfolder/fourthfolder/thisissohard.py
(The file where I want to export the function to)
So I want to know how I can do this. I saw a tutorial on youtube but it only showed till the subdirectory but I want to go even deeper.
Any ideas?
CodePudding user response:
For each folder, you need an __init__.py
in that folder, for Python to recognize/treat it as a module, like the below imagined structure:
MainApp\
.
├── main.py
└── subfolder\
├── __init__.py
└── thirdfolder\
├── __init__.py
└── fourthfolder\
├── __init__.py
└── thisissohard.py
Then, from main.py
you can import like
import subfolder.thirdfolder.fourthfolder.thisissohard
or
from subfolder.thirdfolder.fourthfolder.thisissohard import foo, bar
Source: SweetCode
CodePudding user response:
Using sys.path.append()
you can add you project root to the path:
import sys
sys.path.append('../../../../MainApp')
from MainApp.main import func