How do I fix ModuleNotFoundError: No module named 'src' when running pytest tests/ -v on Linux?
Problem:
When running automated tests on Linux (Ubuntu/Debian) with Python 3.10+, pytest fails with:
ModuleNotFoundError: No module named 'src' even though the src/ directory exists.
Why it happens:
Unlike some IDE environments, standard Linux pytest does not automatically add the project root directory (.) to the Python module search path (PYTHONPATH).
Solution:
There are two ways to permanently fix this:
- Create a
pytest.ini file in your repository root:
[pytest]
pythonpath = .
testpaths = tests
How do I fix
ModuleNotFoundError: No module named 'src'when runningpytest tests/ -von Linux?Problem:
When running automated tests on Linux (Ubuntu/Debian) with Python 3.10+,
pytestfails with:ModuleNotFoundError: No module named 'src'even though thesrc/directory exists.Why it happens:
Unlike some IDE environments, standard Linux
pytestdoes not automatically add the project root directory (.) to the Python module search path (PYTHONPATH).Solution:
There are two ways to permanently fix this:
pytest.inifile in your repository root: