Why do I get a "ModuleNotFoundError" when pip says "Requirement already satisfied"?

Question:

I’m using Python 3.6.5 on a 64-bit version of Windows 10.

When I try to start Python using py and then import NumPy at the interpreter prompt, I get an exception:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

I thought I did not have NumPy installed, so I tried to install it with pip install numpy at the command line. But this gives

Requirement already satisfied: numpy in c:userspcappdatalocalprogramspythonpython36-32libsite-packages (1.15.0)

What? How can I fix this?

Asked By: Tomas T

||

Answers:

Your problem is that you installed two different Pythons, a 32-bit 3.6, and a 64-bit 3.6.

The first pip on your PATH is the one for the 32-bit 3.6. So, when you pip install numpy, it’s downloading the 32-bit NumPy, and installing into the site-packages for the 32-bit Python.

But your py launcher is defaulting to running the 64-bit 3.6, which can’t see the site-packages for a completely different Python installation, and couldn’t use them even if it did see them.

The simplest solution is to start over from scratch: Uninstall both Pythons, pick the one you want, and reinstall that. (You could just uninstall the one you don’t want, leaving the other… but that may cause problems, like leaving py configured wrong so it can’t run Python at all. At the very least you should re-run the installer for the one you want to keep and tell it to update the existing installation.)

If you can’t do that, you may want to consider using virtual environments. With a virtual environment active, pip, python and py will all come from the active environment, so it doesn’t matter what else you have anywhere on your system.

If you can’t do that, just don’t run pip, run py -m pip. This guarantees that you’re using the pip for the right Python installation, and installing packages for that installation. (And the same goes for other tools—run py -m 2to3, not 2to3, and so on.)

Answered By: abarnert

This issue still persists after running pip install numpy, because you are running Python 3 and pip is a package for Python 2. So the above command will install pip for Python 2.

For Python 3, you have to install pip3 by running the command sudo apt install python3-pip and now install NumPy using the command sudo pip3 install numpy.

Answered By: Nathan Teyou
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.