Skip to content
Latchkey

pytest "E ModuleNotFoundError" in a Test - Missing Dependency in CI

A test imported a module that is not installed in CI. pytest marks the line with E and reports it as a collection error for that module, so those tests never run.

What this error means

A test file fails to import during collection with E ModuleNotFoundError: No module named 'X', where X is a third-party package or an optional dependency that is installed locally but not in CI.

pytest output
_____________ ERROR collecting tests/test_email.py _____________
tests/test_email.py:4: in <module>
    import aiosmtplib
E   ModuleNotFoundError: No module named 'aiosmtplib'

Common causes

Test/dev dependency not installed in CI

The package is a test-only or optional dependency that is present on your machine but was never installed in the CI job.

Dependency missing from the lockfile/extras

The import was added without declaring the dependency, so a clean CI install does not pull it in.

How to fix it

Install the missing dependency in CI

Terminal
pip install aiosmtplib
# better: declare it and install the test extra
pip install -e ".[test]"

Declare it where it belongs

  1. Add the package to your test/dev dependencies (extras or requirements-dev).
  2. Re-lock so a clean CI install includes it.
  3. If it is optional, guard the import and skip the test when absent (pytest.importorskip).

How to prevent it

  • Install with pip install -e ".[test]" (or requirements-dev) in CI.
  • Declare every test import as a tracked dependency.
  • Use pytest.importorskip for genuinely optional dependencies.

Related guides

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