21

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 --version
Python 3.6.1
$ pip --version
pip 9.0.1 from /home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages (python 3.6)
$ pip install gi
Collecting gi
  Downloading gi-1.2.tar.gz
Collecting requests (from gi)
  Downloading requests-2.16.0-py2.py3-none-any.whl (85kB)
    100% |████████████████████████████████| 92kB 959kB/s 
Collecting idna<2.6,>=2.5 (from requests->gi)
  Downloading idna-2.5-py2.py3-none-any.whl (55kB)
    100% |████████████████████████████████| 61kB 1.2MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests->gi)
  Downloading chardet-3.0.3-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 1.8MB/s 
Collecting urllib3<1.22,>=1.21.1 (from requests->gi)
  Downloading urllib3-1.21.1-py2.py3-none-any.whl (131kB)
    100% |████████████████████████████████| 133kB 1.8MB/s 
Collecting certifi>=2017.4.17 (from requests->gi)
  Downloading certifi-2017.4.17-py2.py3-none-any.whl (375kB)
    100% |████████████████████████████████| 378kB 284kB/s 
Installing collected packages: idna, chardet, urllib3, certifi, requests, gi
  Running setup.py install for gi ... done
Successfully installed certifi-2017.4.17 chardet-3.0.3 gi-1.2 idna-2.5 requests-2.16.0 urllib3-1.21.1

Now, running the example:

$ python toolmanager.py 
Traceback (most recent call last):
  File "./toolmanager.py", line 8, 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 10, in <module>
    import gi
  File "/home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages/gi/__init__.py", line 39
    print url
            ^
SyntaxError: Missing parentheses in call to 'print'

Seems like pip somehow installed a Python 2 version? How can I fix this?

4 Answers 4

14

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

2
  • 6
    How did you know that the correct package is pgi?
    – randy
    Apr 20, 2021 at 23:26
  • Doesn't seem to work (anymore): python3: can't open file 'toolmanager.py'
    – jaques-sam
    Aug 22 at 9:31
12

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

5

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
>>> 
0

I was getting the same problem with an gradio app. The solution

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Traceback (most recent call last):
  File "/home/pi/env/lib/python3.9/site-packages/gradio/routes.py", line 508, in predict
    output = await route_utils.call_process_api(
  File "/home/pi/env/lib/python3.9/site-packages/gradio/route_utils.py", line 218, in call_process_api
    with utils.MatplotlibBackendMananger():
  File "/home/pi/env/lib/python3.9/site-packages/gradio/utils.py", line 817, in __enter__
    self._original_backend = matplotlib.get_backend()
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/__init__.py", line 1275, in get_backend
    return rcParams['backend']
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/__init__.py", line 760, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/pyplot.py", line 327, in switch_backend
    switch_backend(candidate)
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/pyplot.py", line 342, in switch_backend
    module = importlib.import_module(cbook._backend_module_name(newbackend))
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/backends/backend_gtk4agg.py", line 4, in <module>
    from . import backend_agg, backend_gtk4
  File "/home/pi/env/lib/python3.9/site-packages/matplotlib/backends/backend_gtk4.py", line 12, in <module>
    import gi
  File "/home/pi/env/lib/python3.9/site-packages/gi/__init__.py", line 39
            print url
                  ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(url)?

pip uninstall gi pip3 install pgi

worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.