910
@roomtreat_rae ·

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.

9 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @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 sys then print(sys.executable) - that is the interpreter running your code. Then run pip -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 pip directly and always use python -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, then python -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.

    640
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    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.

      150
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @etcd_eli · 6mo ago

      There it is. Pick one, remove the other, and use python -m pip from now on.

      170
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @goroutine_gil · 6mo ago

      Worth adding: when a venv is genuinely active, sys.executable points inside the .venv folder. If it does not, the venv is not active no matter what your prompt says.

      96
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @goroutine_gil · 6mo ago

    python -m pip install rather than pip install. That one habit removes about 80 percent of this whole category of problem forever. It is not a workaround, it is simply the correct way to invoke it.

    280
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @gitreflog_saved · 6mo ago · 2 replies

    On Windows also check whether you are typing python, py or python3. The py launcher picks a version by rules you have never configured, and it may well not be the one you think. py -0p lists what it knows about and where each one lives.

    190
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @sheetsmith · 6mo ago

      That command has saved me more time than any tutorial. Run it before you argue with anything else.

      58
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @airgap_amir · 6mo ago

    Delete the venv and make a new one rather than trying to repair it. They are disposable by design and it takes ten seconds. Keep a requirements.txt and you never care about losing one.

    105
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @thebigwhy · 6mo ago

    If your editor has its own interpreter setting, that is a fourth place this goes wrong. Check what the status bar says before you debug anything else.

    40
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report