![](/img/trans.png)
[英]from matplotlib.pyplot import plt ImportError: No module named 'matplotlib'
[英]ImportError: No module named 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。 但是,我自己运行它./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?
环境是:
numpy、scipy、matplotlib 安装有:
sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose
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
你应该通过了。
你的机器上安装了两个python,一个是Mac OSX附带的标准python,第二个是你安装的带有端口的python(这是在其库中安装了matplotlib
的一个,macosx附带的一个没有)。
/usr/bin/python
是标准的mac python,因为它没有matplotlib
,所以你应该总是用安装了ports的脚本来启动你的脚本。
如果python your_script.py
有效,则更改#!
至:
#!/usr/bin/env python
或者将完整路径放入在其库中安装了matplotlib
的 python 解释器。
如果您使用的是 Python 2,只需运行
sudo apt-get install python-matplotlib
获取matplotlib
的最佳方法是:
pip install matplotlib
因为以前的方式可能会给你一个旧版本的matplotlib
这对我有用,灵感来自 Sheetal Kaul
pip uninstall matplotlib
python3 -m pip install matplotlib
当它起作用时,我知道它安装在错误的位置:
python2.7
import matplotlib
首先检查Python的版本
对于python2:
sudo apt-get install python-matplotlib
对于python3:
sudo apt-get install python3-matplotlib
如果您不匹配 Matplotlib 安装和 Python 版本,您将收到 no-module-error,因为该版本没有模块存在。
如果你使用 Anaconda3
就放
conda install -c conda-forge matplotlib
您可以通过执行此操作在 python 3 中安装matplotlib包
python3 -m pip install matplotlib --user
它对我有用。
如果您已安装 pip 并将 Python 配置为路径变量,只需在终端中运行此命令即可。
pip install matplotlib
所以我使用了python3 -m pip install matplotlib' then
import matplotlib.pyplot as plt` 并且它起作用了。
我有一个类似的问题,使用 pip3 并且所有这些东西都适用于安装 matplotlib 但不适用于 pyplot。 这为我解决了它:
import matplotlib as plt
from matplotlib import pyplot as pllt
我有一个类似的问题已解决,这是我的问题:
我在 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 install 安装 3.6 时执行:pip3 install
并且在运行 2.7 的代码时执行:python 在运行 3.6 的代码时执行:python3
我希望这可以帮助别人!
普通提要中的评论被阻止。 让我写下为什么会发生这种情况,就像您执行应用程序时一样。
如果您在安装它的环境之外的另一个环境中运行脚本、python 或 ipython,您将遇到这些问题。
不要混淆重新安装它。 Matplotlib 通常安装在您的用户环境中,而不是 sudo。 你正在改变环境。
所以不要重新安装 pip,只要确保你在 sudo 环境中安装它时,它是作为 sudo 运行的。
我为此苦恼了好几个小时,直到我考虑检查我的 .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
然后使用sudo pip3 install matplotlib
。 现在一切都很好。
我的虚拟环境目录和项目目录的文件权限不正确,因此不允许我安装正确的软件包。 我通过运行更新了它们:
sudo chown user:user -R [project folder]
sudo chown user:user -R [environment folder]
在上面你应该使用你自己的用户名代替“用户”。 -R 递归遍历所有子文件夹和文件。
在 django 项目上工作并面临同样的问题,这就是我所做的。 只需在 python 终端中编写 pip show matplotlib 即可检查您是否已经拥有 matplotlib。
如果您在 venv 中,请不要在 venv 中安装这些库。 从外面安装它们对我有帮助。
如果您在使用venv
并像我一样运行pytest
时遇到此错误ModuleNotFoundError: No module named 'matplotlib'
,您可以通过比较type pytest
显示的路径type python
显示的路径类型来检查。 在我的情况下pytest
不在venv/bin
目录下,例如python
,我通过pip
安装了pytest
并重新激活venv
。 见https://stackoverflow.com/a/54597424/3622415
我已经使用 pip 和 conda 安装了 matplotlib,但是遇到了这个错误ModuleNotFoundError: No module named 'matplotlib' 。
我通过以下修复。
因为它可能有旧版本的 Jupyter 笔记本,所以我尝试使用此命令将其安装在当前内核中。
import sys
!{sys.executable} -m pip install seaborn
在新的 Jupyter 版本(2019)中,可以简单地安装为:
%pip install matplotlib
我通过 conda 解决了。 一旦安装了 miniconda 或 anaconda,输入
conda install matplotlib
然后,当出现提示时,键入
y
在sublime text中你可以设置
"cmd": ["python3", "-u", "$file"]
在tools > buildsystem > new build system
它对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.