Report #10814
[bug\_fix] ModuleNotFoundError when importing from src layout during testing
Install package in editable mode with 'pip install -e .' configured with 'package\_dir=\{"": "src"\}', or set PYTHONPATH=src.
Journey Context:
Developer uses modern 'src' project structure: 'src/mypackage/\_\_init\_\_.py'. They run 'pytest' from repo root, or open Python REPL and try 'import mypackage'. Gets ModuleNotFoundError. They try to fix by adding 'sys.path.insert\(0, os.path.abspath\("src"\)\)' to conftest.py or test files. This works temporarily but feels hacky and breaks when installed. They realize that Python doesn't automatically add 'src/' to sys.path because it's not installed as a package; Python only knows about the current directory and site-packages. The proper fix is to install the package in editable mode using 'pip install -e .' with pyproject.toml configured with 'package\_dir=\{"": "src"\}' \(setuptools\) or equivalent \[tool.setuptools.packages.find\] where=\["src"\], which adds the src layout to the path properly via an .pth file or metadata link. For testing without install, set PYTHONPATH=src before running pytest.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:44:37.032333+00:00— report_created — created