Skip to content

Instantly share code, notes, and snippets.

@n0Oo0Oo0b
Last active April 6, 2023 04:00
Show Gist options
  • Save n0Oo0Oo0b/8c12660aa08cb77bae00e8b043b76b5d to your computer and use it in GitHub Desktop.
Save n0Oo0Oo0b/8c12660aa08cb77bae00e8b043b76b5d to your computer and use it in GitHub Desktop.
How to fix pip not installing packages even though it says 'requirement already satisfied'

Problem

Let's say you wanted to install a package (numpy for example) and you run the following command to install it:

$ pip install numpy

So pip does its thing, and exits with an output like so:

Installing collected packages: numpy
Successfully installed numpy-1.24.1

But when you try to import numpy, this happens:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

Or Visual Studio Code gives you a warning like this:

image

Why does this happen?

This happens because of a mismatch between your python and pip.

Each python version has its own pip. In other words, each python version has its own pip 'linked' to it. This means pip will only install packages for the python it is linked to.

If you have 2 (or more) versions of python, pip may be installing packages to a different python than the one you're running code with, which is why you won't be able to import packages that pip has installed.

**How you can tell that you have the problem**
  1. Check the python version you're using by running the following code:
import sys
print(sys.version_info)

# Example output (on python 3.11.2)
sys.version_info(major=3, minor=11, micro=2, releaselevel='final', serial=0)
  1. Check the pip version you're using by running the following in the command line:
$ pip --version

# Example output (pip for python 3.11)
pip 23.0.1 from C:\Users\DanDan\AppData\Local\Programs\Python\Python311\Lib\site-packages\pip (python 3.11)

If the python versions do not match (e.g. pip for 3.10 but python 3.11), then you have this issue. Note that just because these two match, it doesn't mean you don't have the issue, as might be the case if you downloaded the same version of python from the microsoft store and python.org.

Potential causes

  • you have multiple versions of python installed
  • you have multiple instances of the same version (eg python 3.11 from python.org and a different python 3.11 from the Microsoft Store)
    • In this case, you should uninstall the one you don't want
  • you're using a virtual environment, or venv for short (especially likely if you use PyCharm)

How do I fix this?

NOTE: If you're using PyCharm, it is recommended to use the built-in package manager to install packages instead (the 'Python Packages' menu near the bottom)

There are 2 main ways to fix this: changing the pip version you're using, or changing the python version you're using to run code.

If your interpreter version is newer, you should change the pip version and vice versa.

Method 1: changing the pip version

Click to expand Instead of running pip, which is 'linked' to a different version of python, you'll have to invoke the pip linked to the correct one. You can do this by **invoking pip as a module**:
> python -m pip install numpy

Instead of running any pip, this takes the specific python you're using and finds the pip associated with it. Note that you may have to run python differently, usually like so:

# Windows
> py -3.11 -m pip ...

# MacOS
> python3.11 -m pip ...

# Obviously, you should change `3.11` to whatever python version you're trying to use

Method 2: changing the python version

Click to expand If you run code from the command line, just change the python command to include the version (e.g. `py -3.11 ...` on Windows or `python3.11 ...` on Mac). On the other hand, if you're using an IDE like Visual Studio Code or PyCharm, you'll have to change your IDE settings to use a different python interpreter.

You can find the correct interpreter version you have to use by running pip --version.

Visual Studio Code

(This guide assumes you're using the Python extension to run code)

Click on the python interpreter version on the bottom right.

Alternatively, you can open the command palette (ctrl+shift+P or cmd+shift+P on Mac) and find 'Python: Select Interpreter'

image

You should get a menu that looks like this:

image

And you can select the correct version that corresponds to the pip you're using.

PyCharm

On pycharm, find the interpreter selection menu on the bottom right.

image

If you see the correct one from the list, then use that one and you're set!

Otherwise, you'll have to add the interpreter to PyCharm. Select 'Add New Interpreter' and then 'Add Local Interpreter...'

image

From the menu that opens, go to 'System Interpreter', and look for the python you want in the dropdown. If it isn't there, you can also click on the ... on the right and enter the interpreter path.

image

From there, you can hit 'OK' and it will use the newly added interprer to execute code.

Hopefully I included everything, but feel free to tell me if I missed anything/have any suggestions or improvements (comments are fine, or ping nO0o0Oo0b#0002 on PyDis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment