ModuleNotFoundError: No module named 'app.project' when running pytest on a Django project in GitLab CI
I'm having trouble running my tests with pytest in a Django project on GitLab CI. My project structure looks like this:
my-project/
├── app/
│ ├── app/
│ │ ├── settings.py
│ │ ├── (other Django files)
│ │ └── __init__.py
│ └── project/
│ ├── __init__.py
│ ├── models.py
│ └── tests/
│ ├── __init__.py
│ └── test_models.py
├── pytest.ini
└── conftest.py
In my test file (project/tests/test_models.py), I import my modules like this:
from project.models import User, Course, Lesson, Exercise
I also have the following in my pytest configuration (pytest.ini):
[pytest]
DJANGO_SETTINGS_MODULE = app.app.settings
When I run pytest from GitLab CI pipeline:
docker --context digitavox compose -f docker-compose.yml exec -T app bash -c 'export DJANGO_SETTINGS_MODULE=app.settings && export PYTHONPATH=$(pwd):$(pwd)/app && pytest -v --collect-only --disable-warnings'
I get the following error: ModuleNotFoundError: No module named 'app.project'
I have verified that:
- All necessary init.py files are present.
- The tests use the import statement from project.models import ... (without the “app.” prefix).
- The conftest.py file is located at the project root, so the project root should be added to sys.path automatically when pytest runs.
My question is: How should I configure the PYTHONPATH or adjust my imports/structure so that pytest can correctly locate my modules when running on GitLab CI? Do I need to add any extra configuration in pytest.ini or modify the way I run the tests?
Any help is appreciated!