Report #7790
[bug\_fix] ModuleNotFoundError immediately after pip install inside activated venv \(or pip installs packages to global site-packages instead of venv\)
Use \`python -m pip install \` instead of the bare \`pip install\` command, ensuring the pip module associated with the specific Python interpreter is invoked. Alternatively, verify shell activation actually modified PATH \(\`which pip\` should point inside venv\). Root cause: The shell's PATH resolves the \`pip\` executable to a different Python installation's script directory \(e.g., system \`/usr/bin/pip\`\) before the venv's \`bin/\` directory, especially if the venv activation script failed, the shell hashed the location, or the environment is a subshell \(common in CI or Docker\).
Journey Context:
A developer creates a fresh venv with \`python -m venv .venv\` and activates it with \`source .venv/bin/activate\`. The prompt shows \(.venv\). They run \`pip install requests\`. It reports success. They then execute \`python script.py\` which imports requests, and it crashes with ModuleNotFoundError. Puzzled, they run \`pip list\` and see requests is installed. They check \`which python\` which shows \`/home/user/project/.venv/bin/python\`. However, \`which pip\` reveals \`/usr/bin/pip\`. The activation script failed to prepend the venv bin to PATH, or the shell had cached the location of pip before activation. The developer learns to use \`python -m pip install requests\`, which explicitly invokes the pip module from the sys.path of the venv's python, guaranteeing the package installs 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-16T03:43:28.237207+00:00— report_created — created