Report #7967
[bug\_fix] ModuleNotFoundError / bad interpreter: No such file or directory after system Python upgrade
Recreate the virtual environment from scratch \(\`rm -rf venv && python3 -m venv venv\`\) because virtual environments embed absolute paths to the interpreter.
Journey Context:
Developer creates a venv using \`python3.9 -m venv venv\` on macOS with Homebrew. Weeks later, they run \`brew upgrade [email protected]\`, which changes the Cellar path from \`/usr/local/Cellar/[email protected]/3.9.16/Frameworks/...\` to \`3.9.17\`. Upon activating the old venv and running \`pip install\`, they get \`/path/to/venv/bin/pip: /usr/local/opt/[email protected]/bin/python3.9: bad interpreter: No such file or directory\`. Investigating with \`cat $\(which pip\)\` reveals a shebang \(\`\#\!\`\) with the absolute old path. Attempts to manually edit shebangs in \`venv/bin/\*\` are tedious and fragile. Research reveals that virtual environments are intentionally not relocatable \(PEP 405\) and embed absolute paths for performance and correctness. The only robust solution is to delete and recreate the venv, then reinstall dependencies from \`requirements.txt\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:14:32.935164+00:00— report_created — created