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.
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
- Confirm which Python runs the step with
python -c "import sys; print(sys.executable)". - Install requirements into that exact interpreter with
python -m pip. - For your own package, install it editable from the project root.
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.
env:
PYTHONPATH: ${{ github.workspace }}/srcHow 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.pathimplicitly.