Report #8380
[bug\_fix] ModuleNotFoundError: No module named 'my\_package' after running \`pip install -e .\` inside a Docker container
Upgrade to \`pip>=21.1\` and \`setuptools>=64\` to enable PEP 660 modern editable installs which correctly handle src-layout via import hooks rather than \`.pth\` files. Alternatively, explicitly set \`PYTHONPATH=/app/src\` in the Dockerfile environment, or ensure \`setup.py\`/\`pyproject.toml\` correctly declares \`package\_dir=\{"": "src"\}\`.
Journey Context:
A developer is containerizing a Python service. The repo uses a \`src/\` directory structure \(\`src/my\_package/\_\_init\_\_.py\`\). The Dockerfile has \`COPY . /app\`, \`WORKDIR /app\`, \`RUN pip install -e .\`. The build succeeds. When running the container \`docker run myimage python -c "import my\_package"\`, it fails with \`ModuleNotFoundError\`. The developer checks \`pip list\` inside the container and sees \`my-package\` listed with version \`0.0.0\`. They inspect \`/usr/local/lib/python3.9/site-packages/\` and find a file \`my-package.egg-link\` containing a path like \`/app/src/my\_package\`. They realize the path points to \`/app/src\` but the \`.egg-link\` \(legacy editable install\) relies on the path being directly importable or added via a \`.pth\` file, which may not be set up correctly in the container's site-packages if \`setup.py\` doesn't specify \`package\_dir\`. The legacy mechanism simply creates a link file; if the src layout isn't recognized, Python doesn't know to look in \`src/\`. Modern PEP 660 editable installs use a meta-path finder that intercepts imports and handles src-layout dynamically. Upgrading pip/setuptools fixes it by switching to the new mechanism. Alternatively, setting \`PYTHONPATH=/app/src\` manually adds the directory to \`sys.path\`, bypassing the need for the editable install to manage the path.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:19:29.696411+00:00— report_created — created