Gobject-introspection transformations cause import errors

I am using gobject-introspection in python2.7 on ubuntu raring and I am running an import error when creating some packages. I have outlined the minimum set of steps for replicating it:

  • Create a local directory structure:

    gi: __init__.py overrides: __init__.py 
  • Put a standard template

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

    in both __init__.py files.

  • From the directory containing your local copy of gi , do the following:

     python -c "from gi import repository" 
  • An error message appears:

     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 cannot find decent documentation about the alleged behavior, because gobject-introspection seems to be a very poorly documented project. Help is much appreciated!

+4
source share
1 answer

From the Python documentation:

The __init__.py files are necessary for Python to treat directories as containing packages ; this is done to prevent directories with a common name, such as a string, inadvertently hiding the active modules that appear later in the module search path.

Just having these __init__.py files available from a working directory, you tell the interpreter that this is an implementation of the gi module. Any use of the main gi module will not be correct.

Now, why is it printing an error 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 works /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 detect the registry, so you have a failure.

Try adding registry='dumb_string' to gi/overrides/__init__.py and see that the error gi/overrides/__init__.py away.

+1
source

Source: https://habr.com/ru/post/1479275/


All Articles