QT Creator converted ui to py working in windows terminal but not working with spyder IDE
<p>I am following a tutorial on making a basic GUI with QT creator and python. I have successfully created a window with a button that closes the window when pressed. It works fine in QT Creator and I converted the ui file to py and it runs and creates the window as expected when I open a command prompt window and call it with <code>python main.py</code>. I enjoy working in the spyder IDE and would like to continue to do so. The issue is that if I open the main.py in spyder and run it will first give the error </p>

<pre><code>An exception has occurred, use %tb to see the full traceback.

SystemExit: -1
</code></pre>

<p>then if I attempt to run it a second time the kernel will hang. </p>

<p>What is required to run this script in spyder successfully? Here is the code:</p>

<pre><code>from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(400, 300)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralWidget)
        self.horizontalLayout.setMargin(11)
        self.horizontalLayout.setSpacing(6)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.pushButton = QtGui.QPushButton(self.centralWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
        self.menuBar.setObjectName(_fromUtf8("menuBar"))
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(MainWindow)
        self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(MainWindow)
        self.statusBar.setObjectName(_fromUtf8("statusBar"))
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "Close", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
</code></pre>

<p>It's hard to say without the actual traceback.  I'm guessing it's one of two things, both related to the fact that Spyder is also built on Qt.</p>

<ol>
<li><p>You're trying to run your app within the Spyder environment.  Your app tries to create a <code>QApplication</code>, but because Spyder already runs on <code>Qt</code>, so a <code>QApplication</code> already exists and it errors.</p></li>
<li><p>Mismatched versions of PyQt/Qt.  I'm assuming Spyder ships with it's own version of Qt/PyQt and you didn't have to install it to run Spyder.  But I'm guessing you also did install your own version of Qt/PyQt and PyQt is loading the wrong dll's (the ones shipped with Spyder and not the ones you installed).</p></li>
</ol>

<p>Some things to check:</p>

<ul>
<li><p>Check to make sure you're launching an external process from Spyder (i.e. in a brand new shell with a new python process) and not simply running the code within the Spyder embedded python process.  I don't know Spyder well enough to know how to do this, but most IDE's have some setup that control how it launches external processes.</p></li>
<li><p>Check the <code>PATH</code> environment variable from your launched script.  It's possible that the Spyder directory is being added before your installation of Qt, causing the Spyder Qt dll's to load instead of your system install of Qt when importing PyQt.</p></li>
</ul>

python user-interface pyqt pyqt4 spyder