diff --git a/lib/matplotlib/backends/qt_compat.py b/lib/matplotlib/backends/qt_compat.py index c564fd0c6c02..6ce8fd63d501 100644 --- a/lib/matplotlib/backends/qt_compat.py +++ b/lib/matplotlib/backends/qt_compat.py @@ -109,9 +109,18 @@ except: res = 'QVariant API v2 specification failed. Defaulting to v1.' verbose.report(cond + res, 'helpful') + if QT_API == QT_API_PYQT5: + try: + from PyQt5 import QtCore, QtGui, QtWidgets + _getSaveFileName = QtWidgets.QFileDialog.getSaveFileName + except ImportError: + # fell through, tried PyQt5, failed fall back to PyQt4 + QT_API = rcParams['backend.qt4'] + QT_RC_MAJOR_VERSION = 4 + # needs to be if so we can re-test the value of QT_API which may + # have been changed in the above if block if QT_API in [QT_API_PYQT, QT_API_PYQTv2]: # PyQt4 API - from PyQt4 import QtCore, QtGui try: @@ -131,9 +140,10 @@ def _getSaveFileName(*args, **kwargs): def _getSaveFileName(*args, **kwargs): return QtGui.QFileDialog.getSaveFileName(*args, **kwargs), None - else: # PyQt5 API - from PyQt5 import QtCore, QtGui, QtWidgets - _getSaveFileName = QtWidgets.QFileDialog.getSaveFileName + else: + raise RuntimeError("PyQt{4,5} bindings found despite sip importing\n" + "Please install PyQt4 or PyQt5, uninstall sip or " + "explicitly set the pyside backend.") # Alias PyQt-specific functions for PySide compatibility. QtCore.Signal = QtCore.pyqtSignal