关于python:在virtualenv中安装PyGtk

Installing PyGtk in virtualenv

因此,我试图在我的virtualenv(在控制台中)中运行一个简单的matplotlib示例。这是代码:

1
2
3
4
5
6
7
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()

但是,当我运行它时,我得到:

ImportError: Gtk* backend requires pygtk to be installed.

现在,乐趣开始了。我试图通过pip安装pygtk,但是会抛出:

1
2
3
4
5
6
********************************************************************
* 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:
********************************************************************

我已经检查了INSTALL文件,并说尝试./configfure; make; make install。然而。我不太确定如何在virtualenv中执行此操作。为了在virtualenv中安装pygtk,我在哪里解压缩源代码。


诀窍是手动设置正确的路径,然后在virtualenv内部运行configure。这是很基本的,但是对我有用。

在虚拟环境中安装python-config并将其链接到python2.7-config:

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

在虚拟环境中安装cairo:

1
2
3
4
5
6
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

安装PyGTK

1
2
3
4
5
6
7
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

那应该做到。只需将PATH / TO / VIRT /替换为您自己的路径即可。我确定有人可以协助将路径添加到virtualenvwrapper吗?


我做到了

sudo apt-get install python-gtk2

我发现经过调查后已经安装了它,我发现当我创建虚拟环境时,它缺少一些链接,因此我遇到了这篇文章:
Ubuntu上的Virtualenv,没有站点包。

我阅读了该文件,并按如下方式调整了提供给我的设置的命令:

  • 首先,我更改为virtualenv并通过

    激活了它

    1
    source bin/activate
  • 然后我进入我的virtualenv内部的lib / python2.7文件夹:

    1
    cd lib/python2.7
  • 然后我执行了以下命令。

    1
    2
    3
    4
    $ 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/

  • 最后,要检查我是否键入了" python"并执行:

    1
    >>> import pygtk

    它没有给我任何错误,因此我知道它现在可以在我的虚拟环境中使用。

  • 我在Intel Core i5上使用Ubuntu 14.04(64位)。


    pygtk无法从PyPI安装在您的virtualenv中,因此

    1
    pip install pygtk

    将下载但不会安装。您可以下载tar文件并编译和安装这些文件,但是如果可以链接到系统中安装的相关软件包,则可以激活virtualenv并安装ruamel.venvgtk就足够了:

    1
    pip install ruamel.venvgtk

    这是我工作的无耻插件,此处的其他解决方案都不能像重复的virtualenv创建那样很好地工作。由tox

    完成

    在软件包的setup.py中发生以下情况:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    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)

    即询问系统的python pygtk的安装位置(在Linux Mint 17.1上为/usr/lib/python2.7/dist-packages),然后将链接设置为激活的python的第一个路径(非零长度)。 >


    我通过安装python-gtk2 debian软件包解决了此问题。


    根据我的经验(仅在Posix系统上),有些软件包无法安装在virtualenv中(我认为是因为它们需要自行编译等)。有时,以后可以将它们安装在单独的包装中。

    处理这种情况的一种方法是在其他地方编译并安装软件包,然后配置virtualenv以通过添加站点软件包路径来加载该软件包。查看文档以获取更多信息。 (或设置每次启动环境时都会更改环境路径的boostrap脚本(使用virtualenvwrapper