10

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:\users\pc\appdata\local\programs\python\python36-32\lib\site-packages (1.15.0)

What? How can I fix this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Tomas T
  • 449
  • 1
  • 4
  • 17
  • Where is located your Python 3.6 root directory? I think is not in “AppData” but in your “Programs” folder. I mean that your library is installed in the wrong directory and Python can’t load it. – Samuel Roberto Aug 19 '18 at 22:43
  • On some systems (e.g., some versions of [Ubuntu](https://en.wikipedia.org/wiki/Ubuntu_%28operating_system%29)), it would be `pip3 install numpy` instead of `pip install numpy` (otherwise the result could be `Command 'pip' not found`). – Peter Mortensen Aug 22 '22 at 14:19
  • Should this be tagged with Windows or not? – Peter Mortensen Aug 22 '22 at 14:20
  • This question was ***the*** top search engine hit using `site:stackoverflow.com ModuleNotFoundError: No module named 'numpy'` (though not with `site:stackoverflow.com ModuleNotFoundError: No module named numpy` (without the single quotes)). – Peter Mortensen Aug 22 '22 at 14:23

2 Answers2

14

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.)

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • For me actually worked out install on VSC this extension https://marketplace.visualstudio.com/items?itemName=ms-toolsai.vscode-ai – Felipe Oct 12 '21 at 11:36
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nathan Teyou
  • 2,116
  • 16
  • 14
  • This is not true. `pip` comes with Python on Windows and is included in venvs on other systems. `pip` and `pip3` do the same thing, and separate wrappers exist only so that you can arrange for `pip` to refer to a 2.x installation while `pip3` continues referring to a 3.x installation. – Karl Knechtel Aug 28 '22 at 01:27