Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named scipy.sparse

I installed Scipy on Ubuntu using the following commands:

sudo apt-get install python-scipy
pip install scipy

but when run import, I get this error:

ImportError: No module named scipy.sparse

I searched and tried the following and reinstalled Scipy:

sudo apt-get purge python-scipy

but still got the same error.

Update: I didn't import scipy in my python file, just imported keras.

Here is the error message:

(my_env)  ..  $ python test.py
Using TensorFlow backend.
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    import keras
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/utils/__init__.py", line 27, in <module>
    from .multi_gpu_utils import multi_gpu_model
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/utils/multi_gpu_utils.py", line 7, in <module>
    from ..layers.merge import concatenate
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/layers/__init__.py", line 4, in <module>
    from ..engine.base_layer import Layer
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/engine/__init__.py", line 8, in <module>
    from .training import Model
  File "/home/ ../my_env/lib/python3.5/site-packages/keras/engine/training.py", line 21, in <module>
    from . import training_arrays
  File "/home/../my_env/lib/python3.5/site-packages/keras/engine/training_arrays.py", line 8, in <module>
    from scipy.sparse import issparse
ImportError: No module named 'scipy.sparse'
like image 506
userInThisWorld Avatar asked Oct 16 '22 05:10

userInThisWorld


2 Answers

scipy path mixed up. Uninstall

pip uninstall scipy

Install using conda worked for me

conda install scipy
like image 145
Kennedy Kairu Kariuki Avatar answered Oct 20 '22 11:10

Kennedy Kairu Kariuki


In Ubuntu 18.04 and later you could install Scipy and Keras for Python 3 with sudo apt install python3-scipy python3-keras and you'd be good to go, however you are using Ubuntu 16.04 and you installed Scipy for Python 2 which is not compatible with TensorFlow for Python 3.4, 3.5 and 3.6, so install the default Scipy package for Python 3 instead with this command:

sudo apt install python3-scipy  

For further instructions on installing TensorFlow in Ubuntu read this answer. It's worth reading because you are going to have to check for package version compatibility when working with TensorFlow from now on.

The command pip install scipy is not correct either if the purpose of that command was to upgrade Scipy. The correct command to upgrade Scipy would have been pip install --upgrade --user scipy and even so it would have been useless because your scipy that is currently installed is only for Python 2 and your TensorFlow is for Python 3.

like image 20
karel Avatar answered Oct 20 '22 10:10

karel