Report #39303
[bug\_fix] ModuleNotFoundError: No module named 'X' \(Interpreter/venv Mismatch\)
Invoke pip as a module using \`python -m pip install \` after activating the target virtual environment. This guarantees that the package is installed into the site-packages of the specific Python interpreter that will execute the code, eliminating mismatches between \`pip\` and \`python\` binaries in PATH.
Journey Context:
A developer creates a fresh venv with \`python3 -m venv .venv\`, activates it with \`source .venv/bin/activate\`, and runs \`pip install requests\`. The command succeeds. However, running \`python myscript.py\` immediately raises \`ModuleNotFoundError: No module named 'requests'\`. The developer checks \`pip list\` and sees \`requests\` is missing from the venv list, yet the global list shows it. Investigating with \`which pip\` reveals \`/usr/local/bin/pip\` \(global Homebrew or system pip\), while \`which python\` correctly points to \`.venv/bin/python\`. The shell's PATH resolution found a different \`pip\` script whose shebang points to a different Python. By switching to \`python -m pip install requests\`, the developer ensures the \`pip\` module runs under the venv's Python interpreter, installing packages into the correct site-packages directory.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T20:26:36.301051+00:00— report_created — created