4

I am using gobject-introspection in python2.7 on ubuntu raring and I run into an import error while building some packages. I have isolated a minimal set of steps to replicate it:

  1. Make a local directory structure:

    gi:
            __init__.py
            overrides:
                    __init__.py
    
  2. Put the standard boilerplate

    from pkgutil import extend_path
    
    __path__ = extend_path(__path__, __name__)
    print __path__, __name__
    

    in both __init__.py files.

  3. From the directory containing your local copy of gi, run the following:

    python -c "from gi import repository"
    
  4. I get an error message that looks like:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python2.7/dist-packages/gi/repository/__init__.py", line 25, in         <module>
        from ..importer import DynamicImporter
      File "/usr/lib/python2.7/dist-packages/gi/importer.py", line 28, in <module>
        from .module import DynamicModule
      File "/usr/lib/python2.7/dist-packages/gi/module.py", line 37, in <module>
        from .overrides import registry
    ImportError: cannot import name registry
    

Any explanation? I can't find any decent documentation of the intended behavior because gobject-introspection seems like a very poorly documented project. Help is greatly appreciated!

jamylak
  • 128,818
  • 30
  • 231
  • 230
pre-kidney
  • 193
  • 9

1 Answers1

1

From the Python documentation:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

Simply by having those __init__.py files accessible from the running directory, you are telling the interpreter that that is an implementation of the gi module. Any usage of the main gi module will not go correctly.

Now, why does it print the error as coming from /usr/lib ? Because gi was found in local/gi, but gi.repository was found in /usr/lib/python2.7/dist-packages/gi/repository . It is running /usr/lib/python2.7/dist-packages/gi/repository/__init__.py. From there, it imports some other submodules correctly, but when it tries to import overrides it finds your local stub in gi/overrides. Your stub does not define registry, so you have the fail.

Try and put registry='dumb_string' in gi/overrides/__init__.py and see that the error is gone.

salicideblock
  • 388
  • 3
  • 8
  • Actually, this was one of the first things I tried. The issue is that I actually need to use the registry to import packages. So if I later try to import something like Gtk from the gi repository, I get an import error. Essentially, I am trying to find out the correct way to do a local override of a specific gi repository package, in such a way that there is a fall over to the global gi repository information. Does that make sense? – pre-kidney May 22 '13 at 16:59
  • That's another question then, one I have no idea how to answer :( Good luck! – salicideblock May 26 '13 at 14:19