Python 3: Installing gi package with pip
The chosen answer is a little bit outdated as of now:
- Last update of pgi in 2018
- Since 2018, PyGObject is available for python3 from PyPI.
Installing the relevant package, plus its dependencies, depending on your environment, is still possible, just follow the steps from the doc.
But if you work from a venv, you might still stumble upon a ModuleNotFoundError: No module named 'gi'
when importing gi
. The venv ignores the system-wide module. Then simply run, from your venv: pip install PyGObject
(or use poetry
or your favorite python packages manager). Of course, you still need to have installed PyGObject's dependencies (see the link to the doc, right above).
First, pip install gi
will install another unrelated package, the correct
name is pgi
. But after running:
$ pip uninstall gi
$ pip install pgi
$ python toolmanager.py
[...]
Traceback (most recent call last):
File "toolmanager.py", line 14, in <module>
import matplotlib.pyplot as plt
File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3cairo.py", line 6, in <module>
from . import backend_gtk3
File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 12, in <module>
raise ImportError("Gtk3 backend requires pygobject to be installed.")
ImportError: Gtk3 backend requires pygobject to be installed.
It seems that pygobject
for Python 3 cannot be installed from PyPI. So I tried to install everything from the Ubuntu distribution package python3-gi
instead:
$ sudo apt-get install python3-gi
$ pyenv local system
$ python3 --version
Python 3.5.3
$ python3 toolmanager.py
and this works fine :)
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<version>
.
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
>>>