2015-04-03 207 views
59

我正在虚拟环境中使用烧瓶。我能够用pip安装matplotlib,并且我可以在Python会话中使用import matplotlib。然而,当我导入为无法在virtualenv中导入“matplotlib.pyplot as plt”

matplotlib.pyplot as plt 

我得到以下错误:

>>> 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. 

我感到困惑,为什么要求我先安装Python的框架。它不存在吗? “将Python安装为框架”意味着什么,以及如何安装它?

+0

你在做什么操作系统? – 2016-06-29 04:12:19

+0

这是MacOS。我相信它是10.6。 – Rohit 2016-06-29 15:22:59

回答

135

solution为我工作。如果你已经在使用PIP在虚拟环境中安装matplotlib,您只需键入以下内容:

$ cd ~/.matplotlib 
$ nano matplotlibrc 

,然后在那里写backend: TkAgg。 如果您需要更多信息,请转到解决方案链接。

+0

好又容易。非常感谢! – 2016-06-24 01:14:08

+33

单行:'echo“后端:TkAgg”>〜/ .matplotlib/matplotlibrc'或'echo“后端:Agg”>〜/ .matplotlib/matplotlibrc' – msanford 2016-07-13 21:57:38

+0

作品非常感谢 – Oliver 2017-03-07 15:53:56

13

当我使用pip安装matplotlib时,我遇到了类似的问题。默认情况下,它安装了1.5.0的最新版本。但是,我有另一个Python 3.4和matplotlib 1.4.3的虚拟环境,当我导入matplotlib.pyplot时,这个环境工作正常。因此,我用下面安装matplotlib的早期版本:

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

我知道这只是一个变通,但它的工作对我来说是一个短期修复。

+0

这对我来说很好。谢谢。 – Ankur 2017-11-29 23:23:45

+0

谢谢!我喜欢这个答案! – Hanfeng 2018-01-02 14:02:09

+0

对我来说,我有一个python 2.7 pyenv环境,其中matplotlib == 2.1.0和python 3.6以及matplotlib == 2.1.1。我将2.7的matplotlib升级到2.1.1,问题得到解决。 – bajafresh4life 2018-01-03 21:47:05

0

尽管大多数答案似乎指向修补activate脚本以使用系统python,但我无法让它工作,并且对我来说一个简单的解决方案 - 尽管有点小巧 - 是将matplotlib安装到全局环境中,并且使用它来代替virtualenv实例。您可以通过使用--system-site-packages标志(如virtualenv --system-site-packages foo)创建virtualenv来执行此操作,也可以在安装pip时使用通用标志,如pip install -U matplotlib

4

您可以通过使用后端Agg

转到User/yourname/.matplotlib和打开/创建matplotlibrc修复此问题并添加以下行backend : Agg,它应该为你工作。

27

我得到了同样的错误,并试图Jonathan的回答是:

You can fix this issue by using the backend Agg

Go to User/yourname/.matplotlib and open/create matplotlibrc and add the following line backend : Agg and it should work for you.

我运行的程序,没有错误,也没有情节,我试图backend: Qt4Agg, 它打印出来,我的天堂“没有安装PyQt4。

然后我尝试了另一个后端:backend: TkAgg,它的工作原理!

所以,也许我们可以尝试不同的后端,有些可能会工作或安装像PyQt4这样的需求包。

这里是一个示例python片段,你可以尝试和测试matplotlib。

import matplotlib 

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

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

我正在使用macOS high sierra,当我使用TkAgg时,它显示我没有安装tkinter软件包。当我使用Qt4Agg或Qt5Agg时,它显示我没有安装PyQt。如果我使用'brew install pyqt'安装PyQt,这并没有帮助。最后,我必须将它安装在virtualenv中,“激活virtualenv,然后'pip安装PyQt5'”。这工作。 – nngeek 2017-11-21 01:57:17

6

如果你不希望设置一个.matplotib/matplotlibrc配置文件,您可以通过设置在运行时'Agg'后端进口matplotlib后,进口matplotlib.pyplot前右绕过这个问题:

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') 

enter image description here

0

一干净而简单的解决方案是创建一个内核,将PYTHONHOME设置为'VIRTUAL_ENV`,然后使用系统Python可执行文件(而不是virtualenv中的文件)。

如果要自动创建此类内核,可以使用jupyter-virtualenv-osx脚本。

相关问题