Gtk *后端需要安装pygtk

时间:2023-01-24 15:06:12

From within a virtual environment, trying to load a script which uses matplotlib's GTKAgg backend, I fail with the following traceback:

在虚拟环境中,尝试加载使用matplotlib的GTKAgg后端的脚本,我失败了以下回溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 97, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module>
    raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

The code which I ran in order to produce that ImportError is as follows:

我为了产生ImportError而运行的代码如下:

import matplotlib as mpl
mpl.use('GTKAgg')
import matplotlib.pyplot as plt

When running the very same code after deactivating my virtual environment, everything goes well.

在停用我的虚拟环境后运行相同的代码时,一切顺利。

I assumed this may be due to version differences; indeed, such differences exist on my machine. However, the version in the virtual environment is newer (1.2.0 versus 1.1.1rc), so I am not expecting less support.

我认为这可能是由于版本差异造成的;实际上,我的机器上存在这种差异。但是,虚拟环境中的版本更新(1.2.0对1.1.1rc),所以我不期望得到更少的支持。

In case it is not clear: my question is how to allow importing pyplot with GTKAgg backend on a new version of matplotlib, or at least an attempt to understand the reasons for this import failure.

如果不清楚:我的问题是如何允许在新版本的matplotlib上导入带有GTKAgg后端的pyplot,或者至少试图了解导入失败的原因。

2 个解决方案

#1


5  

You probably created your virtual evn by something like:

你可能通过以下方式创建了你的虚拟evn:

 $ virtualenv ~/.virtualenvs/my_env

by default this can see none of your system-installed packages (including pygtk) so when you try to run mpl it rightly complains that you do not have pygtk installed because (with in the context of the virtualenv) you do not.

默认情况下,这可以看不到你的系统安装的软件包(包括pygtk)所以当你尝试运行mpl时,它会正确地抱怨你没有安装pygtk,因为(在virtualenv的上下文中)你没有。

You can either build and install pygtk within your virtualenv or you can use

您可以在virtualenv中构建和安装pygtk,也可以使用

$ virtualenv --system-site-packages ~/.virtualenvs/my_env

(doc) which will make your virtualenv inherit from your global packages.

(doc)将使你的virtualenv继承你的全局包。

#2


1  

I'm not sure if you're on Ubuntu, but to solve this I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:

我不确定你是否使用Ubuntu,但为了解决这个问题,我必须从源代码安装matplotlib才能使其工作。关键说明(来自http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/)是:

$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

Changing backends and using --system-site-packages didn't work for me.

更改后端和使用--system-site-packages对我来说不起作用。

#1


5  

You probably created your virtual evn by something like:

你可能通过以下方式创建了你的虚拟evn:

 $ virtualenv ~/.virtualenvs/my_env

by default this can see none of your system-installed packages (including pygtk) so when you try to run mpl it rightly complains that you do not have pygtk installed because (with in the context of the virtualenv) you do not.

默认情况下,这可以看不到你的系统安装的软件包(包括pygtk)所以当你尝试运行mpl时,它会正确地抱怨你没有安装pygtk,因为(在virtualenv的上下文中)你没有。

You can either build and install pygtk within your virtualenv or you can use

您可以在virtualenv中构建和安装pygtk,也可以使用

$ virtualenv --system-site-packages ~/.virtualenvs/my_env

(doc) which will make your virtualenv inherit from your global packages.

(doc)将使你的virtualenv继承你的全局包。

#2


1  

I'm not sure if you're on Ubuntu, but to solve this I had to install matplotlib from source to get this to work. The key instructions (from http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/) are:

我不确定你是否使用Ubuntu,但为了解决这个问题,我必须从源代码安装matplotlib才能使其工作。关键说明(来自http://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/)是:

$ workon plotting
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

Changing backends and using --system-site-packages didn't work for me.

更改后端和使用--system-site-packages对我来说不起作用。