Report #45121
[bug\_fix] ModuleNotFoundError after \`pip install -e .\` in a src-layout project, or \`importlib.metadata.PackageNotFoundError\` when querying version of an editable install
Ensure \`pyproject.toml\` contains \`\[tool.setuptools\]\` with \`package-dir = \{"" = "src"\}\` \(for setuptools\) to map the package root correctly during editable installs, ensuring the generated \`.pth\` file points to the \`src/\` directory rather than the repository root.
Journey Context:
Developer clones a modern Python project using the src-layout \(\`src/mypackage/\_\_init\_\_.py\`\) with a \`pyproject.toml\` using setuptools as the build backend. They create a virtual environment and run \`pip install -e .\` to install in editable mode. The command succeeds with no errors. However, when they open a Python REPL and type \`import mypackage\`, they receive \`ModuleNotFoundError: No module named 'mypackage'\`. Investigating \`site-packages\`, they find a file named \`\_\_editable\_\_.mypackage.pth\`. Opening it reveals it contains the path to the project root \(\`/home/user/project\`\), not the \`src/\` subdirectory where the actual package code resides. The developer consults the setuptools documentation and realizes that for src-layout projects, \`pyproject.toml\` must explicitly declare \`package-dir = \{"" = "src"\}\` under \`\[tool.setuptools\]\`. Without this, setuptools assumes the package is at the repository root during the editable install hook \(PEP 660\), generating an incorrect path. Adding the configuration and reinstalling with \`pip install -e .\` updates the \`.pth\` file to point to \`src/\`, allowing Python to locate \`mypackage\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:12:24.301427+00:00— report_created — created