34

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.

ferrangb
  • 2,012
  • 2
  • 20
  • 34
Nicholas Kolatsis
  • 427
  • 1
  • 5
  • 9

7 Answers7

41

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
lofidevops
  • 15,528
  • 14
  • 79
  • 119
  • 15
    after doing this, I still get `no module named gi` – bluppfisk Sep 04 '18 at 04:56
  • 2
    @bluppfisk: you still have to `sudo apt install python-gi`. As I understood, `vext` only gives _visibility_ to the `gi` system libraries – MestreLion Nov 05 '19 at 13:32
  • 5
    This is incorrect. On Ubuntu 18, in a Python3.7 virtualenv, I still get `ImportError: No module named gi`. – Cerin May 16 '20 at 23:14
16

Update 2023 – macOS

  1. Install GTK+ 3 and Gobject Introspection with Homebrew.

     brew install gtk+3 gobject-introspection
    
  2. Create a virtual environment.

     python3 -mvenv venv
    
  3. Install pygobject in the virtual environment (pycairo should come as a dependency).

     venv/bin/pip install pygobject
    

Tested on macOs Ventura 13.5, Python 3.11, Apple M1 Max chip.

Update 2018 – Debian Stretch

  1. Install GTK+ 3 / GIR.

     apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
    
  2. Create a virtual environment.

     python3 -mvenv venv
    
  3. Install pygobject (pycairo should come as a dependency).

     venv/bin/pip install pygobject
    

Update 2018 – macOS

  1. Install GTK+ 3 and Gobject Introspection with Homebrew.

     brew install gtk+3 gobject-introspection
    
  2. Create a virtual environment.

     python3 -mvenv venv
    
  3. Install pygobject in the virtual environment (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.

  1. Install GTK+ 3 with Homebrew.

     brew install gtk+3
    
  2. Create and activate a virtual environment.

     pyvenv-3.5 venv
     source venv/bin/activate
     cd venv
    
  3. 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 ..
    
  4. 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 ..
    
  5. 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.

Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
  • Thanks for this Rafa, going to try this today actually. – Malcolm Jones Sep 05 '16 at 01:08
  • Just tried this Rafa and this worked very well for me, one typo I saw. sed -i '' '154s/data={}/return/' .waf3-1.6.4-*/waflib/Build.py should be sed -i '154s/data={}/return/' .waf3-1.6.4-*/waflib/Build.py Everything else worked out perfectly, thank you! – Malcolm Jones Sep 05 '16 at 01:37
  • 1
    Hey Malcom, that is not a typo. The `-i` option with an empty string tells the standard `sed` that comes with OS X to perform an inline replace on the file without creating a backup. Maybe you are using the GNU implementation of `sed` instead? – Rafa Viotti Sep 06 '16 at 18:00
  • 1
    Shucks, that's exactly it, I have gnu sed installed ! Thanks again for this !! – Malcolm Jones Sep 08 '16 at 21:19
12

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).

Aidan Kane
  • 3,856
  • 2
  • 25
  • 28
6

The pip package name is somewhat counterintuitive - use pip install PyGObject.

rafalmp
  • 3,988
  • 3
  • 28
  • 34
1

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.

Paul Weaver
  • 284
  • 5
  • 9
0

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.

jmpcm
  • 1,814
  • 2
  • 19
  • 27
-1
sudo apt install build-dep python3-gi
pip install PyGObject
eri
  • 3,133
  • 1
  • 23
  • 35