diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py index 28650dc9ff83..e76ff67e1cef 100644 --- a/lib/matplotlib/backends/qt_compat.py +++ b/lib/matplotlib/backends/qt_compat.py @@ -133,7 +133,8 @@ def _isdeleted(obj): else: raise ImportError( "Failed to import any of the following Qt binding modules: {}" - .format(", ".join(_ETS.values()))) + .format(", ".join([QT_API for _, QT_API in _candidates])) + ) else: # We should not get there. raise AssertionError(f"Unexpected QT_API: {QT_API}") _version_info = tuple(QtCore.QLibraryInfo.version().segments()) diff --git a/lib/matplotlib/tests/test_backends_interactive.py b/lib/matplotlib/tests/test_backends_interactive.py index ade636433144..a8cbc8885daf 100644 --- a/lib/matplotlib/tests/test_backends_interactive.py +++ b/lib/matplotlib/tests/test_backends_interactive.py @@ -343,6 +343,26 @@ def test_qt5backends_uses_qt5(): _run_helper(_implcore, timeout=_test_timeout) +def _impl_missing(): + import sys + # Simulate uninstalled + sys.modules["PyQt6"] = None + sys.modules["PyQt5"] = None + sys.modules["PySide2"] = None + sys.modules["PySide6"] = None + + import matplotlib.pyplot as plt + with pytest.raises(ImportError, match="Failed to import any of the following Qt"): + plt.switch_backend("qtagg") + # Specifically ensure that Pyside6/Pyqt6 are not in the error message for qt5agg + with pytest.raises(ImportError, match="^(?:(?!(PySide6|PyQt6)).)*$"): + plt.switch_backend("qt5agg") + + +def test_qt_missing(): + _run_helper(_impl_missing, timeout=_test_timeout) + + def _impl_test_cross_Qt_imports(): import sys import importlib