Safely check for PyQt4/5, PySide or PySide2, without importing submodules Parameters ---------- api : str [ 'pyqtv1' | 'pyqt' | 'pyqt5' | 'pyside' | 'pyside2' | 'pyqtdefault'] Which module to check for Returns ------- True if the relevan
(api)
| 107 | |
| 108 | |
| 109 | def has_binding(api): |
| 110 | """Safely check for PyQt4/5, PySide or PySide2, without importing submodules |
| 111 | |
| 112 | Parameters |
| 113 | ---------- |
| 114 | api : str [ 'pyqtv1' | 'pyqt' | 'pyqt5' | 'pyside' | 'pyside2' | 'pyqtdefault'] |
| 115 | Which module to check for |
| 116 | |
| 117 | Returns |
| 118 | ------- |
| 119 | True if the relevant module appears to be importable |
| 120 | """ |
| 121 | module_name = api_to_module[api] |
| 122 | from importlib.util import find_spec |
| 123 | |
| 124 | required = ['QtCore', 'QtGui', 'QtSvg'] |
| 125 | if api in (QT_API_PYQT5, QT_API_PYSIDE2): |
| 126 | # QT5 requires QtWidgets too |
| 127 | required.append('QtWidgets') |
| 128 | |
| 129 | for submod in required: |
| 130 | try: |
| 131 | spec = find_spec('%s.%s' % (module_name, submod)) |
| 132 | except ImportError: |
| 133 | # Package (e.g. PyQt5) not found |
| 134 | return False |
| 135 | else: |
| 136 | if spec is None: |
| 137 | # Submodule (e.g. PyQt5.QtCore) not found |
| 138 | return False |
| 139 | |
| 140 | if api == QT_API_PYSIDE: |
| 141 | # We can also safely check PySide version |
| 142 | import PySide |
| 143 | return check_version(PySide.__version__, '1.0.3') |
| 144 | |
| 145 | return True |
| 146 | |
| 147 | |
| 148 | def qtapi_version(): |
no test coverage detected