问题描述
在虚拟环境中,尝试加载使用 matplotlib 的 GTKAgg 后端的脚本,我失败以下追溯:
Traceback(最近一次调用最后一次):
文件< stdin>,line 1,在< module>
文件/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/pyplot.py,第97行,位于< module>
_backend_mod,new_figure_manager,draw_if_interactive,_show = pylab_setup()
文件/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py ,第25行,在pylab_setup
globals(),locals(),[backend_name])
文件/home/user/.virtualenvs/venv/local/lib/python2.7/site-packages /matplotlib/backends/backend_gtkagg.py,第10行,位于< 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,第16行,在< module>
提高ImportError(Gtk *后端需要安装pygtk。)
ImportError:Gtk *后端需要安装pygtk。
为了生成 ImportError
pre $ import $ matplotlib as mpl
mpl.use('GTKAgg')
import matplotlib.pyplot as plt
在停用我的虚拟环境后运行相同的代码时,好的。
我认为这可能是由于版本差异;事实上,我的机器上存在这种差异。但是,虚拟环境中的版本较新(1.2.0与1.1.1rc),所以我不希望获得较少的支持。
如果不明确:我的问题是如何在新版本的 c> GTKAgg 后端导入 pyplot code> matplotlib ,或者至少试图理解导入失败的原因。
您可能通过如下方式创建了您的虚拟evn:
$ virtualenv〜/ .virtualenvs / my_env
默认情况下,您可以看不到任何系统安装的软件包(包括pygtk),所以当您尝试运行mpl时,您没有安装pygtk,因为(在virtualenv的情况下)您不需要。
您可以在virtualenv中构建并安装pygtk,也可以使用
$ virtualenv --system-site-packages〜/ .virtualenvs / my_env
(),这将使你的virtualenv继承你的全局包。
From within a virtual environment, trying to load a script which uses matplotlib's GTKAgg backend, I fail with the following traceback:
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:
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.
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.
You probably created your virtual evn by something like:
$ 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.
You can either build and install pygtk within your virtualenv or you can use
$ virtualenv --system-site-packages ~/.virtualenvs/my_env
(doc) which will make your virtualenv inherit from your global packages.
这篇关于Gtk *后端需要安装pygtk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!