Why is matplotlib failing on import matplotlib.pyplot as plt

亡梦爱人 提交于 2019-12-12 06:38:31

问题


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.


回答1:


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.


来源:https://stackoverflow.com/questions/48268935/why-is-matplotlib-failing-on-import-matplotlib-pyplot-as-plt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!