ImportError: No module named scipy.sparse

18,367

Solution 1

scipy path mixed up. Uninstall

pip uninstall scipy

Install using conda worked for me

conda install scipy

Solution 2

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.

Share:
18,367
userInThisWorld
Author by

userInThisWorld

Updated on June 18, 2022

Comments

  • userInThisWorld
    userInThisWorld almost 2 years

    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'
    
  • Ahmed Hawary
    Ahmed Hawary about 5 years
    Then I think your Scipy install went wrong. Try to uninstall and install the wheel file this time: lfd.uci.edu/~gohlke/pythonlibs/#scipy
  • userInThisWorld
    userInThisWorld about 5 years
    nothing appeared when execute which python .. but with python -- version .. pythn3.5.2 and from pip freeze scipy==1.2.1 is shown
  • Ahmed Hawary
    Ahmed Hawary about 5 years
    Did you try to install it through wheel file?
  • Ahmed Hawary
    Ahmed Hawary about 5 years
    I also do recommend to try installing Anaconda distribution . It would save plenty of time of installing packages and that type of issues.
  • kitokid
    kitokid about 3 years
    you can also use conda install -c conda-forge scipy
  • ah bon
    ah bon about 3 years
    pip install --upgrade --user scipy solved my problem, thanks.