How do I install python3-gi within virtualenv?
Question:
I’m following the Python GTK+ 3 Tutorial and I’m trying to get a working install running in virtualenv. I have python3-gi installed through the Ubuntu package manager already. Things look like this:
:~$ 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
>>>
As you can see, python3-gi is obviously not available within virtualenv but I am not sure how to install it since python3-gi is installed through my package manager and not with pip.
Answers:
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
).
I installed pgi
via pip
, which may be an option. It is apparently API compatible with PyGObject
and so far seems to work ok running Gtk
.
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 venv
source venv/bin/activate
cd venv
-
Install pycairo on the virtual environment.
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 ..
-
Install pygobject on the virtual environment.
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 ..
-
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 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 downloaded and installed from PSF.
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 vext
pip install vext.gi
The pip package name is somewhat counterintuitive – use pip install PyGObject
.
On Ubuntu (tested in 19.04), you can download the package and then install it as follows:
apt-get -y download python3-gi
dpkg-deb -x <package>.deb <virtualenv path>
In Ubuntu, the name of the downloaded package is python3-gi_3.32.0-1_amd64.deb
.
sudo apt install build-dep python3-gi
pip install PyGObject
I’m following the Python GTK+ 3 Tutorial and I’m trying to get a working install running in virtualenv. I have python3-gi installed through the Ubuntu package manager already. Things look like this:
:~$ 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
>>>
As you can see, python3-gi is obviously not available within virtualenv but I am not sure how to install it since python3-gi is installed through my package manager and not with pip.
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
).
I installed pgi
via pip
, which may be an option. It is apparently API compatible with PyGObject
and so far seems to work ok running Gtk
.
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 venv source venv/bin/activate cd venv
-
Install pycairo on the virtual environment.
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 ..
-
Install pygobject on the virtual environment.
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 ..
-
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 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 downloaded and installed from PSF.
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 vext
pip install vext.gi
The pip package name is somewhat counterintuitive – use pip install PyGObject
.
On Ubuntu (tested in 19.04), you can download the package and then install it as follows:
apt-get -y download python3-gi
dpkg-deb -x <package>.deb <virtualenv path>
In Ubuntu, the name of the downloaded package is python3-gi_3.32.0-1_amd64.deb
.
sudo apt install build-dep python3-gi
pip install PyGObject