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
(api)
| 108 | |
| 109 | |
| 110 | def has_binding(api): |
| 111 | """Safely check for PyQt4 or PySide, without importing |
| 112 | submodules |
| 113 | |
| 114 | Parameters |
| 115 | ---------- |
| 116 | api : str [ 'pyqtv1' | 'pyqt' | 'pyside' | 'pyqtdefault'] |
| 117 | Which module to check for |
| 118 | |
| 119 | Returns |
| 120 | ------- |
| 121 | True if the relevant module appears to be importable |
| 122 | """ |
| 123 | # we can't import an incomplete pyside and pyqt4 |
| 124 | # this will cause a crash in sip (#1431) |
| 125 | # check for complete presence before importing |
| 126 | |
| 127 | if api == QT_MOCK: |
| 128 | return True |
| 129 | |
| 130 | module_name = {QT_API_PYSIDE: 'PySide', |
| 131 | QT_API_PYQT: 'PyQt4', |
| 132 | QT_API_PYQTv1: 'PyQt4', |
| 133 | QT_API_PYQT_DEFAULT: 'PyQt4'} |
| 134 | module_name = module_name[api] |
| 135 | |
| 136 | import importlib |
| 137 | try: |
| 138 | #importing top level PyQt4/PySide module is ok... |
| 139 | #...importing submodules is not |
| 140 | mod = importlib.import_module(module_name + '.QtCore') |
| 141 | mod = importlib.import_module(module_name + '.QtGui') |
| 142 | mod = importlib.import_module(module_name + '.QtSvg') |
| 143 | |
| 144 | #we can also safely check PySide version |
| 145 | if api == QT_API_PYSIDE: |
| 146 | return check_version(mod.__version__, '1.0.3') |
| 147 | else: |
| 148 | return True |
| 149 | except ImportError: |
| 150 | return False |
| 151 | |
| 152 | |
| 153 | def qtapi_version(): |
no test coverage detected