简体   繁体   中英

Undefined variable from import: GObject

I'm trying to build a GTK+ application with PyDev 2.7.1 in Eclipse Helios on ubuntu 12.04. But Python cannot find the class gobject.GObject. I installed any gobject library i could find and tried several external library paths, like mentioned in these post pydev issue with gobject and How to resolve these unresolved imports in PyDev ( eclipse )? .

but anything worked so far. Does anyone know a workaround/solution ?

Regards Ck

  import pygtk
  pygtk.require("2.0")
  import gobject

  class MyClass(gobject.GObject):
  ...

the import

  from gi.repository import GObject

doesn't work eather.

I also added /usr/lib/pyshared/python2.7, in which the gtk-2.0, gi and gobject modules are located, to my PYTHONPATH in Eclipse, but it made no difference.

I added a forced builtin for gi to the interpreter as well, but without effect.

here a bug is reported http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=649861 , but also that it has been solved since pygobject 3.0.2-4. my version of python-gobject is 3.2.2-1

The import works for the test script executed in PyDev and in a terminal:

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()

When I apply this to class MyClass(GObject.GObject) I get the following error:

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".',))

There is no import gobject , only from gi.repository import GObject .

I finally found it.

import gobject


class MyClass(gobject.GObject):

def __init__(self, channelstrip, name, s_type, mainWin):
    gobject.GObject.__init__(self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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