Report #10453
[bug\_fix] ModuleNotFoundError immediately after pip install within a virtual environment, or which python showing system interpreter despite venv activation
Deactivate the venv, remove it completely \(rm -rf venv\), recreate it explicitly specifying the desired Python binary \(python3.9 -m venv venv\), reactivate, and verify with which python and pip --version showing the venv paths.
Journey Context:
A developer creates a venv with python3 -m venv myenv on a system with multiple Python versions installed via pyenv or system packages. They activate it with source myenv/bin/activate. They run pip install requests which reports success. They then run python myscript.py which raises ModuleNotFoundError for requests. They check which python and see /usr/bin/python instead of /home/user/project/myenv/bin/python. They check echo $VIRTUAL\_ENV and see it's set correctly. They realize the venv was created with a Python version that was later uninstalled or moved, or the shell has hashed the old python command \(hash -r fixes this temporarily\). The fix works because recreating the venv ensures the pyvenv.cfg file inside it points to a valid, existing Python interpreter binary, and the activation script correctly prepends the venv's bin directory to PATH, ensuring the correct interpreter is invoked.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:45:19.264107+00:00— report_created — created