How do I install python3-gi within virtualenv?

24,361

Solution 1

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

Solution 2

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 and activate a virtual environment.

    python3 -mvenv venv
    
  3. 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.

  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.

Solution 3

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

Solution 4

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

Solution 5

I installed pgi via pip, which may be an option. It is apparently API compatibly with PyGObject and so far seems to work ok running Gtk.

Share:
24,361
Nicholas Kolatsis
Author by

Nicholas Kolatsis

Updated on July 11, 2020

Comments

  • Nicholas Kolatsis
    Nicholas Kolatsis almost 4 years

    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.