Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - Why is matplotlib failing on import matplotlib.pyplot as plt

I installed matplotlib using conda:

conda install matplotlib

The following code failed:

#!/usr/bin/env python
import matplotlib 
import matplotlib.pyplot as plt

With this error message:

"ImportError: No module named 'matplotlib.pyplot'"

I tried installing matplotlib with apt-get:

sudo apt-get install python3-matplotlib

I got the same error.

I tried loading matplotlib with ubuntu application load and got the same error.

I tried cloning from GitHub with:

git clone git://github.com/matplotlib/matplotlib.git

I got the same error.

I looked at the matplotlib directory and did not see a pyplot.py entry. I did find pyplot.py in matplotlib/lib/matplotlib. I copied it to matplotlib. The error went away, but I got another module that pyplot was trying to include. I found it in matplotlib/lib/matplotlib. I copied it to matplotlib. Got another error for another module. Copied it. Eventually I got an error for a module I could not find.

I do not know what to try next.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Normally conda isn't added to your path (it asks you during the installation if it should do that but the default is "no"), so the default python will be the systems Python 2 (you start Python 3 with python3).

You could verify this by using:

$ which python

That will return the path associated with the python command. Likely this will return the path to the systems Python 2 "installation".

For example on my Ubuntu machine the commands which python and which python3 return:

usr/bin/python   (starts Python 2.7.12)
usr/bin/python3  (starts Python 3.5.2)

While my conda installation is somewhere in the /home/michael/miniconda directory.

There are several options how you could use the conda Python:

  • Temporarily add the conda directory it to the PATH. (See for example How to add [...] to path)
  • Permanently add the conda directory to the PATH. (see for example How to add a directory to the path)
  • Use the anaconda promt which gives you a terminal with the conda directory prepended to the PATH.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...