pytest ImportPathMismatchError / ModuleNotFoundError for `src/` Layout

Test runner executed without `src/` on `sys.path` or stale `__pycache__` from moved test.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: pytest ImportPathMismatchError / ModuleNotFoundError for `src/` Layout Platform: Python
Common Root Causes:
  • Test runner executed without `src/` on `sys.path` or stale `__pycache__` from moved test.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Run `python3 -m pytest`, install project editable (`pip install -e .`), or set `pythonpath = ["src"]`.
Safe Diagnostic Command:
python3 -m pytest --collect-only
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Test runner executed without `src/` on `sys.path` or stale `__pycache__` from moved test. Recommended diagnostic: `python3 -m pytest --collect-only`. Remediation: Run `python3 -m pytest`, install project editable (`pip install -e .`), or set `pythonpath = ["src"]`.

Worked Examples

  • Run `python3 -m pytest --collect-only` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-py-pytest-import-file-mismatch · Independently verified: 2026-11-25

Related Resources