5

我看过这个,并尝试了以下代码:

ln -s /usr/lib/python2.7/dist-packages/pygtk.pth tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/gobject tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/gtk-2.0 tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/pygtk.pth tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/glib tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/gi tools/python_2_7_9/lib/python2.7/site-packages/
ln -s /usr/lib/python2.7/dist-packages/pygtkcompat tools/python_2_7_9/lib/python2.7/site-packages/

,但import glibimport gi仍会产生错误:

yba@ubuntu:~/Documents/XXX/tools$ source python_2_7_9/bin/activate
(python_2_7_9) yba@ubuntu:~/Documents/XXX/tools$ python
Python 2.7.9 (default, Aug 29 2016, 16:04:36) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import glib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yba/Documents/XXX/tools/python_2_7_9/lib/python2.7/dist-packages/glib/__init__.py", line 22, in <module>
    from glib._glib import *
ImportError: /home/yba/Documents/XXX/tools/python_2_7_9/lib/python2.7/dist-packages/glib/_glib.so: undefined symbol: PyUnicodeUCS4_DecodeUTF8
>>> import gi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yba/Documents/XXX/tools/python_2_7_9/lib/python2.7/dist-packages/gi/__init__.py", line 36, in <module>
    from ._gi import _gobject
ImportError: /home/yba/Documents/lucida/tools/python_2_7_9/lib/python2.7/dist-packages/gi/_gi.so: undefined symbol: PyUnicodeUCS4_FromUnicode
>>> 

与那篇文章类似,系统范围的 python 工作正常:

yba@ubuntu:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> import glib
>>> 

如何解决这个问题?另外,我真正需要的是import gi.repository而不是import gi. 非常感谢!

4

3 回答 3

6

现在可以使用vext解决这个问题。Vext 允许您在单独访问您的系统包的 virtualenv 中安装包。要访问gi,请执行以下操作:

pip install vext
pip install vext.gi
于 2017-05-06T06:10:26.273 回答
-2

您需要在虚拟环境中安装必要的模块。

激活后,您必须pip install <library name>. 在你的情况下,它应该是pip install gi

于 2016-08-29T20:54:53.453 回答
-2

首先,请记住,虚拟环境(2.7.9)使用的 Python 与系统范围的 Python(2.7.6)不同,所以我看不出在它们之间进行比较的意义.

您可以做的一件事是从头开始创建虚拟环境,但使用-p标志来指示应该使用哪个 Python 版本。像这样:

virtualenv -p /usr/bin/python2.7 <virtualenv/new/path/>

其次,undefined symbol: PyUnicodeUCS4_FromUnicode您使用 2.7.9 版本报告的错误可能与 Python 源代码的不正确编译有关。尝试再次编译它们,但请注意--enable-unicode=ucs4该行中的选项./configure

$> tar -xf Python-2.7.6.tar
$> cd Python-2.7.6
$> ./configure --prefix=/usr/local --enable-shared --enable-unicode=ucs4
$> make && make altinstall
于 2016-11-18T07:33:52.573 回答