Report #38281
[bug\_fix] ModuleNotFoundError: No module named 'src' or ModuleNotFoundError: No module named 'app' in Docker
Set the \`PYTHONPATH\` environment variable in the Dockerfile to include the directory containing your packages: \`ENV PYTHONPATH=/app\`. Alternatively, adjust the \`WORKDIR\` to be the directory containing the code \(e.g., \`WORKDIR /app/src\`\), or install the package in the Dockerfile using \`pip install /app\` so imports are absolute from site-packages.
Journey Context:
Developer has a Python project using the src-layout: \`src/myapp/\_\_init\_\_.py\`. They write a Dockerfile: \`FROM python:3.11\`, \`COPY . /code\`, \`WORKDIR /code\`, \`CMD \["python", "src/myapp/main.py"\]\`. Locally they run \`python src/myapp/main.py\` from the repo root and it works \(because current dir is in sys.path\). In Docker, it fails with \`ModuleNotFoundError: No module named 'myapp'\` or \`No module named 'src'\`. Developer tries adding \`\_\_init\_\_.py\` to \`src/\` \(making it a package\), then changes import to \`from src.myapp import ...\`, which works in Docker but breaks local development if not run from root. Eventually, they learn that \`WORKDIR\` in Docker sets the working directory, and Python adds the script's directory to sys.path, not necessarily the workdir. They fix it by setting \`ENV PYTHONPATH=/code\` so \`import myapp\` works from \`/code/src\`, or by installing the package with \`pip install -e .\` in the Dockerfile so imports are from site-packages.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:44:01.700549+00:00— report_created — created