5

I have looked at this, and tried the following code:

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/

,but import glib or import gi still generates errors:

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
>>> 

Similar to that post, the system-wide python works fine:

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
>>> 

How to solve this issue? Also, what I really need is import gi.repository rather than import gi. Thanks a lot!

Community
  • 1
  • 1
Yunsheng Bai
  • 179
  • 3
  • 14

3 Answers3

5

It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access gi, do the following:

pip install vext
pip install vext.gi
lofidevops
  • 11,655
  • 11
  • 69
  • 101
  • this just gives me a long string of error messages, and then a failure :( – Erik Aronesty Oct 16 '17 at 19:31
  • @ErikAronesty I'm not on the same system anymore so I can't double check - maybe post an issue https://github.com/stuaxo/vext/issues and if I've missed a step or something like being explicit about installing gi itself, please feel free to edit – lofidevops Oct 16 '17 at 21:48
  • 1
    This worked for me. If you don't want vext just create a virtualenv with --system-site-packages. Or for pipenv: 'pipenv --python 3.6 --site-packages'. – roeland Dec 01 '17 at 11:57
-2

You need to install the necessary modules on your virtual environment.

After activating it, you have to pip install <library name>. In your case, it should be pip install gi

  • (python_2_7_9) yba@ubuntu:~/Documents/XXX/tools$ pip install glib Collecting glib Could not find a version that satisfies the requirement glib (from versions: ) No matching distribution found for glib – Yunsheng Bai Aug 29 '16 at 21:16
  • So are you sure it is `pip install glib`? Thanks! – Yunsheng Bai Aug 29 '16 at 21:17
  • It looks like the library is called gi, I edited the answer accordingly. If it works, please mark it as a valid answer. – Daniel Kravetz Malabud Aug 29 '16 at 21:20
  • It partially works, because this still does not work: `from gi.repository import GObject`. >>> import gi >>> from gi.repository import GObject Traceback (most recent call last): File "", line 1, in ImportError: No module named repository` – Yunsheng Bai Aug 29 '16 at 21:55
  • 1
    Basically, it only gives me gi, not gi.repository. Do you know how to install gi.repository as well? Thank you very much! – Yunsheng Bai Aug 29 '16 at 21:56
-2

First of all, keep in mind that the Python used by the virtual environment (2.7.9) it is not the same than the system-wide Python (2.7.6) so I don't see the point in making a comparison between them.

One thing you can do is to create the virtual environment from the scratch but using the -p flag to indicate which Python version should use. Like this:

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

Second of all, the undefined symbol: PyUnicodeUCS4_FromUnicode error you are reporting with the 2.7.9 version might be related with a non-proper compilation of the Python sources. Try compiling them again but pay attention to the --enable-unicode=ucs4 option in the ./configure line:

$> tar -xf Python-2.7.6.tar
$> cd Python-2.7.6
$> ./configure --prefix=/usr/local --enable-shared --enable-unicode=ucs4
$> make && make altinstall
Fernando Martin
  • 529
  • 5
  • 19