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.

  • Try pip-2.x install numpy. Get x from your python2 version. – kingmanas Nov 23 '19 at 20:50
  • Have you installed an alternative version of python? what does type -a python say? – steeldriver Nov 24 '19 at 0:26
  • @steeldriver $ type -a python python is /usr/local/bin/python python is /usr/bin/python – SKR Nov 24 '19 at 5:51
  • @steeldriver I have both python-2.7.15 and python-3.6.8. The default, i.e., those in /usr/bin and /usr/local/bin direct to python2 – SKR Nov 24 '19 at 5:57
  • @ManasSingh pip-2.7.15 is not a command. Only pip2 or pip3 should work, but it doesn't for me which is why I posted this question. – SKR Nov 24 '19 at 5:59

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.

  • I am not sure how you came to know that apt install python-numpy installed numpy for python3. Could you please let me know. I think, my system also uses python2 as default. $ python --version Python 2.7.15 – SKR Nov 24 '19 at 6:03
  • And like I have already mentioned in the question, I have tried using pip2. This is what pip2 install --user numpy had to tell for me: Requirement already satisfied: numpy in /usr/local/lib/python2.7/dist-packages (1.16.5) – SKR Nov 24 '19 at 6:04
  • I tested it by uninstalling numpy from both python 2 and python 3. Installing libraries directly from apt install does not always get the desired result. Sadly, the dependencies or aliasing break. I would try to do sudo -H pip2 install --system numpy after you do pip2 uninstall numpy. – elam Nov 24 '19 at 6:12
  • Apparently, there is no such option as --system. $ sudo -H pip2 install --system numpy no such option: --system – SKR Nov 24 '19 at 6:21
  • Could you do pip2 --version for me? your pip2 version might be out of date. If the version is less that 9.0.1, please update it. So, do a sudo apt update then sudo apt upgrade. – elam Nov 24 '19 at 6:23

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.