Python 2.7 ImportError:没有名为repository的模块。

我完全不知道怎么办。我卸载了gi和python2.7,然后重新安装了它们。你们有什么想法吗?
~ > /usr/bin/python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> import gi.repository
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named repository

谢谢!

这个链接有帮助吗?http://askubuntu.com/questions/80448/what-would-cause-the-gi-module-to-be-missing-from-python - mchid
1嘿!谢谢评论!我查看了那篇帖子。重新配置了Python2.7,并重新安装了python-gobject和python-gobject-2。/usr/lib/python2.7/dist-packages在路径中。gi存在。但是不知道为什么gi.repository不存在。不确定那是什么。@mchid - Tyler Berezowsky
4个回答

您需要安装以下软件包:
sudo apt-get install --reinstall python-gi

这个软件包包含了用于支持gobject-introspection的库的Python 2.x绑定生成器,也就是那些提供gir1.2--软件包的库。通过这些软件包,可以从Python中使用这些库。

我已经卸载并重新安装了python-gi。 - Tyler Berezowsky
这很奇怪,你是在虚拟环境中启动解释器吗?如果是的话,尝试使用mkvirtualenv environment --system-site-packages来代替。 - Sylvain Pineau
嘿 @Sylvain Pineau,谢谢帮忙!我不这么认为。只是发现缺少了gobject-introspection!可惜的是,我仍然无法导入gi.repository或运行software-center。 - Tyler Berezowsky

在终端中输入命令:
sudo apt-get install python-gobject

我在使用Ubuntu 16.04时遇到了同样的问题。我的问题是使用pip更新了python包。
我的解决方案是使用pip删除该包,并使用apt-get重新安装它:
sudo -H pip uninstall gi

sudo apt-get install --reinstall python-gi

我同意Sylvain Pineau的观点,但是对我来说,仅仅使用apt-get重新安装python-gi是不够的。

谢谢大家的回复。

我在我的 /usr/local/bin 中安装了另一个版本的 Python。


我强烈推荐使用虚拟环境。这解决了任何冲突依赖的问题。虚拟环境非常容易安装,基本上为每个虚拟环境创建了一个独立的Python环境。这样,您可以在项目Y中使用X包的版本1,而在项目Z中使用X包的版本2,而不会遇到依赖地狱的问题。 - Gx1sptDTDa
我主要使用Anaconda发行版来避免使用Matlab,而且大部分时间都是用Python。最近开始尝试使用GTK来开发GUI界面,但不知怎么的,把一切都搞砸了。我想知道是否可以通过虚拟环境来解决这个问题,以免影响到系统中已安装的Python?也就是说,我能否在虚拟环境中访问Ubuntu的通知和面板功能? - Tyler Berezowsky
绝对没问题。 - Gx1sptDTDa