Python和Gtk+ 3的导入错误

4
我正在使用Gtk+ 3Python编写程序。我之前提交了一个有关我的程序的相关问题,链接在这里
现在,我更进一步,有以下导入信息:
import aplpy
import montage
import subprocess
from gi.repository import Gtk, GdkPixbuf

当我运行它时,会出现以下错误:
$ ./makeRGB-frame.py 
Traceback (most recent call last):
  File "./makeRGB-frame.py", line 34, in <module>
    from gi.repository import Gtk, GdkPixbuf
  File "/usr/lib64/python2.7/site-packages/gi/__init__.py", line 27, in <module>
    from ._gi import _API, Repository
ImportError: could not import gobject (error was: ImportError('When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject".',))

如果我改变导入的顺序:
from gi.repository import Gtk, GdkPixbuf
import aplpy
import montage
import subprocess

我也遇到了一个错误:
$ ./makeRGB-frame.py 
/usr/lib64/python2.7/site-packages/gobject/constants.py:24: Warning: g_boxed_type_register_static: assertion `g_type_from_name (name) == 0' failed
  import gobject._gobject
/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py:40: Warning: specified class size for type `PyGtkGenericCellRenderer' is smaller than the parent type's `GtkCellRenderer' class size
  from gtk import _gtk
/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py:40: Warning: g_type_get_qdata: assertion `node != NULL' failed
  from gtk import _gtk
Segmentation fault (core dumped)

导入的模块包括:APLpypython-montage
请帮我解决这些错误。非常感谢您的帮助!
Germán.
1个回答

3

您的一些模块正在使用静态Gtk+ 2.0绑定 "PyGtk": http://www.pygtk.org/ 您正在使用动态Gtk+ 3.0绑定 "PyGObject": https://live.gnome.org/PyGObject

您不能同时加载这两个库,如果这样做,将会导致导入错误和分段错误。

我猜测问题出在APLpy模块上,您应该检查源代码并查找PyGtk导入,或者询问开发人员是否支持PyGObject。


感谢您的回答@Havok。现在我确定问题出在APLpy上,因为注释其他部分不会出错。但是我没有看到任何在APLpy中导入的pygtk。命令grep -i pygtk /usr/lib/python2.7/site-packages/aplpy/*.py的输出为空。 - skytux
skytux 是对的,在 aplpy 中没有明确使用 gtk,但是 aplpy 使用 matplotlib,而 matplotlib 可以使用 gtk。@Havok,您有任何想法吗?matplotlib 是否会导致冲突? - keflavich
1
这个问题已经通过更改 matplotlib 后端 得到解决,具体请参见 这里 - skytux

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接