Skip to content
Latchkey

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.

pytest
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.

pyproject.toml
[tool.pytest.ini_options]
pythonpath = ["."]

Make tests a real package

  1. Add tests/__init__.py if you import submodules as tests.x.
  2. Keep a conftest.py at the repo root so pytest sets rootdir correctly.
  3. Re-run so from tests... import resolves consistently.

How to prevent it

  • Set pythonpath = ["."] in the pytest config.
  • Keep a root-level conftest.py to anchor rootdir.
  • Be consistent about whether tests is a package.

Frequently asked questions

What causes ""No module named 'tests'""?
pytest's rootdir or PYTHONPATH does not include the directory above tests/, so import tests cannot resolve.
How do I fix "No module named 'tests'"?
Configure pytest to add the rootdir to sys.path so tests is importable.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card