Skip to content
Latchkey

pytest "ImportError while importing test module" in CI

pytest tried to import a test module during collection and the import itself raised. The hint points at duplicate filenames, a missing __init__.py, or a dependency the test imports that is not installed.

What this error means

pytest reports "ERROR collecting tests/test_x.py - ImportError while importing test module" followed by the underlying import error and a hint about test module names.

pytest
ImportError while importing test module '/home/runner/work/app/tests/test_api.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_api.py:2: in <module>
    import requests
E   ModuleNotFoundError: No module named 'requests'

Diagnose it: what did pytest actually collect?

Most pytest failures that only happen in CI are collection or import-path problems rather than test failures. The runner has a different working directory, a different sys.path, and usually no editable install, so a test module that imports your package locally may not resolve at all.

Terminal
# what would run, without running it
pytest --collect-only -q | tail -20

# where pytest thinks the root is (drives conftest and import mode)
pytest --collect-only 2>&1 | grep -i rootdir

# is the package importable at all from the runner cwd?
python -c "import yourpackage, sys; print(yourpackage.__file__)"
python -c "import sys; print(sys.path)"

Common causes

A dependency the test imports is missing

The test file imports a package not installed in the CI environment, so its import fails at collection.

Duplicate test filenames without packages

Two test_x.py files in different folders with no __init__.py collide under the default import mode.

How to fix it

Install the missing dependency

Ensure test dependencies are installed before pytest runs.

Terminal
pip install -r requirements-dev.txt
pytest

Disambiguate duplicate test modules

Add __init__.py files or switch to importlib import mode so same-named files do not collide.

pytest.ini
[pytest]
addopts = --import-mode=importlib

Make a zero-test run fail the build

Terminal
# fail explicitly when nothing is collected
pytest --strict-markers -q
test "${PIPESTATUS[0]}" -ne 5 || { echo "pytest collected no tests"; exit 1; }

How to prevent it

  • Install all test dependencies (a dev requirements set) in CI.
  • Use unique test filenames or package the test tree with __init__.py.
  • Consider --import-mode=importlib to avoid name collisions.

Frequently asked questions

What causes pytest "ImportError while importing test module" in CI?
There are 2 common causes: a dependency the test imports is missing and duplicate test filenames without packages. The test file imports a package not installed in the CI environment, so its import fails at collection.
How do I fix pytest "ImportError while importing test module" in CI?
There are 2 fixes depending on which cause you have: install the missing dependency and disambiguate duplicate test modules. Work through them in order, since the first is the most common.
What does pytest "ImportError while importing test module" in CI actually mean?
pytest reports "ERROR collecting tests/test_x.py - ImportError while importing test module" followed by the underlying import error and a hint about test module names.
How do I stop pytest "ImportError while importing test module" in CI happening again?
Install all test dependencies (a dev requirements set) in CI. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card