Skip to content
Latchkey

Python "ModuleNotFoundError: No module named X" import in CI

Python could not find the named module on sys.path at import time. Either the package was never installed in the active environment, or the project's own package is not importable from the runner's working directory.

What this error means

A script or test fails with "ModuleNotFoundError: No module named 'X'", even though it imports fine on a developer machine.

python
Traceback (most recent call last):
  File "app/main.py", line 2, in <module>
    import myapp.config
ModuleNotFoundError: No module named 'myapp'

Common causes

The dependency is not installed in this environment

CI used a different interpreter or venv than expected, so the third-party package was never installed where the import runs.

The local package is not on the import path

Your own package is not installed (pip install -e .) and the working directory is not on sys.path, so import myapp fails.

How to fix it

Install dependencies into the active interpreter

  1. Confirm which Python runs the step with python -c "import sys; print(sys.executable)".
  2. Install requirements into that exact interpreter with python -m pip.
  3. For your own package, install it editable from the project root.
Terminal
python -m pip install -r requirements.txt
python -m pip install -e .

Set PYTHONPATH for a src layout

If you do not install the package, add its directory to the import path explicitly.

.github/workflows/ci.yml
env:
  PYTHONPATH: ${{ github.workspace }}/src

How to prevent it

  • Install your project (editable) so it is importable like in production.
  • Pin the interpreter with setup-python and install into it.
  • Avoid relying on the working directory being on sys.path implicitly.

Related guides

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