无法“将 matplotlib.pyplot 导入为 plt";在虚拟环境中 [英] Unable to "import matplotlib.pyplot as plt" in virtualenv

查看:59
本文介绍了无法“将 matplotlib.pyplot 导入为 plt";在虚拟环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在虚拟环境中使用烧瓶.我能够使用 pip 安装 matplotlib,并且我可以在 Python 会话中import matplotlib.但是,当我将其导入为

matplotlib.pyplot 作为 plt

我收到以下错误:

<预><代码>>>>导入 matplotlib.pyplot 作为 plt回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中 中的文件//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py",第 109 行_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()文件//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py",第32行,在pylab_setupglobals(),locals(),[backend_name],0)文件//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",第24行,在<module>从 matplotlib.backends 导入 _macosx运行时错误:Python 未作为框架安装.如果 Python 没有作为框架安装,Mac OS X 后端将无法正常运行.有关在 Mac OS X 上将 Python 作为框架安装的更多信息,请参阅 Python 文档.请重新安装 Python 作为框架,或尝试使用其他后端之一.

我很困惑为什么它要求我安装 Python 作为框架.不是已经存在了吗?将 Python 安装为框架"是什么意思,我该如何安装?

解决方案

这个解决方案 为我工作.如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则只需输入以下内容:

$ cd ~/.matplotlib$ nano matplotlibrc

然后,在其中写入 backend: TkAgg.如果您需要更多信息,只需转到解决方案链接.

I am working with flask in a virtual environment. I was able to install matplotlib with pip, and I can import matplotlib in a Python session. However, when I import it as

matplotlib.pyplot as plt

I get the following error:

>>> import matplotlib.pyplot as plt

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 109, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "//anaconda/envs/myenv/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 24, in <module>
    from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends.

I am confused about why it asks me to install Python as framework. Doesn't it already exists? What does it mean to "install Python as framework", and how do I install it?

解决方案

This solution worked for me. If you already installed matplotlib using pip on your virtual environment, you can just type the following:

$ cd ~/.matplotlib
$ nano matplotlibrc

And then, write backend: TkAgg in there. If you need more information, just go to the solution link.

这篇关于无法“将 matplotlib.pyplot 导入为 plt";在虚拟环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆