I am getting a strange module not found error and I can't seem to grasp why it is happening.
I have a directory structure of:
projects/services/skittles/app/
database/
--__init__.py
--testresults.py
models/
--__init__.py
--testresult.py
results.py imports:
from app.models.testresult import TestResult, TestRunDetails, Step
app.models.testresult is a file with pydantic models
When I try to run testresults.py directly with:
python testresults.py
I get:
ModuleNotFoundError: No module named 'app'
I ran:
import os try: user_paths = os.environ['PYTHONPATH'].split(os.pathsep) except KeyError: user_paths = []
and printing user_paths outputs:
[':/c/Users/xxxxxxx/Desktop/projects/']
Please help me understand what is happening. Is the PYTHONPATH incorrect? Seems to me like it is looking into the projects directory, but should it be pointing into the individual folders as well?
To fix this issue, you can modify your import statement in results.py to specify a relative import path. Since results.py is within the database folder and you're trying to import from app.models.testresult, you can use relative imports like this:
from ..models.testresult import TestResult, TestRunDetails, Step