在 virtualenv 中安装 PyGtk

发布于 2024-12-29 23:14:32 字数 998 浏览 5 评论 0 原文

所以我尝试在我的 virtualenv 中(在控制台中)运行一个简单的 matplotlib 示例。这是代码:

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()

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

导入错误:Gtk* 后端需要安装 pygtk。

现在有趣的事情开始了。我尝试 pip install pygtk 但它抛出:

********************************************************************
* 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;制作;进行安装。然而。我不太确定如何在 virtualenv 中执行此操作。我在哪里解压 pygtk 的源代码才能安装在 virtualenv 中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空心空情空意 2025-01-05 23:14:32

诀窍是手动设置正确的路径,然后在 virtualenv 中运行配置。这是非常基本的,但它对我有用。

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

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

在虚拟环境中安装 cairo:

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

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 的路径?

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?

冷清清 2025-01-05 23:14:32

我做了这个

sudo apt-get install python-gtk2

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

我阅读了它并定制了为我的设置提供的命令,如下所示:

  1. 首先,我更改为我的 virtualenv 并通过

    激活它

    源 bin/激活
    
  2. ,然后我更改为我的 virtualenv 中的 lib/python2.7 文件夹:

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

    $ 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. 最后,检查我输入了“python”并执行:

    <前><代码>>>>导入pygtk

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

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

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.

随波逐流 2025-01-05 23:14:32

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

pip install pygtk

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

pip install ruamel.venvgtk

这是我自己工作的无耻插件,这里没有其他解决方案与重复的 virtualenv 创建配合得很好,例如由 tox 完成的。

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

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- package),然后将链接设置为激活的 python 的第一个路径(非零长度)。

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.

飘然心甜 2025-01-05 23:14:32

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

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

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

一向肩并 2025-01-05 23:14:32

我通过安装 python-gtk2 debian 包解决了这个问题。

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

焚却相思 2025-01-05 23:14:32

我在支持 gtk 的 Linux 系统上使用 python 3.10。
没有一个解决方案适合我,而且我无法找到 gtk 的替代安装。
但我注意到在全局python环境中有这个库并且你可以使用它。正如您所了解的,您无法一次选择多个虚拟环境,但您想要测试许多其他库。

总的来说,这就是我的想法。创建了一个名为 venv 的本地虚拟 python shell。
它包含位于 venv/lib/python3.10/site-packages/ 的库。
在全局环境中,库存储在这里/usr/lib/python3/dist-packages/。
我需要的 gtk 全局环境中的目录称为“gi”。
接下来我做了一个符号链接。

$ ln -s /usr/lib/python3/dist-packages/gi/ venv/lib/python3.10/site-packages/

现在它在本地 venv 中看起来像这样:

gi -> /usr/lib/python3/dist-packages/gi/

奇怪的是,它有效。
也许这只是某种拐杖。

I'm using python 3.10 on a Linux system with gtk support.
Not a single solution worked for me, plus I couldn’t figure out the alternative installation of gtk.
But I noticed that in the global python environment there is this library and you can use it. And as you understand, you can’t select several virtual environments at once, but you want to test a lot of other libraries.

In general, this is what I came to. Created a local virtual python shell called venv.
It contains libraries at venv/lib/python3.10/site-packages/.
In the global environment, the library is stored here /usr/lib/python3/dist-packages/.
The directory in the gtk global environment that I needed is called "gi".
Next I made a symbolic link.

$ ln -s /usr/lib/python3/dist-packages/gi/ venv/lib/python3.10/site-packages/

Now it looks like this in the local venv:

gi -> /usr/lib/python3/dist-packages/gi/

Strangely enough, it works.
Perhaps this is just some kind of crutch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文