Skip to content

Instantly share code, notes, and snippets.

@gideao
Last active March 30, 2019 03:10
Show Gist options
  • Save gideao/c49fc630242fb78ff3cafd3e51682081 to your computer and use it in GitHub Desktop.
Save gideao/c49fc630242fb78ff3cafd3e51682081 to your computer and use it in GitHub Desktop.
How to install and compile PyGObject to use GTK+ 3 bindings on Python 3 and pyenv.

I'm using Arch Linux and in the current date I selected the most updated version of these libraries. If use system with stable but old packages you may encounter some problems.

I listed what was needed be installed on my system. I alredy have gcc and make and stuff like that installed. I recomend you to check out the documentation of each libary and see the build dependencies.

sudo pacman -S autoconf-archive gtk3 cairo

You will need install python 3 through pyenv and select it to use.

pyenv install 3.6.4
pyenv global 3.6.4

During the compilation the location of installation is define by the prefix param and the location of already installed libraries is set through PKG_CONFIG_PATH env variable.

export PYENV_PREFIX="$(pyenv prefix)"
export PKG_CONFIG_PATH=$PYENV_PREFIX/lib/pkgconfig

Clone and compile PyCairo

git clone https://github.com/pygobject/pycairo
cd pycairo
git checkout tags/v1.16.1
python3 setup.py build
python3 setup.py install --prefix=$PYENV_PREFIX
cd ..

Clone and compile GObject Introspection

git clone https://github.com/GNOME/gobject-introspection
cd gobject-introspection
git checkout tags/1.54.1
./autogen.sh --prefix=$PYENV_PREFIX
make
make install
cd ..

Clone and compile PyGObject

git clone https://github.com/GNOME/pygobject
cd pygobject
git checkout tags/3.26.1
python3 setup.py build
./autogen.sh --with-python=python3 --prefix=$PYENV_PREFIX
make
make install
cd ..

Run the hello.py exemple to check your installation.

python3 hello.py

If your try run hello.py and get errors like this:

Traceback (most recent call last):
  File "hello.py", line 2, in <module>
    gi.require_version("Gtk", "3.0")
  File "/home/gideao/.pyenv/versions/3.6.4/lib/python3.6/site-packages/gi/__init__.py", line 130, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available

This error is coused becouse GObject Introspection can't find Typelib files in your system. Typelib files are used by GObject Introspection as map of C libraries like GTK+ 3 and to do python bindings works.

The solution that I find was find where these files are and link it to directly to python's installation folder.

find / -name 'Gtk-3.0.typelib' 2>&1 | grep -v "Permission denied"

In my case Typelib files was located in /usr/lib/girepository-1.0. So I linked to it.

ln -sn /usr/lib/girepository-1.0 $PYENV_PREFIX/lib
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.show()
window.connect("destroy", Gtk.main_quit)
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment