4

I'm having some strange issues with PyGTK in "virtualenv". gtk does not import in my virtualenv, while it does import in my global python install. (I wasn't having this particular issue last week, guessing some software update upset something.)

Is there a good way to resolve this behavior?

Shown here: importing gtk globally,

tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> gtk
<module 'gtk' from '/usr/lib/pymodules/python2.7/gtk-2.0/gtk/__init__.pyc'>

and then failing to import gtk,

tom@zeppelin:~$ workon py27
(py27)tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named gtk

Unfortunately, this has broken my ipython --pylab environment: http://pastebin.com/mM0ur7Hc

UPDATE: I was able to fix this by adding symbolic links as suggested by grepic / this thread: Python: virtualenv - gtk-2.0

with a minor difference, namely that my "cairo" package was located in /usr/lib/pymodules/python2.7/cairo/ rather than in /usr/lib/python2.7/dist-packages/cairo.

SECOND UPDATE: I also found it useful to add the following lines to my venv/bin/activate:

export PYTHONPATH=$PYTHONPATH:/home/tom/.virtualenvs/py27/lib/python2.7/dist-packages
export PYTHONPATH=$PYTHONPATH:/home/tom/.virtualenvs/py27/lib/python2.7/dist-packages/gtk-2.0
export PYTHONPATH=$PYTHONPATH:/usr/lib/pymodules/python2.7/gtk-2.0

(I suspect that one or more of these is unneccessary, but I've been fiddling around with this for too long and have decided to stop investigating -- my setup now works and so I'm satisfied.)

Problem solved! Thanks everyone.

halfer
  • 19,824
  • 17
  • 99
  • 186
tomr_stargazer
  • 199
  • 3
  • 12

5 Answers5

14

Try creating your virtual environment with the --system-site-packages flag.

Brett
  • 1,223
  • 13
  • 8
  • I'll consider trying this next time, thanks for the tip. As it is, this particular virtualenv is already heavily customized with tons of extra packages and I don't want to start from scratch. – tomr_stargazer Oct 29 '12 at 01:31
  • I was having the same problem and this solution worked. I recreated the virtualenv with that flag. – Luis Lezcano Airaldi Mar 04 '17 at 22:51
4

So gtk normally lives in a place like /usr/lib/python2.7/dist-packages which is in your Python path in your global environment, but not in your virtual environment.

You may wish to just add the path to gtk manually with something like

import sys
sys.path.append("/usr/lib/python2.7/dist-packages/gtk")

You could also change the path when you activate the virtual environment. Open up venv/bin/activate. Its a scary looking file, but at the end you can just put:

export PATH=$PATH:/my/custom/path

Save that and the next time you activate the virtual environment with:

source venv/bin/activate

your custom path will be in the path. You can verify this with

echo $PATH

An alternative approach suggested Python: virtualenv - gtk-2.0 is to go into your virtualenv directory and add a 'dist-packages' directory and create symbolic links to the gtk package you were using previously:

mkdir -p venv/lib/python2.7/dist-packages/
cd venv/lib/python2.7/dist-packages/

For GTK2:

ln -s /usr/lib/python2.7/dist-packages/glib/ glib
ln -s /usr/lib/python2.7/dist-packages/gobject/ gobject
ln -s /usr/lib/python2.7/dist-packages/gtk-2.0* gtk-2.0
ln -s /usr/lib/python2.7/dist-packages/pygtk.pth pygtk.pth
ln -s /usr/lib/python2.7/dist-packages/cairo cairo

For GTK3:

ln -s /usr/lib/python2.7/dist-packages/gi gi

Full disclosure: I feel that both these solutions are somewhat hackish, which is ok given that you say the question is urgent. There is probably a 'proper' way to extend a virtual environment so let us know if you eventually discover the better solution. You may have some luck with http://www.virtualenv.org/en/latest/index.html#creating-your-own-bootstrap-scripts

Community
  • 1
  • 1
dgh
  • 8,969
  • 9
  • 38
  • 49
  • OK, this seems to be the right direction. If I might ask a dumb question, how can I ensure that these new symbolic links are in my venv-python's path upon startup? – tomr_stargazer Oct 11 '12 at 02:47
  • OK, this is all very helpful! I have found that, within python, all I have to do in order to get GTK to work properly is the following lines: `import sys` `sys.path.append('/usr/lib/pymodules/python2.7/gtk-2.0/')` `sys.path.append('/usr/lib/pymodules/python2.7/')` `import gtk >>> gtk ` But when I insert those two paths into the `venv/bin/activate` shell file as suggested above, "echo $PATH" gives me the expected result but, within python, sys.path does not contain the new locations. – tomr_stargazer Oct 14 '12 at 23:32
3

Another way to do this is to create a .pth file in your virtualenv's site-packages dir

eg

(in <virtualenv>/lib/python2.7/site-packages/dist-packages.pth)

/usr/lib/python2.7/dist-packages/

This fixed the issue I was having with apt-get installed version of pycairo

Anentropic
  • 32,188
  • 12
  • 99
  • 147
2

If you want to include the links to the relevant system's python gtk-2.0 in the virtualenv, you can just use pip to install ruamel.venvgtk:

pip install ruamel.venvgtk

You don't have import anything, the links are setup during installation.

This is especially handy if you are using tox, in that case you only need to include the dependency (for tox):

deps:
    pytest
    ruamel.venvgtk

and a newly setup python2.7 environment will have the relevant links included before the tests are run.

Anthon
  • 69,918
  • 32
  • 186
  • 246
2

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 PyGTK, do the following:

pip install vext
pip install vext.pygtk
lofidevops
  • 15,528
  • 14
  • 79
  • 119