简体   繁体   English

导入错误:没有名为 _backend_gdk 的模块

[英]ImportError: No module named _backend_gdk

I am starting to get some insight into interactive plotting with python and matplotlib using pyGTK+.我开始深入了解使用 pyGTK+ 使用 python 和 matplotlib 进行交互式绘图。 Therefore I took a look at the example given at the matplotlib website .因此,我查看了 matplotlib 网站上给出示例

This is a short exerpt of the Code:这是代码的简短摘录:

#!/usr/bin/env python
"""
Example of embedding matplotlib in an application and interacting with
a treeview to store data.  Double click on an entry to update plot
data

"""
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk

import matplotlib
matplotlib.use('GTKAgg')  # or 'GTK'
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas

from numpy.random import random
from matplotlib.figure import Figure

Ones I try to run this Script in the Terminal I get the following error:我尝试在终端中运行此脚本时出现以下错误:

Traceback (most recent call last):
  File "gtk_spreadsheet.py", line 15, in <module>
    from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 33, in <module>
    from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
    from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk

I have python 2.7 and pygtk 2.24 installed.我安装了 python 2.7 和 pygtk 2.24。

Can anyone figure out where the error is located?谁能找出错误所在的位置? I think it might be connected to some linking issues?我认为它可能与某些链接问题有关?

Note that the Debian/Ubuntu package you need is not 'pygtk2-devel' but 'python-gtk2-dev':请注意,您需要的 Debian/Ubuntu 软件包不是“pygtk2-devel”而是“python-gtk2-dev”:

sudo apt-get install python-gtk2-dev

should fix this error on these platforms.应该在这些平台上修复这个错误。

This was a symptom of using a pip-installed matplotlib instead of an apt-installed matplotlib on my system, just now.这是刚刚在我的系统上使用 pip 安装的 matplotlib 而不是 apt 安装的 matplotlib 的症状。 If on Ubuntu/Debian, try:如果在 Ubuntu/Debian 上,请尝试:

pip uninstall matplotlib
apt install python-matplotlib

I believe what was happening is that the pip-install didn't build the C extension required for GTK output, but the apt package has the extension prebuilt.我相信发生的事情是 pip-install 没有构建 GTK 输出所需的 C 扩展,但 apt 包已经预构建了扩展。 I don't have logs from the initial pip install of matplotlib, so I can't confirm that that's what happened.我没有来自 matplotlib 的初始 pip 安装的日志,所以我无法确认这就是发生的事情。

In addition to Haldean Brown's answer, note that if you really need to use pip you can force it to recompile matplotlib locally and get "the deep magic that setup.py does" with the --no-binary option:除了 Haldean Brown 的回答之外,请注意,如果您确实需要使用 pip,您可以强制它在本地重新编译 matplotlib 并使用--no-binary选项获得“setup.py 所做的深层魔法”:

pip uninstall matplotlib
pip install matplotlib --no-binary=matplotlib

This will solve your problem, provided that you already installed gtk2 with sudo apt-get install python-gtk2-dev这将解决您的问题,前提是您已经使用sudo apt-get install python-gtk2-dev

As you want to use the GTKAgg backend, Using pip may prove useful in the future to freeze matplotlib at a version where it is supported (the deprecation warning states it will be dropped in 3.0):由于您想使用 GTKAgg 后端,因此将来使用 pip 可能会证明在支持它的版本中冻结 matplotlib 很有用(弃用警告指出它将在 3.0 中删除):

pip install matplotlib==2.2.2 --no-binary=matplotlib

Ubuntu 18.04 has a broken matplotlib package. Ubuntu 18.04 有一个损坏的 matplotlib 包。 See this bug: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458看到这个错误: https : //bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458

You can either compile matplotlib yourself (eg with pip) or use the PPA linked from the bug report.您可以自己编译 matplotlib(例如使用 pip)或使用错误报告中链接的 PPA。

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

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