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
- Add the package to your test/dev dependencies (extras or requirements-dev).
- Re-lock so a clean CI install includes it.
- 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.importorskipfor genuinely optional dependencies.
Related guides
pytest "ImportError while loading conftest" in CIFix pytest "ImportError while loading conftest '...conftest.py'" in CI - a broken import, missing dependency,…
pytest Exit Code 4 "usage error" - Fix Bad Options & Config in CIFix pytest exit code 4 "usage error" in CI - an unrecognized option, a bad addopts entry, or a missing plugin…
pytest-xdist "worker ... crashed" / "Replacing crashed worker" in CIFix pytest-xdist "node down: Not properly terminated" / "replacing crashed worker" in CI - a segfault, OOM ki…