简体   繁体   English

无法在virtualenv中“将matplotlib.pyplot导入为plt”

[英]Unable to “import matplotlib.pyplot as plt” in virtualenv

I am working with flask in a virtual environment. 我正在虚拟环境中使用flask。 I was able to install matplotlib with pip, and I can import matplotlib in a Python session. 我能够用pip安装matplotlib,我可以在Python会话中import matplotlib 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. 我很困惑为什么它要求我安装Python作为框架。 Doesn't it already exists? 它不存在吗? What does it mean to "install Python as framework", and how do I install it? “将Python安装为框架”是什么意思,我该如何安装它?

This solution worked for me. 这个解决方案对我有用。 If you already installed matplotlib using pip on your virtual environment, you can just type the following: 如果您已在虚拟环境中使用pip安装了matplotlib,则只需键入以下内容:

$ cd ~/.matplotlib
$ nano matplotlibrc

And then, write backend: TkAgg in there. 然后,写backend: TkAgg在那里。 If you need more information, just go to the solution link. 如果您需要更多信息,请转到解决方案链接。

I got the same error, and tried Jonathan 's answer: 我得到了同样的错误,并尝试了Jonathan的回答:

You can fix this issue by using the backend Agg 您可以使用后端Agg来解决此问题

Go to User/yourname/.matplotlib and open/create matplotlibrc and add the following line backend : Agg and it should work for you. 转到User/yourname/.matplotlib并打开/创建matplotlibrc并添加以下行backend : Agg ,它应该适合您。

I run the program, no error, but also no plots, and I tried backend: Qt4Agg , it prints out that I haven't got PyQt4 installed. 我运行程序,没有错误,但也没有图,我尝试backend: Qt4Agg ,它打印出我没有安装PyQt4。

Then I tried another backend: backend: TkAgg , it works! 然后我尝试了另一个后端: backend: TkAgg ,它有效!

So maybe we can try difference backends and some may work or install the requeired packages like PyQt4. 所以也许我们可以尝试差异后端,有些可能会工作或安装像PyQt4这样的重新获得的软件包。

Here is a sample python snippet that you can try and test matplotlib. 这是一个示例python片段,您可以尝试并测试matplotlib。

import matplotlib

matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [0, 3, 7])
plt.show()

I had similar problem when I used pip to install matplotlib. 当我使用pip安装matplotlib时,我遇到了类似的问题。 By default, it installed the latest version which was 1.5.0. 默认情况下,它安装了最新版本1.5.0。 However, I had another virtual environment with Python 3.4 and matplotlib 1.4.3 and this environment worked fine when I imported matplotlib.pyplot. 但是,我有另一个使用Python 3.4和matplotlib 1.4.3的虚拟环境,当我导入matplotlib.pyplot时,这个环境运行正常。 Therefore, I installed the earlier version of matplotlib using the following: 因此,我使用以下方法安装了早期版本的matplotlib:

cd path_to_virtual_environment    # assume directory is called env3
env3/bin/pip install matplotlib==1.4.3

I know this is only a work-around, but it worked for me as a short-term fix. 我知道这只是一种解决方法,但它对我来说是一个短期的解决方案。

If you do not want to set a .matplotib/matplotlibrc configuration file, you can circumvent this issue by setting the 'Agg' backend at runtime right after importing matplotlib and before importing matplotlib.pyplot : 如果您不想设置.matplotib/matplotlibrc配置文件,可以通过在导入matplotlib和导入matplotlib.pyplot之前在运行时设置'Agg'后端来避免此问题:

In [1]: import matplotlib

In [2]: matplotlib.use('Agg')

In [3]: import matplotlib.pyplot as plt

In [4]: fig, ax = plt.subplots(1, 1)

In [5]: import numpy as np

In [6]: x = np.linspace(-1., 1.)

In [7]: y = np.sin(x)

In [8]: ax.plot(x, y)
Out[8]: [<matplotlib.lines.Line2D at 0x1057ecf10>]

In [9=]: fig.savefig('myplot.png')

在此输入图像描述

You can fix this issue by using the backend Agg 您可以使用后端Agg来解决此问题

Go to User/yourname/.matplotlib and open/create matplotlibrc and add the following line backend : Agg and it should work for you. 转到User/yourname/.matplotlib并打开/创建matplotlibrc并添加以下行backend : Agg ,它应该适合您。

Although most answers seem to point towards patching the activate script to use the system python, I was having trouble getting that to work and an easy solution for me - though a little cringey - was to install matplotlib to the global environment and use that instead of a virtualenv instance. 虽然大多数答案似乎都指向修补activate脚本以使用系统python,但是我无法使用它来工作,而且对我来说一个简单的解决方案 - 虽然有点吝啬 - 是将matplotlib安装到全局环境并使用它代替一个虚拟实例。 You can do this either by creating your virtualenv with the --system-site-packages flag like virtualenv --system-site-packages foo , or to use the universal flag when pip installing like pip install -U matplotlib . 您可以通过使用--system-site-packages标志创建virtualenv,如virtualenv --system-site-packages foo ,或者在pip install -U matplotlibpip install -U matplotlib时使用通用标志。

A clean and easy solution is to create a kernel that sets PYTHONHOME to VIRTUAL_ENV and then uses the system Python executable (instead of the one in the virtualenv). 一个干净而简单的解决方案是创建一个内核,将PYTHONHOME设置为VIRTUAL_ENV ,然后使用系统Python可执行文件(而不是virtualenv中的那个)。

If you want to automate the creation of such a kernel, you can use the jupyter-virtualenv-osx script. 如果要自动创建此类内核,可以使用jupyter-virtualenv-osx脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM