Python 导入错误:没有名为 matplotlib.pyplot 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18176591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named matplotlib.pyplot
提问by songsong
I am currently practicing matplotlib. This is the first example I practice.
我目前正在练习 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()
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:
当我使用 运行此脚本时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
Does python look for matplotlib in different locations?
python 会在不同的位置寻找 matplotlib 吗?
The environment is:
环境是:
Mac OS X 10.8.4 64bit
built-in python 2.7
numpy, scipy, matplotlib is installed with:
numpy、scipy、matplotlib 安装有:
sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose
采纳答案by Viktor Kerkez
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).
你的机器上安装了两个python,一个是Mac OSX自带的标准python,第二个是你用ports安装的(这个是matplotlib
在它的库中安装的,macosx自带的没有) .
/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.
是标准的 mac python,因为它没有,所以matplotlib
你应该总是用安装了端口的脚本来启动你的脚本。
If python your_script.py
works then change the #!
to:
如果python your_script.py
有效,则将其更改#!
为:
#!/usr/bin/env python
Or put the full path to the python interpreter that has the matplotlib
installed in its library.
或者将完整路径放在已matplotlib
安装在其库中的 python 解释器。
回答by Sheetal Kaul
pip
will make your life easy!
pip
会让你的生活变得轻松!
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.htmlor directly here: https://bootstrap.pypa.io/get-pip.py(You may have to use Save As ..)
第 1 步:安装 pip - 只需在 python 控制台中编写 pip 即可检查您是否已经安装了 pip。如果你不具备画中画,会得到一个名为get-pip.py Python脚本,通过在这里:https://pip.pypa.io/en/latest/installing.html或直接在这里:HTTPS://bootstrap.pypa .io/get-pip.py(您可能必须使用另存为..)
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"
第 2 步:记下文件的保存位置,然后从命令提示符 cd 目录。运行 get-pip.py 脚本来安装 pip。您可以在 cmd 中用引号将这一行写入:“python .\get-pip.py”
Step 3: Now in cmd type: pip install matplotlib
第 3 步:现在在 cmd 中输入: pip install matplotlib
And you should be through.
你应该通过。
回答by k.biao
If you are using Python 2, just run
如果您使用的是 Python 2,只需运行
sudo apt-get install python-matplotlib
The best way to get matplotlib
is :
最好的获取方式matplotlib
是:
pip install matplotlib
cause the previous way may give you a old version of matplotlib
因为以前的方式可能会给你一个旧版本 matplotlib
回答by David
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:
我为此苦恼了几个小时,直到我想到检查我的 .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
And then re-installed matplotlib with sudo pip3 install matplotlib
. All is working beautifully now.
然后用sudo pip3 install matplotlib
. 现在一切都很好。
回答by Punnerud
This worked for me, inspired by Sheetal Kaul
这对我有用,灵感来自 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
回答by Dinob661
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
我在 python3 上设置了所有内容,但我使用 python 来调用我的文件,例如:我正在输入“python mnist.py”......因为我在 python3 上拥有所有内容,所以我想我正在尝试使用 python 2.7
The correction: "python3 mnist.py" - the 3 made all the difference
更正:“python3 mnist.py” - 3 完全不同
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)
我绝不是python或pip的专家,但pip和pip3之间肯定有区别(pip绑定到python 2.7)(pip3绑定到python 3.6)
so when installing for 2.7 do: pip install when installing for 3.6 do: pip3 install
因此,在为 2.7 安装时执行:pip install 为 3.6 安装时执行:pip3 install
and when running your code for 2.7 do: python when running your code for 3.6 do: python3
并在运行 2.7 的代码时执行:python 运行 3.6 的代码时执行:python3
I hope this helps someone!
我希望这可以帮助别人!
回答by Alekssandro Assis Barbosa
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.
如果您在不同于安装它的环境中运行脚本、python 或 ipython,则会遇到这些问题。
Don't confuse reinstalling it. Matplotlib is normally installed in your user environment, not in sudo. You are changing the environment.
不要混淆重新安装它。Matplotlib 通常安装在您的用户环境中,而不是安装在 sudo 中。你正在改变环境。
So don't reinstall pip, just make sure you are running it as sudo if you installed it in the sudo environment.
所以不要重新安装pip,如果你在sudo环境中安装了它,请确保你以sudo的身份运行它。
回答by negi
first check version of python
首先检查python的版本
for python2 Vesion
对于 python2 版本
sudo apt-get install python-matplotlib
for python3 version
对于python3版本
sudo apt-get install python3-matplotlib
if you miss-match matplotlib installation with python version you will get No module error because no module for that version exits.
如果您错过了与 python 版本匹配的 matplotlib 安装,您将收到 No module 错误,因为该版本的模块不存在。
回答by Hoang Tran Son
回答by Ashiq Imran
If you using Anaconda3
如果您使用 Anaconda3
Just put
就放
conda install -c conda-forge matplotlib