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:
JavaScript
x
7
1
Dir:
2
Dir_1:
3
__init__.py
4
src.py
5
Dir_2:
6
app.py
7
I am using, in app.py, the following lines:
JavaScript
1
8
1
from pathlib import Path
2
PROJECT = Path(__file__).resolve().parent.parent
3
PROJECT = PROJECT/'Dir_1'
4
5
import sys
6
sys.path.insert(1, PROJECT)
7
from src import odd_all, even_all
8
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?
Advertisement
Answer
I managed to solve the problem. The issue was that PROJECT is a Posixpath object, while SYS accepts only strings. Therefore, the solution was just to transform PROJECT to string:
JavaScript
1
8
1
from pathlib import Path
2
PROJECT = Path(__file__).resolve().parent.parent
3
PROJECT = str(PROJECT)+'/Dir_1'
4
5
import sys
6
sys.path.insert(1, PROJECT)
7
from src import odd_all, even_all
8
Now everything works perfectly.