41

So I am trying to run a simple matplotlib example in my virtualenv (in the console). Here's 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 pygtk to be installed.

And now the fun starts. I tried to pip install 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 have checked the INSTALL file and says to try ./configfure; make; make install. However. I am not quite sure how to do this within virtualenv. Where do I unpack the sources for pygtk in order to be installed within virtualenv.

jcollado
  • 39,419
  • 8
  • 102
  • 133
George Eracleous
  • 4,278
  • 6
  • 41
  • 50
  • Where you unpack shouldn't matter if you have the virtualenv active when you install it. – Thomas K Jan 30 '12 at 13:29
  • Ok, so when I tried to install pygtk with pip I got that msg I mentioned before. Then I went to the build folder and there was a pygtk folder. I did the steps explained in the INSTALL file and everything worked just fine. However, when I tried to run my code it complained about pygtk not being installed. I don't get it. What am I doing wrong? – George Eracleous Jan 30 '12 at 19:29
  • Why not just use a different backend? – ptomato Feb 01 '12 at 09:01
  • Ok why not? But which one is suitable for rendering on the screen? I know that Agg is intended for files. – George Eracleous Feb 01 '12 at 09:39

5 Answers5

18

The trick is to manually set the correct paths and then run configure inside the virtualenv. This is quite basic, but it worked for me.

Install python-config in the virtual env and link it to 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 the 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.0
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 could assist on adding the path to virtualenvwrapper?

AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
salomonvh
  • 1,739
  • 1
  • 15
  • 15
  • 2
    Now you have to build and install pygobject separately as well, apparently. I myself haven't managed to do this successfully. – Aryeh Leib Taurog Jul 24 '13 at 16:44
  • 1
    I was able to install it following the script steps at (https://gist.github.com/ches/1094799), but 'import matplotlib.pyplot' still thinks pygtk is not installed. – ninly Jan 05 '14 at 22:12
  • 1
    I have done all of this successfully, but I still get errors like "ImportError: Gtk* backend requires pygtk to be installed." – Mala Jan 15 '15 at 18:58
  • For whatever reason this does not seem to install the gtk module in the pythonpath. It installs to /home/PATH/TO/VIRT/usr/lib/python2.7/site-packages/gtk-2.0/gtk. Of course, gtk-2.0 is not a valid module name -- not sure why it installs that in site-packages. – Ben Davis Jun 21 '15 at 02:03
  • Could you expand on, or update, `ln -s /home/PATH/TO/VIRT/bin/python-config /home/PATH/TO/VIRT/bin/python2.7-config`, please? I'm sure everyone uses Python 3 now, and I guess the source is `venv/lib/python3.6/site-packages/setuptools/config.py`, but then, what is the correct target? – Michael Scheper Aug 19 '20 at 18:52
10

I did this

sudo apt-get install python-gtk2

I found that it was already installed upon some investigation, i found out that when I create a virtual environment, it was missing some links so I came across this post: Virtualenv on Ubuntu with no site-packages.

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

  1. First I changed into my virtualenv and activated it by

    source bin/activate
    
  2. Then I changed into the lib/python2.7 folder inside my virtualenv:

    cd lib/python2.7
    
  3. I then 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/
    
  4. Finally, to check I typed 'python', and executed:

    >>> import pygtk
    

    It gave me no error, and therefore I knew its now available in my virtual env.

I'm using Ubuntu 14.04 (64-bit) on an intel Core i5.

Community
  • 1
  • 1
Salar Khan
  • 457
  • 3
  • 13
  • 3
    This works and i think it's a relevant answer because it solves the underlying problem. I had to also do `ln -s /usr/lib/python2.7/dist-packages/gobject/`, `ln -s /usr/lib/python2.7/dist-packages/gobject/`, `ln -s /usr/lib/python2.7/dist-packages/glib ` in order to use `view` from `ase.visualize`. Though may be these packages can be installed the regular way. – Jonatan Öström Aug 22 '16 at 09:52
7

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 the tar files and compiling and installing those, but if it is OK to make links to the relevant packages installed in the system then activating your virtualenv and installing ruamel.venvgtk is enough:

pip install ruamel.venvgtk

This is a shameless plug for my own work, none of the other solutions here worked well with repeated virtualenv creation as is e.g. done by tox.

In the setup.py of the 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)

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

jpmorin
  • 6,008
  • 2
  • 28
  • 39
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • So I definitely just installed this and it seems to be working so far, going to give it the acid test now Anthon, and if it really helps with my dev work, ill def upvote! – Malcolm Jones Jan 20 '15 at 01:19
  • 1
    This seems to only work with system python that is /usr/bin/python but fails with any other python (i.e., /opt/local/bin/python) :( – Good Person Sep 24 '17 at 04:23
0

My experience (on Posix systems exclusively) has been that some packages cannot be installed in virtualenv (I think it's because they need to compile themselves, etc). Sometimes they can be installed in the individual package afterwards.

One way you could handle this situation is to compile and install the package somewhere else and then configure the virtualenv to load that package by adding site-packages paths. Check out the documentation for more. (or setup a boostrap script that changes the environment path every time you activate your environment (easy to do with virtualenvwrapper

Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
0

I fixed the problem by installing the python-gtk2 debian package.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Wael Ben Zid El Guebsi
  • 2,670
  • 1
  • 17
  • 15