Install PyGtk in virtualenv

So, I'm trying to run a simple matplotlib example in my virtualenv (in the console). Here is the code:

import matplotlib matplotlib.use('GTKAgg') import matplotlib.pyplot as plt radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724] plt.plot(radius, area) plt.show() 

However, when I run this, I get:

ImportError: Gtk * backend requires the installation of pygtk.

And now the fun begins. I tried installing pygtk, but it throws:

 ******************************************************************** * Building PyGTK using distutils is only supported on windows. * * To build PyGTK in a supported way, read the INSTALL file. * ******************************************************************** Complete output from command python setup.py egg_info: ******************************************************************** 

I checked the INSTALL file and tried ./configfure; make; make install ./configfure; make; make install ./configfure; make; make install . However. I'm not quite sure how to do this in virtualenv. Where can I unzip sources for pygtk for installation in virtualenv.

+35
python matplotlib virtualenv pygtk
Jan 30 '12 at 13:01
source share
5 answers

The trick is to manually set the correct paths and then run configure inside virtualenv. It is pretty simple, but it worked for me.

Install python-config in virtual env and associate it with python2.7-config:

 pip install config ln -s /home/PATH/TO/VIRT/bin/python-config /home/PATH/TO/VIRT/bin/python2.7-config 

Install cairo in virtual env:

 wget http://cairographics.org/releases/py2cairo-1.10.0.tar.bz2 tar -xf py2cairo-1.10.0.tar.bz2 cd py2cairo-1.10.0 ./waf configure --prefix=/home/PATH/TO/VIRT/ ./waf build ./waf install 

Install PyGTK

 wget http://pypi.python.org/packages/source/P/PyGTK/pygtk-2.24.0.tar.bz2 tar -xf pygtk-2.24.0.tar.bz2 cd pygtk-2.24 export PKG_CONFIG_PATH=/home/PATH/TO/VIRT/lib/pkgconfig ./configure --prefix=/home/PATH/TO/VIRT/ make make install 

And that should do it. Just replace PATH / TO / VIRT / with your own path. I'm sure someone can help add a path to virtualenvwrapper?

+15
Jun 15 '13 at 17:22
source share
β€” -

I did it

sudo apt-get install python-gtk2

I found that it was already installed during some kind of investigation, I found out that when I create the virtual environment, I lost some links, so I came across this message: Virtualenv on Ubuntu without site packages .

I read it and executed the commands provided to my setup as follows:

  • First I went to my virtualenv and activated it

     source bin/activate 
  • Then I went to the lib / python2.7 folder inside my virtualenv:

     cd lib/python2.7 
  • Then I executed the following commands.

     $ ln -s /usr/lib/python2.7/dist-packages/cairo/ $ ln -s /usr/lib/python2.7/dist-packages/pygtk.py $ ln -s /usr/lib/python2.7/dist-packages/pygtk.pth $ ln -s /usr/lib/python2.7/dist-packages/gtk-2.0/ 
  • Finally, to verify that I typed "python" and executed:

     >>> import pygtk 

    This did not give me any errors, and therefore I knew that it was now available in my virtual env.

I am using Ubuntu 14.04 (64-bit) on Intel Core i5.

+7
Feb 21 '16 at 18:51
source share

pygtk cannot be installed in your virtualenv from PyPI, so

 pip install pygtk 

will download but not install. You can go through the hoops of downloading tar files and compiling and installing them, but if it’s ok to link to the appropriate packages installed on the system, activate your virtualenv and install ruamel.venvgtk enough:

 pip install ruamel.venvgtk 

This is a shameless plugin for my own work, none of the other solutions here worked well with the re-creation of a virtual virtual disk, such as the one made by tox .

In setup.py packages, the following happens:

 try: import gtk except ImportError: print('--------------') import subprocess instdir = subprocess.check_output([ '/usr/bin/python', '-c', 'import os, pygtk; print os.path.dirname(pygtk.__file__)', ]).strip() for dst_base in sys.path: if dst_base.strip(): break for d in [ 'pygtk.pth', 'pygtk.py', 'gtk-2.0', 'gobject', 'glib', 'cairo', ]: src = os.path.join(instdir, d) dst = os.path.join(dst_base, d) if os.path.exists(src) and not os.path.exists(dst): print('linking', d, 'to', dst_base) os.symlink(src, dst) 

that is, the python system is asked where pygtk is installed (on Linux Mint 17.1 it is /usr/lib/python2.7/dist-packages ), and then the links are configured on the first path (i.e. non-zero length) for the activated python.

+6
Dec 14 '14 at 16:29
source share

My experience (only on Posix systems) was that some packages cannot be installed in virtualenv (I think this is because they have to compile themselves, etc.). Sometimes they can be installed in a separate package later.

One way to deal with this situation is to compile and install the package elsewhere, and then configure virtualenv to download the package by adding the paths of the site packages. See more details. (or set up a boostrap script that changes the environment path every time you activate your environment (easy to do with virtualenvwrapper

0
Apr 09 2018-12-12T00:
source share

I have the same problem, but I fixed it by installing the debian python-gtk2 package

0
Jun 11 '14 at 19:23
source share



All Articles