问题
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'
回答1:
You are using Python 3, but with pip you install scipy for Python 2.
Use:
pip3 install scipy
or:
sudo apt install python3-scipy
回答2:
try
pip3 install scipy
and to install pip3
sudo python3 get-pip.py
回答3:
Just to make sure on which Python version you've installed Scipy try in the terminal:
which Python
Then try:
pip freeze
to get a list of all the installed packages.
Maybe you have more than one python version and have installed the package to one and trying to execute your code using the other.
回答4:
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.
回答5:
Install it with pip instead, run:
pip install scipy
That did the trick for me, hope it helps!
EDIT: Forgot you're using python 3.x, run this instead:
pip3 install scipy
来源:https://stackoverflow.com/questions/55043789/importerror-no-module-named-scipy-sparse