如何在virtualenv中安装python3-gi?
我正在跟着Python GTK+ 3 教程,想在虚拟环境中安装一个可以正常运行的环境。我已经通过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在虚拟环境中显然是不可用的,但我不太确定该怎么安装它,因为python3-gi是通过包管理器安装的,而不是用pip安装的。
7 个回答
我通过 pip
安装了 pgi
,这可能是一个不错的选择。它显然和 PyGObject
兼容,至今为止在运行 Gtk
时似乎一切正常。
这个 pip 包的名字有点让人摸不着头脑,你可以用 pip install PyGObject
来安装它。
我还没找到一个合适的解决办法。当我遇到一些东西无法直接安装到虚拟环境(virtualenv)里的情况时,我会用符号链接(symlink)把它放进去,这样就能正常工作了(可能有些例外,但这不是其中之一)。
ln -s /usr/lib/python3/dist-packages/gi /path_to_venv/lib/python3.4/site-packages/
这并不是一个优雅的做法;不过比起让虚拟环境完全访问所有系统包(通过 --system-site-packages
),这样似乎要好一些。
更新 2023 – macOS
通过Homebrew安装 GTK+ 3 和 Gobject Introspection。
brew install gtk+3 gobject-introspection
创建一个虚拟环境。
python3 -mvenv venv
在虚拟环境中安装
pygobject
(pycairo
会作为依赖自动安装)。venv/bin/pip install pygobject
在 macOS Ventura 13.5、Python 3.11 和 Apple M1 Max 芯片上测试通过。
更新 2018 – Debian Stretch
安装 GTK+ 3 / GIR。
apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
创建一个虚拟环境。
python3 -mvenv venv
安装
pygobject
(pycairo
会作为依赖自动安装)。venv/bin/pip install pygobject
更新 2018 – macOS
通过Homebrew安装 GTK+ 3 和 Gobject Introspection。
brew install gtk+3 gobject-introspection
创建一个虚拟环境。
python3 -mvenv venv
在虚拟环境中安装
pygobject
(pycairo
会作为依赖自动安装)。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 的步骤。
通过Homebrew安装 GTK+ 3。
brew install gtk+3
创建并激活一个虚拟环境。
pyvenv-3.5 venv source venv/bin/activate cd venv
在虚拟环境中安装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 ..
在虚拟环境中安装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 ..
完成。
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下载并安装的。
现在可以通过 vext 来解决这个问题。vext 让你可以在一个虚拟环境中安装包,这些包可以单独访问你系统里的其他包。要访问 gi
,你可以按照下面的步骤操作:
pip install vext
pip install vext.gi