I am trying to run this Matplotlib example using Python 3. To run this I needed to install gi first (I am using pyenv):
$ python --
To install for the standard python, Håkon Hægland answer is the best choice.
But for an alternate python version,
one can use pip.
Beware that the alternate pip has to be used to match the alternate python.
The full explanations are given in the documentation.
For instance on openSUSE (standard python version 3.6, alternate installed 3.8):
> sudo zypper install cairo-devel pkg-config python3-devel gcc gobject-introspection-devel
> pip3.8 install --user pycairo
> pip3.8 install --user PyGObject
> python3.8
Python 3.8.1 (default, Feb 1 2020, 14:50:41)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>