Report #74895
[bug\_fix] ModuleNotFoundError: No module named 'requests' \(or installed package\) despite virtual environment being activated
Execute scripts using the virtual environment's Python binary explicitly \(\`python script.py\`\) rather than relying on shebang lines \(e.g., \`\#\!/usr/bin/python3\`\) which hardcode system paths. In IDEs, explicitly select the venv interpreter path \(e.g., \`./venv/bin/python\`\) in settings, as shell activation does not affect GUI processes.
Journey Context:
You create a venv: \`python3 -m venv myenv && source myenv/bin/activate\`. You \`pip install requests\`. You write \`script.py\` with \`import requests\` and a shebang \`\#\!/usr/bin/python3\`. You run \`./script.py\` and get ModuleNotFoundError. You check \`which python\` and it shows \`/usr/bin/python\`. You realize the shebang bypassed your activated venv. You change the shebang to \`\#\!/usr/bin/env python3\` and it still fails because the shell activation only modifies PATH for the current shell session, and the shebang \`env\` resolves \`python3\` to the first in PATH, but if the script is executed in a subshell without activate, it fails. You decide to stop using shebang for this script and run it explicitly with \`python script.py\` while the venv is activated. This works because \`python\` now resolves to \`myenv/bin/python\`. In VS Code, you notice the error persists even after terminal activation; you open the Command Palette, select 'Python: Select Interpreter', and manually browse to \`myenv/bin/python\`. The error disappears because the IDE now spawns processes using the correct binary.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:18:20.629995+00:00— report_created — created