Python "ModuleNotFoundError: No module named 'tests'" in CI
Something imported tests (or tests.something) as a package, but the directory holding tests/ is not on sys.path, or the tests package is missing an __init__.py that the import style requires. CI's rootdir differs from local.
What this error means
Collection or a test fails with "ModuleNotFoundError: No module named 'tests'" when a test or helper does from tests.utils import ..., only in CI.
ImportError while importing test module '/app/tests/test_api.py'.
from tests.helpers import make_client
E ModuleNotFoundError: No module named 'tests'Common causes
The project root is not on the import path
pytest's rootdir or PYTHONPATH does not include the directory above tests/, so import tests cannot resolve.
Inconsistent package vs rootdir import mode
With the default import mode, a missing __init__.py or a conftest at the wrong level changes how tests is resolved between local and CI.
How to fix it
Put the project root on the path
Configure pytest to add the rootdir to sys.path so tests is importable.
[tool.pytest.ini_options]
pythonpath = ["."]Make tests a real package
- Add
tests/__init__.pyif you import submodules astests.x. - Keep a
conftest.pyat the repo root so pytest sets rootdir correctly. - Re-run so
from tests... importresolves consistently.
How to prevent it
- Set
pythonpath = ["."]in the pytest config. - Keep a root-level
conftest.pyto anchor rootdir. - Be consistent about whether
testsis a package.