Skip to content

[Bug]: error plotting: AttributeError: module 'gi' has no attribute 'require_version' #30654

@heinrich5991

Description

@heinrich5991

Bug summary

Plotting something raises AttributeError: module 'gi' has no attribute 'require_version' on Arch Linux when at-spi2-core is installed, but python-gobject is not.

Code for reproduction

import matplotlib.pyplot as plt
plt.plot([0, 1], [0, 1])

Actual outcome

Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    plt.plot([0, 1], [0, 1])
    ~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 3838, in plot
    return gca().plot(
           ~~~^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 2785, in gca
    return gcf().gca()
           ~~~^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 1108, in gcf
    return figure()
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 1042, in figure
    manager = new_figure_manager(
        num, figsize=figsize, dpi=dpi,
        facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
        FigureClass=FigureClass, **kwargs)
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 551, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 528, in _warn_if_gui_out_of_main_thread
    canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
                                                ~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 369, in _get_backend_mod
    switch_backend(rcParams._get("backend"))
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 411, in switch_backend
    switch_backend(candidate)
    ~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/lib/python3.13/site-packages/matplotlib/pyplot.py", line 425, in switch_backend
    module = backend_registry.load_backend_module(newbackend)
  File "/usr/lib/python3.13/site-packages/matplotlib/backends/registry.py", line 317, in load_backend_module
    return importlib.import_module(module_name)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/usr/lib/python3.13/site-packages/matplotlib/backends/backend_gtk4agg.py", line 4, in <module>
    from . import backend_agg, backend_gtk4
  File "/usr/lib/python3.13/site-packages/matplotlib/backends/backend_gtk4.py", line 19, in <module>
    gi.require_version("Gtk", "4.0")
    ^^^^^^^^^^^^^^^^^^
AttributeError: module 'gi' has no attribute 'require_version'

Expected outcome

No nondescript error, potentially a plot if a suitable backend is installed.

Other discussions observing this issue in matplotlib

AUTOMATIC1111/stable-diffusion-webui#10373
RVC-Boss/GPT-SoVITS#2619
cschwan/sage-on-gentoo#665
enkore/i3pystatus#314
msys2/MINGW-packages#19540
ravin-d-27/PyDeepFlow#40

Documentation that mentions this issue

https://wiki.archlinux.org/index.php?title=Redshift&oldid=844147#Redshift_does_not_appear_in_system_tray
https://github.com/volzotan/CompressorCam/blob/1ea06249a0d5de7708dda5c699beda26ea7582fe/software/compressor/README.md?plain=1#L32-L36
https://github.com/BenMiriello/Jarvis/blob/3d14e935035b28959e15db0c64809fed5bb4ca0d/Raspberry_Pi_Install_Instructions.md?plain=1#L88

Workarounds in software for this issue

https://github.com/ponty/pyscreenshot/blob/c9f6051fe339b4b4c59ef75aa17140211e51606f/tests/test_pygdk3.py#L10

Other software with the same bug (relying on python-gobject without having it installed)

jeffreywildman/homebrew-virt-manager#74
atlast/Postgres-GUI#4
bhrigu123/Instant-Lyrics#7
MyDuerOS/DuerOS-Python-Client#5
MyDuerOS/DuerOS-Python-Client#14
MyDuerOS/DuerOS-Python-Client#16
TheImagingSource/Linux-tiscamera-Programming-Samples#43
adrienverge/PhotoCollage#105
GabMus/gestureManager#2
voc/voctomix#341
rjw57/videosequence#2
calebstewart/gmusicclient#1 (comment)
C2N14/AutomaThemely#49
ivan-hc/GIMP-appimage#31
borisbabic/browser_cookie3#53
korcankaraokcu/PINCE@74d1c1f

Operating system

Arch Linux

Matplotlib Version

3.10.6

Matplotlib Backend

crashes with the same error as above

Python version

3.13.7

Jupyter version

n/a

Installation

Linux package manager

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions