我在ubuntu raring的python2.7中使用gobject-introspection,并且在构建一些包时遇到导入错误.我已经隔离了一组最小的步骤来复制它:
>创建本地目录结构:
gi:
__init__.py
overrides:
__init__.py
>放置标准样板
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
print __path__, __name__
在__init__.py文件中.
>从包含gi本地副本的目录中,运行以下命令:
python -c "from gi import repository"
>我收到一条错误消息,看起来像:
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
任何解释?我找不到任何关于预期行为的体面文档,因为gobject-introspection看起来像是一个记录很差的项目.非常感谢帮助!
解决方法:
从Python文档:
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.
只需从运行目录中访问那些__init__.py文件,就可以告诉解释器这是gi模块的一个实现.主gi模块的任何使用都将无法正常使用.
现在,为什么它会将错误打印为/usr/lib?因为在local / gi中找到了gi,但是在/usr/lib/python2.7/dist-packages/gi/repository中找到了gi.repository.它正在运行/usr/lib/python2.7/dist-packages/gi/repository/__init__.py.从那里,它正确地导入了一些其他子模块,但是当它尝试导入覆盖时,它会在gi / overrides中找到你的本地存根.您的存根没有定义注册表,因此您有失败.
尝试在gi / overrides / __ init__.py中放入registry =’dumb_string’,看看错误消失了.