如何在 virtualenv 中安装 python3-gi?

新手上路,请多包涵

我正在按照 Python GTK+ 3 教程 进行操作,并且正在尝试在 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 中不可用,但我不确定如何安装它,因为 python3-gi 是通过我的包管理器而不是 pip 安装的。

原文由 Nicholas Kolatsis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

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

 pip install vext
pip install vext.gi

原文由 lofidevops 发布,翻译遵循 CC BY-SA 3.0 许可协议

2018 年更新 – Debian Stretch

  1. 安装 GTK+ 3/GIR。
    apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0

  1. 创建一个虚拟环境。
    python3 -mvenv venv

  1. 安装 pygobjectpycairo 应该作为依赖项出现)。
    venv/bin/pip install pygobject

2018 年更新 – macOS

  1. 使用 Homebrew 安装 GTK+ 3 和 Gobject Introspection。
    brew install gtk+3 gobject-introspection

  1. 创建并激活虚拟环境。
    python3 -mvenv venv

  1. 安装 pygobjectpycairo 应该作为依赖项出现)。
    PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig ARCHFLAGS="-arch x86_64" venv/bin/pip install pygobject

原答案

这就是我在 OS X 10.11 上的 Python 3.5 虚拟环境中获得 GTK+ 3 所做的。

  1. 使用 Homebrew 安装 GTK+ 3。
    brew install gtk+3

  1. 创建并激活虚拟环境。
    pyvenv-3.5 venv
   source venv/bin/activate
   cd venv

  1. 在虚拟环境中安装 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 ..

  1. 在虚拟环境中安装 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 ..

  1. 利润。
    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
   >>>

PSF 下载并安装 Python 3.5。

原文由 Rafa Viotti 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题