ImportError:没有名为matplotlib.pyplot的模块

问题:ImportError:没有名为matplotlib.pyplot的模块

我目前正在练习matplotlib。这是我练习的第一个示例。

#!/usr/bin/python

import matplotlib.pyplot as plt

radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]

plt.plot(radius, area)
plt.show()

当我使用运行脚本时python ./plot_test.py,它可以正确显示绘图。但是,我自己运行./plot_test.py,它引发了以下问题:

Traceback (most recent call last):
File "./plot_test.py", line 3, in <module>
  import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

python是否在不同位置查找matplotlib?

环境是:

Mac OS X 10.8.4 64bit
built-in python 2.7

numpy,scipy,matplotlib已安装:

sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose

I am currently practicing matplotlib. This is the first example I practice.

#!/usr/bin/python

import matplotlib.pyplot as plt

radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]

plt.plot(radius, area)
plt.show()

When I run this script with python ./plot_test.py, it shows plot correctly. However, I run it by itself, ./plot_test.py, it throws the followings:

Traceback (most recent call last):
File "./plot_test.py", line 3, in <module>
  import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

Does python look for matplotlib in different locations?

The environment is:

Mac OS X 10.8.4 64bit
built-in python 2.7

numpy, scipy, matplotlib is installed with:

sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose

回答 0

您的计算机上安装了两个python,一个是Mac OSX随附的标准python,第二个是您使用端口安装的python(这是已matplotlib安装在其库中的一个,而macosx却没有安装)。 。

/usr/bin/python

是标准的mac python,由于没有,所以matplotlib您应该始终使用安装了端口的脚本启动脚本。

如果python your_script.py可行,请将更改#!为:

#!/usr/bin/env python

或将完整路径放入matplotlib已在其库中安装的python解释器。

You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).

/usr/bin/python

Is the standard mac python and since it doesn’t have matplotlib you should always start your script with the one installed with ports.

If python your_script.py works then change the #! to:

#!/usr/bin/env python

Or put the full path to the python interpreter that has the matplotlib installed in its library.


回答 1

pip 让您的生活更轻松!

第1步:安装pip-简单地通过在python控制台中编写pip来检查是否已有pip。如果没有pip,请通过以下网址获取一个名为get-pip.py的python脚本:https://pip.pypa.io/en/latest/installing.html或直接在此处:https://bootstrap.pypa .io / get-pip.py(您可能必须使用另存为..)

第2步:记下文件的保存位置,然后从命令提示符下cd目录。运行get-pip.py脚本以安装pip。您可以在cmd这一行中用引号引起来:“ python。\ get-pip.py”

步骤3:现在输入cmd: pip install matplotlib

而你应该通过。

pip will make your life easy!

Step 1: Install pip – Check if you have pip already simply by writing pip in the python console. If you don’t have pip, get a python script called get-pip.py , via here: https://pip.pypa.io/en/latest/installing.html or directly here: https://bootstrap.pypa.io/get-pip.py (You may have to use Save As ..)

Step 2: Take note of where the file got saved and cd the directory from command prompt. Run the get-pip.py script to install pip. You can write in cmd this line within quotes: “python .\get-pip.py”

Step 3: Now in cmd type: pip install matplotlib

And you should be through.


回答 2

如果您使用的是Python 2,请运行

sudo apt-get install python-matplotlib

最好的获取方法matplotlib是:

pip install matplotlib

因为以前的方法可能会给您旧版本 matplotlib

If you are using Python 2, just run

sudo apt-get install python-matplotlib

The best way to get matplotlib is :

pip install matplotlib

cause the previous way may give you a old version of matplotlib


回答 3

这对我有用,受到Sheetal Kaul的启发

pip uninstall matplotlib
python3 -m pip install matplotlib

我知道它在工作时安装在错误的位置:

python2.7
import matplotlib

This worked for me, inspired by Sheetal Kaul

pip uninstall matplotlib
python3 -m pip install matplotlib

I knew it installed in the wrong place when this worked:

python2.7
import matplotlib

回答 4

python的第一个检查版本

对于python2 Vesion

sudo apt-get install python-matplotlib

对于python3版本

sudo apt-get install python3-matplotlib

如果您将python版本的matplotlib安装错过匹配,则会出现“无模块错误”,因为该版本没有模块退出。

First check the version of Python

For python2:

sudo apt-get install python-matplotlib

For python3:

sudo apt-get install python3-matplotlib

If you mismatch the Matplotlib installation and the Python version you will get the no-module-error because no module for that version exits.


回答 5

对于python3。只需运行pip3 install matplotlib

For python3. Just need to run pip3 install matplotlib


回答 6

如果您使用Anaconda3

刚放

conda install -c conda-forge matplotlib

If you using Anaconda3

Just put

conda install -c conda-forge matplotlib

回答 7

我有一个类似的问题已解决,这是我的问题:

我在python3上进行了所有设置,但是例如,我使用python来调用我的文件:我正在输入“ python mnist.py” …因为我在python3上拥有所有内容,所以我认为我正在尝试使用python 2.7

的更正:“ python3 mnist.py”-3使一切有所不同

我绝不是python或pip的专家,但是pip和pip3之间肯定有区别(pip与python 2.7绑定)(pip3与python 3.6绑定)

因此,当安装2.7时要执行:pip安装当安装3.6时要执行:pip3安装

当运行2.7的代码时执行:python运行3.6的代码时执行:python3

我希望这可以帮助别人!

I had a similar issue that I resolved and here is my issue:

I set everything up on python3 but I was using python to call my file for example: I was typing “python mnist.py” …since I have everything on python3 it was thinking I was trying to use python 2.7

The correction: “python3 mnist.py” – the 3 made all the difference

I’m by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip is tied to python 2.7) (pip3 is tied to python 3.6)

so when installing for 2.7 do: pip install when installing for 3.6 do: pip3 install

and when running your code for 2.7 do: python when running your code for 3.6 do: python3

I hope this helps someone!


回答 8

正常供稿中的评论被屏蔽。让我写下为什么会发生这种情况,就像您执行应用程序时一样。

如果在安装环境之外的其他环境中运行脚本,python或ipython,则会遇到这些问题。

不要混淆重新安装它。Matplotlib通常安装在您的用户环境中,而不是sudo中。您正在改变环境。

因此,不要重新安装pip,只要将其安装在sudo环境中,请确保将其作为sudo运行。

Comment in the normal feed are blocked. Let me write why this happens, just like when you executed your app.

If you ran scripts, python or ipython in another environment than the one you installed it, you will get these issues.

Don’t confuse reinstalling it. Matplotlib is normally installed in your user environment, not in sudo. You are changing the environment.

So don’t reinstall pip, just make sure you are running it as sudo if you installed it in the sudo environment.


回答 9

我花了好几个小时不停地思考,直到我想检查一下.bash_profile为止。我没有列出python3的路径,因此添加了以下代码:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

然后使用重新安装matplotlib sudo pip3 install matplotlib。现在一切都运转良好。

I bashed my head on this for hours until I thought about checking my .bash_profile. I didn’t have a path listed for python3 so I added the following code:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.


回答 10

因此,我将python3 -m pip install matplotlib' thenimport matplotlib.pyplot用作plt`,并且有效。

So I used python3 -m pip install matplotlib' thenimport matplotlib.pyplot as plt` and it worked.