Skip to content
Latchkey

pytest "ImportError while loading conftest" in CI

pytest imports every applicable conftest.py before collecting tests. If one fails to import, the entire session aborts with ImportError while loading conftest - no tests run at all.

What this error means

pytest exits early (usage error, exit code 4) with ImportError while loading conftest '.../conftest.py' and a traceback ending in a ModuleNotFoundError or other import failure. Nothing is collected.

pytest output
ImportError while loading conftest '/repo/tests/conftest.py'.
tests/conftest.py:3: in <module>
    from app.fixtures import build_client
E   ModuleNotFoundError: No module named 'app'

Common causes

conftest imports something not installed/importable

A conftest.py imports your package or a dependency that is not installed in CI, or not on sys.path, so importing conftest fails before collection.

A plugin or fixture import error

conftest imports a pytest plugin or helper that errors at import (missing package, bad path), taking the whole session down with it.

How to fix it

Install the project and test deps

Make the imports in conftest resolvable, usually with an editable install plus test extras.

Terminal
pip install -e ".[test]"
pytest

Reproduce the import directly

  1. Run python -c "import app.fixtures" (the failing import) to confirm the root cause.
  2. Fix pythonpath/rootdir so the package resolves, or install the missing dependency.
  3. Keep heavy or optional imports inside fixtures, not at conftest module top-level.

How to prevent it

  • Install the project editable (pip install -e .) so conftest imports resolve.
  • Declare and install all test dependencies in CI.
  • Avoid import-time side effects in conftest.py.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →