Sphere
Log in Join

Fixing `ModuleNotFoundError: No module named 'src'` in pytest on Ubuntu 22.04 CI

I'll keep the story short because the fix is boring, but the debugging wasn't. Our test suite ran fine on my laptop with Python 3.11 and died on the Ubuntu 22.04 CI runner as soon as we added a test that imported our own package. Classic import path problem, and the error text doesn't tell you that.

## The traceback

$ python -m pytest -q
==================================== ERRORS ====================================
___________________ ERROR collecting tests/test_api.py ____________________
ImportError while importing test module '/home/runner/app/tests/test_api.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_api.py:4: in <module>
from src.api import app
E ModuleNotFoundError: No module named 'src'
=========================== short test summary info ============================
ERROR tests/test_api.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!

I'd run pip install -r requirements.txt in the job, so I assumed dependencies were fine. They were. The missing module was our own code.

## Why it worked locally

My shell was in the repo root and I'd added that root to PYTHONPATH in my .zshrc months ago for unrelated reasons. pytest adds the test file's directory to sys.path under its default import mode, which here was tests/, not the repo root. So src was only importable locally because of my personal environment, not because the project was set up correctly. CI had no such env var.

I confirmed by printing the path in a failing job:

python -c "import sys; print(sys.path)"

The repo root wasn't there.

One more diagnostic that would have saved me time: pytest --collect-only -q prints the rootdir and the collected test paths before running anything. Our rootdir was correct but the import path wasn't, which is a distinction I hadn't appreciated. There's also python -m pytest versus plain pytest: they can put different things on sys.path, and I've seen that alone flip an import from working to failing. We standardised on python -m pytest everywhere after this so local and CI behave the same.

## The fix I'd recommend

Set the import path in config so it travels with the repo. In pytest.ini at the root:

[pytest]
pythonpath = .
testpaths = tests

The pythonpath option needs pytest 7 or newer; we were on 8.1.1. If you're stuck on an older pytest, add a conftest.py in the root that inserts the path, or install the package with pip install -e ., which is arguably the cleaner answer for a real project.

## The alternative that's probably better long-term

We do have a pyproject.toml, so installing the project editable in CI is the more honest setup:

python -m pip install -e .
python -m pytest -q

That makes the test environment match how the app is actually deployed. I went with pytest.ini first because it was a one-line diff and we had a release that afternoon. We've since moved to the editable install. Both work; the editable one explains itself better to the next person.

## What bit us second

After the import fix, one test still failed with a different error because CI's working directory was the repo root but the test opened a file with a relative path config/settings.yaml that only resolved from tests/. Two unrelated bugs wearing the same "works on my machine" coat. Worth checking once you fix the first one.