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
  • Try pip-2.x install numpy. Get x from your python2 version. – Manas Singh 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 00:26
  • @steeldriver $ type -a python
    python is /usr/bin/python```
    
    – SKR Nov 24 '19 at 05: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 05: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 05:59
  • @SKR so you have installed an alternate version of python (in /usr/local/bin) which is being run in preference to the "system" version in /usr/bin - likely numpy is only installed for the "system" version – steeldriver Nov 24 '19 at 14:05

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
  • 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 06: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 06: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 06:12
  • Apparently, there is no such option as --system. $ sudo -H pip2 install --system numpy

    no such option: --system

    – SKR Nov 24 '19 at 06: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 06:23
  • pip 19.3.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7) I had updated it already – SKR Nov 24 '19 at 06:27
  • Apparently pip removed the system option in the recent updates. My pip was the older version. The next steps I would suggest would be to do sudo pip2 uninstall numpy then do sudo pip2 install --user numpy again. It might just need the sudo. Also, I said before, sometimes you need to specify things. One of the guys who answered your previous post suggested doing sudo apt install python-numpy. I would suggest doing sudo apt install python2-numpy. However, I would suggest to stick to using pip whenever its possible. It might just be this pip version that is buggy. – elam Nov 24 '19 at 06:42
  • Disregard sudo apt install python2-numpy. That does not work. – elam Nov 24 '19 at 06:53
  • Now it gives altogether a new error message: <pre>Original error was: /home/skrout/.local/lib/python2.7/site-packages/numpy/core/_multiarray_umath.so: undefined symbol: PyUnicodeUCS4_FromObject</pre> This message was printed after a long message asking to check a few things and if nothing works, it suggested to raise a bug report in github. – SKR Nov 24 '19 at 06:54
  • So, this error message confirms to me that it's conflicting between Python2 and Python3. You can look up a fix for this through google. You can read it up in this link. I would also add... You might need to sudo apt remove python-numpy. Also, you need to run pip2 install --user numpy. In your post, you didn't mention it. – elam Nov 24 '19 at 07:01
  • Could you also try this command after uninstalling numpy? python2 -m pip install --user numpy – elam Nov 24 '19 at 07:05
  • Upon trying python -m pip install --user numpy after uninstalling numpy, I get the following message: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Requirement already satisfied: numpy in /home/skrout/.local/lib/python2.7/site-packages (1.16.5) – SKR Nov 24 '19 at 08:45
  • So, this is telling me that numpy is being used by one of your dependencies. I would refer to this stackoverflow for more information. – elam Nov 24 '19 at 10:31
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