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?
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:
from pathlib import Path PROJECT = Path(__file__).resolve().parent.parent PROJECT = str(PROJECT)+'/Dir_1' import sys sys.path.insert(1, PROJECT) from src import odd_all, even_all
Now everything works perfectly.
