0

I used to have numpy working earlier. Not sure what happened, but now I get the import error:

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

I have tried uninstalling and reinstalling numpy using pip and apt (import error:module named numpy) as suggested in other answers, but that did not solve my issue. It says that I have the newest version of numpy:

$ sudo apt install python-numpy
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-numpy is already the newest version (1:1.13.3-2ubuntu1).
The following packages were automatically installed and are no longer required:
  libllvm6.0 libllvm6.0:i386 libllvm7 libllvm7:i386
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 79 not upgraded.
$ python -c "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy

Just to mention, numpy works fine in python3.

SKR
  • 43

2 Answers2

0

When you install an application in pip, you have to specify which pip version, or for the system or user. (pip2 is for Python2 and pip3 is for python3.) Also, there's system and user instances. When you install a python library, sometimes you have to specify whether it is for user or for the whole system. For example

pip2 install --user numpy
sudo -H pip2 install --system numpy

Also, pip2 and pip3 are separate and do not cross over.

$:~/$ pip2 install --user numpy
    Collecting numpy
      Downloading https://files.pythonhosted.org/packages/d7/b1/3367ea1f372957f97a6752ec725b87886e12af1415216feec9067e31df70/numpy-1.16.5-cp27-cp27mu-manylinux1_x86_64.whl (17.0MB)
        100% |████████████████████████████████| 17.0MB 83kB/s 
    Installing collected packages: numpy
    Successfully installed numpy-1.16.5
$:~/$ pip3 install --user numpy
        Collecting numpy
          Downloading https://files.pythonhosted.org/packages/d2/ab/43e678759326f728de861edbef34b8e2ad1b1490505f20e0d1f0716c3bf4/numpy-1.17.4-cp36-cp36m-manylinux1_x86_64.whl (20.0MB)
            100% |████████████████████████████████| 20.0MB 83kB/s 
        Installing collected packages: numpy
        Successfully installed numpy-1.17.4

With some testing, I found out that sudo apt install python-numpy installs for python3 for you. For me, it installed it for python2 and not python3. The reason is because my system is defaulted to using python2 as the main python. For some systems, python3 is the default python for libraries to be installed for. So, I really suggest getting used to doing pip2 and pip3 for installing libraries.

elam
  • 1
0

It turns out that I had two python installations in my system. One at /usr/bin and the other at /usr/local/bin/, the former being the system version, and the later installed by me sometime ago. All the installations and re-installations of the packages that I have done using pip or apt-get were associated with the system version of the python, whereas the path to python was directed to the custom installation. Thus, whenever I opened python from the terminal I was basically calling the custom installation while all the packages were installed in the system version. This led to the errors while importing different packages.

One solution would have been to simply remove the local installation. Another solution, which I did, is to redirect the symbolic link to the system version as follows: sudo ln -sf /usr/bin/python /usr/local/bin/python

This did the job.

SKR
  • 43