Be a little more tolerant of missing Qt packages, and defer import errors until

we try to use the missing package.
This commit is contained in:
Luke Campagnola 2018-01-24 09:03:44 -08:00
parent 6b06922da2
commit 4867149d83

View File

@ -43,8 +43,29 @@ if QT_LIB is None:
if QT_LIB is None: if QT_LIB is None:
raise Exception("PyQtGraph requires one of PyQt4, PyQt5 or PySide; none of these packages could be imported.") raise Exception("PyQtGraph requires one of PyQt4, PyQt5 or PySide; none of these packages could be imported.")
class FailedImport(object):
"""Used to defer ImportErrors until we are sure the module is needed.
"""
def __init__(self, err):
self.err = err
def __getattr__(self, attr):
raise self.err
if QT_LIB == PYSIDE: if QT_LIB == PYSIDE:
from PySide import QtGui, QtCore, QtOpenGL, QtSvg from PySide import QtGui, QtCore
try:
from PySide import QtOpenGL
except ImportError as err:
QtOpenGL = FailedImport(err)
try:
from PySide import QtSvg
except ImportError as err:
QtSvg = FailedImport(err)
try: try:
from PySide import QtTest from PySide import QtTest
if not hasattr(QtTest.QTest, 'qWait'): if not hasattr(QtTest.QTest, 'qWait'):
@ -55,9 +76,9 @@ if QT_LIB == PYSIDE:
while time.time() < start + msec * 0.001: while time.time() < start + msec * 0.001:
QtGui.QApplication.processEvents() QtGui.QApplication.processEvents()
QtTest.QTest.qWait = qWait QtTest.QTest.qWait = qWait
except ImportError as err:
QtTest = FailedImport(err)
except ImportError:
pass
import PySide import PySide
try: try:
from PySide import shiboken from PySide import shiboken
@ -133,16 +154,16 @@ elif QT_LIB == PYQT4:
from PyQt4 import QtGui, QtCore, uic from PyQt4 import QtGui, QtCore, uic
try: try:
from PyQt4 import QtSvg from PyQt4 import QtSvg
except ImportError: except ImportError as err:
pass QtSvg = FailedImport(err)
try: try:
from PyQt4 import QtOpenGL from PyQt4 import QtOpenGL
except ImportError: except ImportError as err:
pass QtOpenGL = FailedImport(err)
try: try:
from PyQt4 import QtTest from PyQt4 import QtTest
except ImportError: except ImportError as err:
pass QtTest = FailedImport(err)
VERSION_INFO = 'PyQt4 ' + QtCore.PYQT_VERSION_STR + ' Qt ' + QtCore.QT_VERSION_STR VERSION_INFO = 'PyQt4 ' + QtCore.PYQT_VERSION_STR + ' Qt ' + QtCore.QT_VERSION_STR
@ -157,6 +178,7 @@ elif QT_LIB == PYQT5:
# users), we install a global exception hook to override this behavior. # users), we install a global exception hook to override this behavior.
ver = QtCore.PYQT_VERSION_STR.split('.') ver = QtCore.PYQT_VERSION_STR.split('.')
if int(ver[1]) >= 5: if int(ver[1]) >= 5:
if sys.excepthook == sys.__excepthook__:
sys_excepthook = sys.excepthook sys_excepthook = sys.excepthook
def pyqt5_qabort_override(*args, **kwds): def pyqt5_qabort_override(*args, **kwds):
return sys_excepthook(*args, **kwds) return sys_excepthook(*args, **kwds)
@ -164,17 +186,17 @@ elif QT_LIB == PYQT5:
try: try:
from PyQt5 import QtSvg from PyQt5 import QtSvg
except ImportError: except ImportError as err:
pass QtSvg = FailedImport(err)
try: try:
from PyQt5 import QtOpenGL from PyQt5 import QtOpenGL
except ImportError: except ImportError as err:
pass QtOpenGL = FailedImport(err)
try: try:
from PyQt5 import QtTest from PyQt5 import QtTest
QtTest.QTest.qWaitForWindowShown = QtTest.QTest.qWaitForWindowExposed QtTest.QTest.qWaitForWindowShown = QtTest.QTest.qWaitForWindowExposed
except ImportError: except ImportError as err:
pass QtTest = FailedImport(err)
# Re-implement deprecated APIs # Re-implement deprecated APIs