ImportError: No module named 'matplotlib'

2020-06-16 06:15发布

问题:

I'm just trying to learn Tensorflow, but am totally new to Python, so I'm using Anaconda I created a conda environment:

$ conda create −n tensorflow python =3.5

Of course I activated my conda environment

$ source activate tensorflow

Then I played a bit around in Spyder, tried to plot a MNIST-digit (copy-paste code from my tutor which is tested several times), it includes of course

import matplotlib.pyplot as plt
[...]
plt.plot(number)

but executing the Python file with bash gives me:

(tensorflow) leon@leon-linux:~/ANNsCourse/Session1$ python helloWorld.py
Traceback (most recent call last):
  File "helloWorld.py", line 10, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'

I'm quite confused right now, as the (tensorflow) in the bash obviously denotes that my conda tensorflow environment works (at least from my understanding). Also, from what I understood, conda should have matplotlib built in, right? And it should also load this in my conda tensorflow environment, right? This is what my tutor's slide said

There is no need to install further packages like numpy or matplotlib, since Anaconda contains current versions of them already.'

and also what I was able to take from everything I Googled and StackOverflowed. Neither Googling nor StackOverflowing gave me any good answer (might also just be because I don't understand enough yet).

My best guess would be that I still have to include matplotlib into my tensorflow conda environment, but this would be contradicting both my tutor & Google, while I also would not know how to do this.

edit: conda list gave me that matplotlib was not in my tensorflowenvironment, so I went

conda install matplotlib

I'm still afraid something is wrong with my conda tensorflow environment, shouldn't the matplotlib have been in there by default? It also told me:

Package plan for installation in environment /home/leon/.conda/envs/tensorflow:

The following NEW packages will be INSTALLED:

cycler:           0.10.0-py35_0    
dbus:             1.10.10-0        
expat:            2.1.0-0          
fontconfig:       2.12.1-3         
freetype:         2.5.5-2          
glib:             2.50.2-1         
gst-plugins-base: 1.8.0-0          
gstreamer:        1.8.0-0          
icu:              54.1-0           
jpeg:             9b-0             
libffi:           3.2.1-1          
libgcc:           5.2.0-0          
libiconv:         1.14-0           
libpng:           1.6.27-0         
libxcb:           1.12-1           
libxml2:          2.9.4-0          
matplotlib:       2.0.0-np112py35_0
mkl:              2017.0.1-0       
numpy:            1.12.0-py35_0    
pcre:             8.39-1           
pyparsing:        2.1.4-py35_0     
pyqt:             5.6.0-py35_2     
python-dateutil:  2.6.0-py35_0     
pytz:             2016.10-py35_0   
qt:               5.6.2-3          
sip:              4.18-py35_0      
six:              1.10.0-py35_0    

Proceed ([y]/n)? y

Which tells me also numpy was missing? Can someone confirm this to be correct now, or is there something fishy with my conda?

回答1:

You just created a conda environment named tensorflow and switched into it. You haven't installed the tensorflow package or any of the default anaconda packages.

To do that, do

conda create -n tensorflow python=3.5 anaconda # install anaconda3 default packages
source activate tensorflow # switch into it
conda install -c conda-forge tensorflow # install tensorflow


回答2:

I ran into the same problem using these instructions: https://www.anaconda.com/tensorflow-in-anaconda/ for tensorflow-gpu.

Running

conda create -n tensorflow_gpuenv tensorflow-gpu
conda activate tensorflow_gpuenv

should ensure that "TensorFlow is now installed and ready for use." But it doesn't. Running 'conda list' shows matplotlib was not installed. So you'll need to install that as well:

conda install -c conda-forge matplotlib


回答3:

I faced the same problem on my mac. So I ran conda list to see whether the matplotlib is installed or not. Once i found it missing, i went ahead and ran the command conda install matplotlib. After this step to verify it's properly installed; Do the following.

conda activate tf

This activates the tensorflow environment on anaconda. After this start interactive python shell on the same terminal.

import matplotlib

If its installed properly this should not throw any error now.