Skip to content

Better selection of Qt bindings when QT_API is not specified #495

wants to merge 1 commit into from
25 IPython/external/
@@ -7,17 +7,34 @@
QT_API_PYQT = 'pyqt'
QT_API_PYSIDE = 'pyside'
-# Use PyQt by default until PySide is stable.
-QT_API = os.environ.get('QT_API', QT_API_PYQT)
+def prepare_pyqt4():
# For PySide compatibility, use the new-style string API that automatically
# converts QStrings to Unicode Python strings. Also, automatically unpack
# QVariants to their underlying objects.
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
+# Select Qt binding, using the QT_API environment variable if available.
+QT_API = os.environ.get('QT_API')
+if QT_API is None:
+ try:
+ import PySide
+ except ImportError:
+ try:
+ prepare_pyqt4()
+ import PyQt4
+ except ImportError:
+ raise ImportError('Cannot import PySide or PyQt4')
+elif QT_API == QT_API_PYQT:
IPython member
fperez added a note Jun 3, 2011

Wouldn't it be cleaner to have the prepare_call in the if QT_API == QT_API_PYQT block below? I realize it may get called twice (if the None path was entered and then it was set due to not finding pyside), but as long as that function is reentrant it shouldn't be a problem, and it makes the code a little cleaner than having a double block with the same if statement.

Not a huge deal though. Otherwise looks good, once you have a look at this point, merge away.


epatters added a note Jun 3, 2011

If a call to sip.setapi is made after importing PyQt4, sip throws an error. So it is actually important that it is not called again.

IPython member
fperez added a note Jun 3, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # Note: This must be called *before* PyQt4 is imported.
+ prepare_pyqt4()
+# Now peform the imports.
from PyQt4 import QtCore, QtGui, QtSvg
# Alias PyQt-specific functions for PySide compatibility.
Something went wrong with that request. Please try again.