【发布时间】:2013-01-06 09:34:15
【问题描述】:
我正在尝试在 ubuntu 12.04 上的 Eclipse Helios 中使用 PyDev 2.7.1 构建 GTK+ 应用程序。 但是 Python 找不到 gobject.GObject 类。我安装了我能找到的任何 gobject 库并尝试了几个外部库路径,就像这些帖子中提到的那样 pydev issue with gobject 和 How to resolve these unresolved imports in PyDev ( eclipse )?。
但到目前为止一切正常。有谁知道解决方法/解决方案?
问候 CK
import pygtk
pygtk.require("2.0")
import gobject
class MyClass(gobject.GObject):
...
导入
from gi.repository import GObject
不管用。
我还在 Eclipse 中的 PYTHONPATH 中添加了 /usr/lib/pyshared/python2.7,其中包含 gtk-2.0、gi 和 gobject 模块,但没有任何区别。
我也为解释器添加了一个强制内置的 gi,但没有效果。
这里报告了一个错误 http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=649861 ,但自 pygobject 3.0.2-4 以来它已得到解决。我的 python-gobject 版本是 3.2.2-1
导入适用于在 PyDev 和终端中执行的测试脚本:
from gi.repository import GObject, Gtk
class Test(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
print ("test")
def main(self):
print ("Import has worked")
print (__name__)
if __name__ == '__main__':
test = Test()
test.main()
当我将它应用于类 MyClass(GObject.GObject) 时,我收到以下错误:
ImportError: could not import gobject (error was: ImportError('When using gi.repository you must not import static modules like "gobject".请将所有出现的“import gobject”更改为“from gi.repository导入 GObject".',))
没有import gobject,只有from gi.repository import GObject。
【问题讨论】:
-
如果您在终端中运行带有
from gi.repository import GObject的脚本,导入是否成功?例如,我在 Ubuntu 12.04 上运行 Python script that uses Gtk 没有任何问题 sys.path 操作或安装其他软件包。
标签: python ubuntu-12.04