matplotlib ImportError:基于gtk的后端需要cairo|没有名为“gi._gi_cairo”的模块

suzh9iv8  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(126)

原始问题

在Ubuntu 22.04上,我在这个MWE中得到以下错误:

import matplotlib as mpl
from matplotlib import pyplot as plt

mpl.use("GTK4Agg")

字符串
完整的追踪是

niko@niko-ubuntu:~/myproj$ python script.py 
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 176, in require_foreign
    _gi.require_foreign(namespace, symbol)
ModuleNotFoundError: No module named 'gi._gi_cairo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/backends/_backend_gtk.py", line 23, in <module>
    gi.require_foreign("cairo")
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 178, in require_foreign
    raise ImportError(str(e))
ImportError: No module named 'gi._gi_cairo'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/niko/myproj/script.py", line 4, in <module>
    mpl.use("GTK4Agg")
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/__init__.py", line 1233, in use
    plt.switch_backend(name)
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/pyplot.py", line 271, in switch_backend
    backend_mod = importlib.import_module(
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/backends/backend_gtk4agg.py", line 4, in <module>
    from . import backend_agg, backend_gtk4
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/backends/backend_gtk4.py", line 26, in <module>
    from . import _backend_gtk
  File "/home/niko/.local/lib/python3.10/site-packages/matplotlib/backends/_backend_gtk.py", line 25, in <module>
    raise ImportError("Gtk-based backends require cairo") from e
ImportError: Gtk-based backends require cairo

检查cairo

只要阅读到回溯的最后一行,要做的第一件事就是尝试安装pycairo

niko@niko-ubuntu:~/myproj$ python -m pip install pycairo
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pycairo in /usr/lib/python3/dist-packages (1.20.1)


我还可以验证cairo是否可以导入:

niko@niko-ubuntu:~/myproj$ ipython
>>> import cairo
>>> cairo.__file__
'/usr/lib/python3/dist-packages/cairo/__init__.py'

没有名为'gi._gi_cairo'的模块

接下来要做的就是阅读完整的回溯,我做到了,并意识到我应该尝试require_foreign("cairo"),我做到了:

>>> import gi
>>> gi.require_foreign("cairo")
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File /usr/lib/python3/dist-packages/gi/__init__.py:176, in require_foreign(namespace, symbol)
    175 try:
--> 176     _gi.require_foreign(namespace, symbol)
    177 except Exception as e:

ModuleNotFoundError: No module named 'gi._gi_cairo'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[4], line 1
----> 1 gi.require_foreign("cairo")

File /usr/lib/python3/dist-packages/gi/__init__.py:178, in require_foreign(namespace, symbol)
    176     _gi.require_foreign(namespace, symbol)
    177 except Exception as e:
--> 178     raise ImportError(str(e))
    179 importlib.import_module('gi.repository', namespace)

ImportError: No module named 'gi._gi_cairo'

Matplotlib docs & google

在谷歌上搜索这个确切的短语并没有导致任何搜索结果,因此这个问题(以帮助未来的谷歌):我应该安装什么才能让matplotlib与GTK4Agg后端一起工作?matplotlib documentation说:
需要PyGObjectpycairo

提问

由于我可以同时导入gipycairo,因此可以假定它们都已安装。缺少了一些东西,因为gi._gi_cairo不存在。为什么会这样,如何解决?

dxpyg8gm

dxpyg8gm1#

显然,安装PyGObject、pycairo和cairo而不安装GObject库的cairo绑定是可能的。这些都包含在python3-gi-cairo包中,应该在安装PyGObject时安装,如PyGObject documentation中所述。
因此,修复方法是使用

sudo apt install python3-gi-cairo

字符串

相关问题