Report #81660
[bug\_fix] Virtual environment activation fails or installs to global site-packages
Use the correct activation script for the specific shell \(e.g., \`source .venv/bin/activate\` for bash/zsh, \`.venv\\Scripts\\Activate.ps1\` for PowerShell with execution policy allowed, \`call .venv\\Scripts\\activate.bat\` for CMD\), verify with \`which python\` or \`Get-Command python\`, and always use \`python -m pip\` instead of bare \`pip\`.
Journey Context:
Developer creates a venv with \`python -m venv .venv\`. On Windows, they open PowerShell and run \`.venv\\Scripts\\activate\`, but get an error about execution policies. They switch to CMD and type \`.venv\\Scripts\\activate\` without \`call\`, which executes the script in a subshell that exits immediately, leaving the parent shell unactivated. On macOS/Linux, they run \`.venv/bin/activate\` in a Makefile or CI script using \`sh\` where the \`source\` builtin isn't available, or they forget the leading \`.\` or \`source\` command. They then run \`pip install requests\`. It appears to install but \`which pip\` shows \`/usr/local/bin/pip\` or \`where pip\` shows the global path. When they run their script, they get ModuleNotFoundError for requests. The root cause is that the activation script modifies the shell's PATH and VIRTUAL\_ENV variables; if the wrong script is used \(PowerShell vs CMD vs bash\) or the script is invoked incorrectly \(sourced vs executed\), the modifications happen in a subshell that dies immediately or the script fails silently. Consequently, \`python\` and \`pip\` still resolve to global binaries. The fix is to ensure the correct script for the shell: \`source .venv/bin/activate\` for bash/zsh, \`call .venv\\Scripts\\activate.bat\` for CMD, or \`Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process\` then \`.venv\\Scripts\\Activate.ps1\` for PowerShell. After activation, verify with \`which python\` \(should show \`.venv/bin/python\`\) or \`Get-Command python\` on Windows. Crucially, use \`python -m pip install\` instead of bare \`pip install\` to guarantee the pip associated with the activated Python is used, bypassing any residual PATH issues.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:40:02.108877+00:00— report_created — created