如何在virtualenv中安装python3 gi?

2024-05-15 13:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在跟踪Python GTK+ 3 Tutorial并尝试在virtualenv中运行一个有效的安装。我已经通过Ubuntu包管理器安装了python3-gi。事情是这样的:

:~$ mkvirtualenv py3 --python=/usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Installing setuptools, pip...python
done.
(py3):~$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
>>> 
(py3):~$ deactivate
:~$ /usr/bin/python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> 

如您所见,python3 gi显然在virtualenv中不可用,但我不确定如何安装它,因为python3gi是通过我的包管理器而不是pip安装的。


Tags: pipindefault管理器binvirtualenvonlinux
3条回答

更新2018–Debian Stretch

  1. 安装GTK+3/GIR。

    apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
    
  2. 创建虚拟环境。

    python3 -mvenv venv
    
  3. 安装pygobjectpycairo应作为依赖项)。

    venv/bin/pip install pygobject
    

更新2018–macOS

  1. Homebrew安装GTK+3和Gobject内省。

    brew install gtk+3 gobject-introspection
    
  2. 创建并激活虚拟环境。

    python3 -mvenv venv
    
  3. 安装pygobjectpycairo应作为依赖项)。

    PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig ARCHFLAGS="-arch x86_64" venv/bin/pip install pygobject
    

原始答案

这就是我在OSX10.11上的Python3.5虚拟环境中获得GTK+3的方法。

  1. Homebrew安装GTK+3。

    brew install gtk+3
    
  2. 创建并激活虚拟环境。

    pyvenv-3.5 venv
    source venv/bin/activate
    cd venv
    
  3. 在虚拟环境中安装pycairo

    export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig
    
    curl -L https://cairographics.org/releases/pycairo-1.10.0.tar.bz2 | tar xj
    cd pycairo-1.10.0
    export ARCHFLAGS='-arch x86_64'
    
    python waf configure --prefix=$VIRTUAL_ENV # It's ok, this will fail.
    sed -i '' '154s/data={}/return/' .waf3-1.6.4-*/waflib/Build.py # Bugfix: https://bugs.freedesktop.org/show_bug.cgi?id=76759
    python waf configure --prefix=$VIRTUAL_ENV # Now it should configure.
    python waf build
    python waf install
    
    unset ARCHFLAGS
    cd ..
    
  4. 在虚拟环境中安装pygobject

    export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
    
    curl -L http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.12/pygobject-3.12.2.tar.xz | tar xJ
    cd pygobject-3.12.2
    
    ./configure CFLAGS="-I$VIRTUAL_ENV/include" --prefix=$VIRTUAL_ENV
    make
    make install
    
    cd ..
    
  5. 利润。

    Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from gi.repository import Gtk, Gdk, Pango, GObject
    >>> from cairo import ImageSurface, Context, FORMAT_ARGB32
    >>>
    

Python 3.5从PSF下载并安装。

我还没有找到合适的解决办法。当我遇到无法直接安装到virtualenv中的情况时,我会在那里对其进行符号链接,它运行良好(可能有例外,但这不是其中之一)。

ln -s /usr/lib/python3/dist-packages/gi /path_to_venv/lib/python3.4/site-packages/

一点也不优雅;似乎比让virtualenv完全访问所有系统包更好(通过--system-site-packages)。

现在可以使用vext解决此问题。Vext允许您在virtualenv中安装单独访问系统包的软件包。要访问gi,请执行以下操作:

pip install vext
pip install vext.gi

相关问题 更多 >