matplotlib错误:没有名为matplotlib的模块,即使它已安装

我已经完成了在我的Ubuntu 12.04计算机上安装matplotlib的过程。 但是,当我尝试运行测试程序时,我收到以下错误:

Traceback (most recent call last): File "../py33/test.py", line 2, in  import matplotlib.pyplot as plt ImportError: No module named 'matplotlib' 

我正在尝试matplotlib网站上的示例程序(basic_example)。 我只是不明白为什么它说没有模块matplotlib,即使我已经安装了它(使用sudo apt-get install python-matplotlib ,并且已经完成了sudo apt-get build-dep python-matplotlib;我也试过了pip install matplotlib ,正如我被告知要做的那样,在安装Python之后,如果我想安装其他二进制文件)。

我相信你使用的是python3 。 因此,为了运行您的脚本,您需要使用python2.x或确保您具有matplotlib v1.2或更高版本。 这是因为matplotlib在v1.2之前不支持python3。

我正在使用matplotlib v1.1python2.7 ,我没有问题。 我试着用python3做一个简单的import matplotlib ,它给出了和你一样的错误。

如果要通过APT包管理器安装Matplotlib for Python 3,则需要包python3-matplotlib

 sudo apt-get install python3-matplotlib 

如果你想用Pip for Python 3安装它,你需要使用pip3

 sudo pip3 install matplotlib 

引用:

…即使我已经安装了它( using sudo apt-get install python-matplotlib ,并且已经完成了sudo apt-get build-dep python-matplotlib ;我也试过pip install matplotlib ,因为我被告知这样做,安装Python本身后,如果我想安装其他二进制文件)。

可能的方法:

摘自官方网页( https://matplotlib.org/users/installing.html ),试试这个:

python -mpip install -U pip

python -mpip install -U matplotlib

(第一行安装pip,库的管理器安装程序,第二行安装matplotlib本身)。

评:

我在Ubuntu 16.04上遇到了同样的问题,这个版本的效果非常好。

最好的祝福,