1

I am trying to compile a python program where I am using numpy.random.choices(). once I compile I get this one error:

ImportError: No module named 'numpy'

I read That I have to install numpy package. I did install numpy-1.11.1 it didn't work. I get the same error. I upgraded to numpy-1.15.0 it also didn't work. I used this solution that I found online

pip install --upgrade --ignore-installed --install-option '--install-data=/usr/local' numpy

and this is the message I get

Successfully installed numpy-1.15.0
.......  $ python3 file.py
Traceback (most recent call last):
File "file.py", line 5, in <module>
import numpy
ImportError: No module named 'numpy'

what am I doing wrong?

  • 2
    you _probably_ have both python2 & python3 installed & pip installs numpy for python2 but you are running the file with the python3 interpreter. – Haleemur Ali Aug 13 '18 at 15:47

1 Answers1

1

you are installing numpy from pip for python 2.x but you are working with python 3.x

to solve your problem download pip3 and download numpy from it.

In the solution below I used python3.4 as binary, but it's safe to use with any version or binary of python. it works fine on windows too (except the downloading pip with wget obviously but just save the file locally and run it with python).

This is great if you have multiple versions of python installed, so you can manage external libraries per python version.

So first, I'd recommend get-pip.py, it's great to install pip :

wget https://bootstrap.pypa.io/get-pip.py

Then you need to install pip for your version of python, I have python3.4 so for me this is the command :

python3.4 get-pip.py

Now pip is installed for python3.4 and in order to get libraries for python3.4 one need to call it within this version, like this :

python3.4 -m pip

So if you want to install numpy you would use :

python3.4 -m pip install numpy

Note that numpy is quite the heavy library. I thought my system was hanging and failing. But using the verbose option, you can see that the system is fine :

python3.4 -m pip install numpy -v

This may tell you that you lack python.h but you can easily get it :

On RHEL (Red hat, CentOS, Fedora) it would be something like this :

yum install python34-devel

On debian-like (Debian, Ubuntu, Kali, ...) :

apt-get install python34-dev

Then rerun this :

python3.4 -m pip install numpy -v

from Loïc

Liam
  • 6,009
  • 4
  • 39
  • 53
  • I forgot to specify that I am working on a mac. but I get your Idea. Thank you –  Aug 13 '18 at 15:51
  • I decided to uninstall numpy and pip to start over with the adequat version. but mac won't let me even with sudo. and I don't want to do it manually-question of damaging the OS. any ways around this? –  Aug 13 '18 at 15:58
  • what do you mean with "mac won't let me"? – Liam Aug 13 '18 at 15:58
  • I wanted to restart the process all over but I guess there is no need for all that. thank you –  Aug 13 '18 at 20:41