Report #42930
[bug\_fix] Venv interpreter mismatch causing ModuleNotFoundError or installing packages to wrong environment
Always use \`python -m pip install \` instead of the bare \`pip\` command to ensure the pip being invoked belongs to the same interpreter. If the shell has cached the \`pip\` location, run \`hash -r\` \(bash\) or \`rehash\` \(zsh\) after activating the venv.
Journey Context:
Developer creates a new venv: \`python3 -m venv .venv\`. Activates it with \`source .venv/bin/activate\`. They run \`pip install requests\`. Then they run their script \`python script.py\` which imports \`requests\`, but it raises \`ModuleNotFoundError: No module named requests\`. Confused, they check \`which python\` -> \`.venv/bin/python\`, but \`which pip\` -> \`/usr/bin/pip\`. The shell's hash table remembered the system pip from before activation. They run \`hash -r\` and now \`which pip\` points to \`.venv/bin/pip\`. They uninstall requests from the system \(if they accidentally installed it there\) and reinstall with \`python -m pip install requests\`, ensuring the module goes into the venv's site-packages. The script now works because the interpreter and pip are aligned.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T02:31:41.092493+00:00— report_created — created