From a58348870a88b4f866e0e2d258168fc533f9260c Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 4 Dec 2018 15:00:40 -0500 Subject: [PATCH] FIX: skip gtk backend if gobject but not pygtk is installed It is possible to install gobject (which provides `gi`) but not pygtk (which provide the `GTK` bindings). This try-except converts the ValueError that gi raises to an ImportError so our machinery works correctly. --- lib/matplotlib/backends/_gtk3_compat.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/_gtk3_compat.py b/lib/matplotlib/backends/_gtk3_compat.py index e0ac33c8d343..94971a6cefd8 100644 --- a/lib/matplotlib/backends/_gtk3_compat.py +++ b/lib/matplotlib/backends/_gtk3_compat.py @@ -40,7 +40,15 @@ if gi.__name__ == "pgi" and gi.version_info < (0, 0, 11, 2): raise ImportError("The GTK3 backends are incompatible with pgi<0.0.11.2") -gi.require_version("Gtk", "3.0") +try: + # :raises ValueError: If module/version is already loaded, already + # required, or unavailable. + gi.require_version("Gtk", "3.0") +except ValueError as e: + # in this case we want to re-raise as ImportError so the + # auto-backend selection logic correctly skips. + raise ImportError from e + globals().update( {name: importlib.import_module("{}.repository.{}".format(gi.__name__, name))