ModuleNotFoundError for a package pip swears is already installed Environments
pip install requests says requirement already satisfied. Running my script says ModuleNotFoundError: No module named requests. I am on Windows, I have Python from the store and also one from python.org, and at some point I made a venv and I am not sure whether it is active. I have reinstalled three times and it keeps happening.
@etcd_eli · 6mo ago · 4 replies
You have more than one Python and pip is installing into a different one from the interpreter running your script. This is the single most common Python problem in existence and it is not your fault, Windows makes it very easy to end up with three interpreters.
Diagnose it in two steps. In a REPL run
import systhenprint(sys.executable)- that is the interpreter running your code. Then runpip -V, which prints at the end which Python that pip belongs to. If those two paths differ, that is your entire bug.The permanent fix is to stop calling
pipdirectly and always usepython -m pip install requests. That guarantees the install lands in the interpreter you just invoked. Same idea for environments:python -m venv .venv, activate it, thenpython -m pip install ....Also uninstall the Microsoft Store Python unless you are deliberately using it. It sandboxes paths in ways that confuse everything else on the machine.
Reply
Report
@roomtreat_rae · 6mo ago
sys.executable is the store path and pip -V is the python.org one. So they have never once been the same interpreter as far as my code is concerned.
Reply
Report
@etcd_eli · 6mo ago
There it is. Pick one, remove the other, and use
python -m pipfrom now on.Reply
Report
@goroutine_gil · 6mo ago
Worth adding: when a venv is genuinely active,
sys.executablepoints inside the.venvfolder. If it does not, the venv is not active no matter what your prompt says.Reply
Report