def import_pyqt4(version=2): """ Import PyQt4 Parameters ---------- version : 1, 2, or None Which QString/QVariant API to use. Set to None to use the system default ImportErrors raised within this function are non-recoverable """ # The new-style string API (version=2) automatically # converts QStrings to Unicode Python strings. Also, automatically unpacks # QVariants to their underlying objects. import sip if version is not None: sip.setapi('QString', version) sip.setapi('QVariant', version) from PyQt4 import QtGui, QtCore, QtSvg if not check_version(QtCore.PYQT_VERSION_STR, '4.7'): raise ImportError("IPython requires PyQt4 >= 4.7, found %s" % QtCore.PYQT_VERSION_STR) # Alias PyQt-specific functions for PySide compatibility. QtCore.Signal = QtCore.pyqtSignal QtCore.Slot = QtCore.pyqtSlot # query for the API version (in case version == None) version = sip.getapi('QString') api = QT_API_PYQTv1 if version == 1 else QT_API_PYQT return QtCore, QtGui, QtSvg, api
def has_binding(api): """Safely check for PyQt4 or PySide, without importing submodules Parameters ---------- api : str [ 'pyqtv1' | 'pyqt' | 'pyside' | 'pyqtdefault'] Which module to check for Returns ------- True if the relevant module appears to be importable """ # we can't import an incomplete pyside and pyqt4 # this will cause a crash in sip (#1431) # check for complete presence before importing module_name = { QT_API_PYSIDE: 'PySide', QT_API_PYSIDE2: 'PySide2', QT_API_PYQT: 'PyQt4', QT_API_PYQTv1: 'PyQt4', QT_API_PYQT_DEFAULT: 'PyQt4', QT_API_PYQT5: 'PyQt5', } module_name = module_name[api] import imp try: # importing top level PyQt4/PySide module is ok... mod = __import__(module_name) # ...importing submodules is not imp.find_module('QtCore', mod.__path__) imp.find_module('QtGui', mod.__path__) imp.find_module('QtSvg', mod.__path__) # we can also safely check PySide version if api == QT_API_PYSIDE: return check_version(mod.__version__, '1.0.3') else: return True except ImportError: return False
def get_options(): """Return a list of acceptable QT APIs, in decreasing order of preference """ #already imported Qt somewhere. Use that loaded = loaded_api() if loaded is not None: return [loaded] mpl = sys.modules.get('matplotlib', None) if mpl is not None and not check_version(mpl.__version__, '1.0.2'): #1.0.1 only supports PyQt4 v1 return [QT_API_PYQT_DEFAULT] if os.environ.get('QT_API', None) is None: #no ETS variable. Ask mpl, then use either return matplotlib_options(mpl) or [QT_API_PYQT_DEFAULT, QT_API_PYSIDE, QT_API_PYQT5] #ETS variable present. Will fallback to external.qt return None
def has_binding(api): """Safely check for PyQt4 or PySide, without importing submodules Parameters ---------- api : str [ 'pyqtv1' | 'pyqt' | 'pyside' | 'pyqtdefault'] Which module to check for Returns ------- True if the relevant module appears to be importable """ # we can't import an incomplete pyside and pyqt4 # this will cause a crash in sip (#1431) # check for complete presence before importing module_name = {QT_API_PYSIDE: 'PySide', QT_API_PYQT: 'PyQt4', QT_API_PYQTv1: 'PyQt4', QT_API_PYQT_DEFAULT: 'PyQt4', QT_API_PYQT5: 'PyQt5', } module_name = module_name[api] import imp try: #importing top level PyQt4/PySide module is ok... mod = __import__(module_name) #...importing submodules is not imp.find_module('QtCore', mod.__path__) imp.find_module('QtGui', mod.__path__) imp.find_module('QtSvg', mod.__path__) #we can also safely check PySide version if api == QT_API_PYSIDE: return check_version(mod.__version__, '1.0.3') else: return True except ImportError: return False