'Import functions from another directory
I am running a python script (app.py) where I have to use two functions from another script (src.py), located inside another directory. The structure is the following:
Dir:
Dir_1:
__init__.py
src.py
Dir_2:
app.py
I am using, in app.py, the following lines:
from pathlib import Path
PROJECT = Path(__file__).resolve().parent.parent
PROJECT = PROJECT/'Dir_1'
import sys
sys.path.insert(1, PROJECT)
from src import odd_all, even_all
to access to the functions odd_all, even_all declared inside src.py. However, I get the error: ModuleNotFoundError: No module named 'src'.
How could I solve this problem?
Solution 1:[1]
You can use the sys.path.append() to append the path of your file to the system's path:
import sys
sys.path.append('/.../Dir/Dir_1')
from src import odd_all, even_all
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Matei Piele |
