Installing PyGtk in virtualenv

半世苍凉 提交于 2019-11-28 06:44:56
salomonvh

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?

Lenny Linus

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.

Anthon

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!