How do I install python3-gi within virtualenv?
It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access gi
, do the following:
pip install vextpip install vext.gi
Update 2018 – Debian Stretch
Install GTK+ 3 / GIR.
apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
Create a virtual environment.
python3 -mvenv venv
Install
pygobject
(pycairo
should come as a dependency).venv/bin/pip install pygobject
Update 2018 – macOS
Install GTK+ 3 and Gobject Introspection with Homebrew.
brew install gtk+3 gobject-introspection
Create and activate a virtual environment.
python3 -mvenv venv
Install
pygobject
(pycairo
should come as a dependency).PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig ARCHFLAGS="-arch x86_64" venv/bin/pip install pygobject
Original answer
This is what I did to get GTK+ 3 within a Python 3.5 virtual environment on OS X 10.11.
Install GTK+ 3 with Homebrew.
brew install gtk+3
Create and activate a virtual environment.
pyvenv-3.5 venvsource venv/bin/activatecd venv
Install pycairo on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfigcurl -L https://cairographics.org/releases/pycairo-1.10.0.tar.bz2 | tar xjcd pycairo-1.10.0export 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=76759python waf configure --prefix=$VIRTUAL_ENV # Now it should configure.python waf buildpython waf installunset ARCHFLAGScd ..
Install pygobject on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfigcurl -L http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.12/pygobject-3.12.2.tar.xz | tar xJcd pygobject-3.12.2./configure CFLAGS="-I$VIRTUAL_ENV/include" --prefix=$VIRTUAL_ENVmakemake installcd ..
Profit.
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 darwinType "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 downloaded and installed from PSF.
I haven't found a proper solution to this. When I run into situations where I can't get something to install into a virtualenv directly, I symlink it there and it works fine (there are probably exceptions, but this is not one of them).
ln -s /usr/lib/python3/dist-packages/gi /path_to_venv/lib/python3.4/site-packages/
Not elegant in the slightest; seems nicer than giving the virtualenv full access to all system packages though (via --system-site-packages
).