无法在 virtualenv 中 "import matplotlib.pyplot as plt"

Unable to "import matplotlib.pyplot as plt" in virtualenv

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

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 作为框架。不是已经存在了吗? "install Python as framework" 是什么意思,我该如何安装它?

我在使用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

我知道这只是一种变通方法,但它对我来说是一种短期修复方法。

虽然大多数答案似乎都指向修补 activate 脚本以使用系统 python,但我很难让它工作,而且对我来说是一个简单的解决方案 - 虽然有点尴尬 -是将 matplotlib 安装到全局环境并使用它而不是 virtualenv 实例。你可以通过像 virtualenv --system-site-packages foo 这样的 --system-site-packages 标志创建你的 virtualenv 来做到这一点,或者在像 pip install -U matplotlib.

这样的 pip 安装时使用通用标志。

您可以使用后端解决此问题 Agg

转到 User/yourname/.matplotlib 和 open/create matplotlibrc 并添加以下行 backend : Agg 它应该适合您。

我遇到了同样的错误,并尝试了 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()

这个 solution 对我有用。如果您已经在虚拟环境中使用 pip 安装了 matplotlib,则只需键入以下内容:

$ cd ~/.matplotlib
$ nano matplotlibrc

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

如果您不想设置 .matplotib/matplotlibrc 配置文件,您可以通过在导入 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')

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

如果你想自动创建这样一个内核,你可以使用jupyter-virtualenv-osx脚本。