7

To add gtk-2.0 to my virtualenv I did the following:

$ virtualenv --no-site-packages --python=/usr/bin/python2.6 myvirtualenv
$ cd myvirtualenv
$ source bin/activate
$ cd lib/python2.6/
$ ln -s /usr/lib/pymodules/python2.6/gtk-2.0/ 

Virtualenv on Ubuntu with no site-packages

Now in the Python interpreter when I do import gtk it says: No module named gtk. When I start the interpreter with sudo it works.

Any reason why I need to use sudo and is there a way to prevent it?

Update:
Forgot to mention that cairo and pygtk work but it's not the one I need.

Update2:
Here the directory to show that I ain't crazy. http://www.friendly-stranger.com/pictures/symlink.jpg

Community
  • 1
  • 1
Pickels
  • 33,902
  • 26
  • 118
  • 178

5 Answers5

10

sudo python imports it just fine because that interpreter isn't using your virtual environment. So don't do that.

You only linked in one of the necessary items. Do the others mentioned in the answer to the question you linked as well.

(The pygtk.pth file is of particular importance, since it tells python to actually put that directory you linked onto the python path)

Update

Put that stuff in $VIRTUALENV/lib/python2.6/site-packages/ rather than the directory above that.

Looks like the .pth files aren't read from that directory - just from site-packages

kenm
  • 23,127
  • 2
  • 43
  • 62
6

This works for me (Ubuntu 11.10):

once you activate your virtualenv directory make sure 'dist-packages' exists:

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

Then, make links:

For GTK2:

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

For GTK3:

ln -s /usr/lib/python2.7/dist-packages/gi lib/python2.7/dist-packages/
t00m
  • 61
  • 1
  • 1
1

Remember to add a link to pygtk.py

ln -s /usr/lib/python2.7/dist-packages/pygtk.py lib/python2.7/dist-packages/
salomonvh
  • 1,739
  • 1
  • 15
  • 15
0

On Debian based Linux systems (Ubuntu, Mint) you can just install the ruamel.venvgtk package I put on PyPI. It will create the relevant links in your virtualenv during installation (if they are not yet there).

A more detailed explanation can be found in this answer

Community
  • 1
  • 1
Anthon
  • 69,918
  • 32
  • 186
  • 246
0

If it is not a requirement, that Python system packages are not used in the virtual environment, I would install apt install python-gtk2 (Ubuntu) and then create the virtual environment with:

virtualenv --system-site-packages .

That way, you do not pollute the system environment with your pip installations in the virtual environment, but reuse everything from the system. Especially pygtk.

thet
  • 697
  • 12
  • 14