Skip to content
Latchkey

Python "ModuleNotFoundError: No module named X" in CI

Python could not import a module. Either it was never installed for the interpreter running your code, or your own package layout puts it somewhere not on sys.path.

What this error means

The program or test run aborts with ModuleNotFoundError: No module named X. It can be a third-party package (not installed) or one of your own modules (not importable from where it runs).

Python traceback
Traceback (most recent call last):
  File "app/main.py", line 2, in <module>
    from app.utils import helper
ModuleNotFoundError: No module named 'app'

Common causes

Package not installed for this interpreter

A third-party import fails because it was installed into a different interpreter or venv than the one running the code.

Your own package is not on sys.path

For first-party imports like from app..., the project root is not on sys.path - usually because the package was not installed (pip install -e .) and CI runs from a different directory.

Missing __init__.py or wrong layout

A directory that should be an importable package may be missing an __init__.py, or the src-layout means the package only resolves after an editable install.

How to fix it

Install the dependency into the active interpreter

Terminal
python -m pip install <package>
python -c "import <package>; print(<package>.__file__)"

Install your project so first-party imports resolve

An editable install puts your package on sys.path regardless of working directory.

Terminal
pip install -e .
# or for src-layout projects, ensure pyproject defines the package

Confirm the interpreter and path

  1. Run which python / python -m site to see the interpreter and its site-packages.
  2. If a venv is involved, make sure it is the one running the failing step.
  3. For first-party modules, prefer an editable install over sys.path hacks.

How to prevent it

  • Install your project with pip install -e . in CI so imports are stable.
  • Use one consistent venv across all steps.
  • Adopt a clear src-layout and declare packages in pyproject.toml.

Related guides

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