Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy - ImportError: cannot import name _distributor_init

Tags:

python

numpy

I have been using matplotlib from python and the spyder ide to create some figures for a publication. It always worked but even after years of using linux the whole thing for me still is a black box.

Yesterday I tried to run another application and it was complaining that numpy and cv2 were not installed (I don't know why as they were installed). I think I tried to upgrade those two packages by calling sudo pip install [package_name] --upgrade. End of the story - nothing works. At the moment my goal is just to get spyder running so that I can create figures again.

When I want to import scipy.io (import scipy.io as sio`) I just get a bunch of errors.

runfile('/home/test/Desktop/python_test.py', wdir='/home/test/Desktop') 
Traceback (most recent call last):

File "<ipython-input-1-83166c6df179>", line 1, in <module>
runfile('/home/test/Desktop/python_test.py', wdir='/home/test/Desktop')

File "/usr/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "/usr/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)

File "/home/test/Desktop/python_test.py", line 1, in <module>
import scipy as sio # to import matlab files

File "/home/test/.local/lib/python2.7/site-packages/scipy/__init__.py", line 62, in <module>
from numpy import show_config as show_numpy_config

File "/home/test/.local/lib/python2.7/site-packages/numpy/__init__.py", line 140, in <module>
from . import _distributor_init

ImportError: cannot import name _distributor_init

What do I need to do to fix this?

Currently, I have the following setup:

Ubuntu 18.04.2 LTS
Spyder 3.2.6

test@test:~$ python --version
Python 2.7.15rc1

test@test:~$ pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

test@test:~$ pip install scipy
Collecting scipy ... Successfully installed numpy-1.16.2 scipy-1.2.1

test@test:~$ sudo apt-get install python-numpy python-scipy
python-numpy is already the newest version (1:1.13.3-2ubuntu1).
python-scipy is already the newest version (0.19.1-2ubuntu1).
like image 605
Green Avatar asked Mar 07 '19 16:03

Green


1 Answers

In my case I found the following solution for my problem:

I ran my python script ("import numpy as np") directly in the terminal:

test@test:~/Desktop$ python python_test.py 
Traceback (most recent call last):
File "python_test.py", line 1, in <module>
import numpy as np
File "/home/test/.local/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/test/.local/lib/python2.7/site-packages/numpy/core/__init__.py", line 91, in <module>
raise ImportError(msg.format(path))
ImportError: Something is wrong with the numpy installation. While importing we detected an older version of numpy in ['/home/test/.local/lib/python2.7/site-packages/numpy']. One method of fixing this is to repeatedly uninstall numpy until none is found, then reinstall this version.

The error in the last line is more descriptive than the one I got in Spyder. I just uninstalled numpy via pip ("pip uninstall numpy") and it removed the 1.16.2 version. Now only the 1.13.3 version is left and it seems to work.

like image 70
Green Avatar answered Sep 20 '22 03:09

Green