title: When running PyQt5 app in Spyder it always exits with -1
body: <p>I am learning to program with PyQt5. No troubles with that, but there is one annoying issue: when I start the app, I immediately get the message that an exception occurred and SystemExit: -1 is repported (see below). </p>
  
  <pre><code>An exception has occurred, use %tb to see the full traceback.
  
  SystemExit: -1
  
  /home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
    warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
  
  
  In [2]: %tb
  Traceback (most recent call last):
  
    File "&lt;ipython-input-1-f5ccc42a06e6&gt;", line 1, in &lt;module&gt;
      runfile('/media/d/home/arnold/development/python/course_Qt/01-basic-window.py', wdir='/media/d/home/arnold/development/python/course_Qt')
  
    File "/home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 692, in runfile
      execfile(filename, namespace)
  
    File "/home/arnold/bin/anaconda3/envs/ml-gpu/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
      exec(compile(f.read(), filename, 'exec'), namespace)
  
    File "/media/d/home/arnold/development/python/course_Qt/01-basic-window.py", line 19, in &lt;module&gt;
      sys.exit(app.exec_()) # mind the underscore, without is python function
  
  SystemExit: -1
  </code></pre>
  
  <p>The program is a very simple one:</p>
  
  <pre><code>import sys
  from PyQt5.QtWidgets import QApplication, QWidget
  
  app = QApplication(sys.argv)
  win = QWidget() # No arguments = toplevel window
  win.setWindowTitle('Qt5 Window')
  win.resize(640, 480)
  win.show()
  
  # wrap Qt5 call in sys.exit in order to catch any unhandled exception
  # just good practice
  sys.exit(app.exec_()) # mind the underscore, without is python function
  </code></pre>
  
  <p>The program runs fine after that, I can do all things what I want and normally exit. When I try to run the program again I get the message:</p>
  
  <pre><code>QCoreApplication::exec: The event loop is already running
  </code></pre>
  
  <p>I forcefully terminate the program and when I try to run it again it starts all over again. Is it my setup or is something else wrong? I could not find a hint when googling for this error.</p>
  
  <p>Setup: linux min 18.3, Spyder 3.5, conda 4.4.11, PyQt 5.10.1</p>
  
  <p><strong>Update</strong></p>
  
  <p>The answer of Carlos below is the correct one. What I did not mention is that I used QtDesigner with PyQt5 and couldn't find a decent example for it. <a href="http://pythonforengineers.com/your-first-gui-app-with-python-and-pyqt/" rel="nofollow noreferrer">This is the best I could find</a> and I adjusted it to the solution Carlos mentioned below. Save <code>pyqt_first.py</code> and <code>tax_calc.ui</code>, run <code>pyuic5 -o tax_calc.py tax_calc.ui</code> and everything should run fine. Do not forget to set the graphics backend to <code>inline</code>(Tools > Preferences > IPython console). </p>
  
  <pre><code>#--- pyqt_first.py ---
  import sys
  from PyQt5 import QtWidgets
  from PyQt5.QtWidgets import QMainWindow
  from tax_calc import Ui_MainWindow
  
  class HelloWindow(QMainWindow, Ui_MainWindow):
      def __init__(self):
          QMainWindow.__init__(self)
          Ui_MainWindow.__init__(self)
          self.setupUi(self)
          self.calc_tax_button.clicked.connect(self.CalculateTax)
  
      def CalculateTax(self):
          price = int(self.price_box.toPlainText())
          tax = (self.tax_rate.value())
          total_price = price  + ((tax / 100) * price)
          total_price_string = "The total price with tax is: " + str(total_price)
          self.results_window.setText(total_price_string)
  
  if __name__ == "__main__":
      def run_app():
          app = QtWidgets.QApplication(sys.argv)
          mainWin = HelloWindow()
          mainWin.show()
          app.exec_()
  
      run_app()
  
  #--- tax_calc.ui ---
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;800&lt;/width&gt;
      &lt;height&gt;600&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralwidget"&gt;
     &lt;widget class="QTextEdit" name="price_box"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;220&lt;/x&gt;
        &lt;y&gt;100&lt;/y&gt;
        &lt;width&gt;104&lt;/width&gt;
        &lt;height&gt;71&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QLabel" name="label"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;50&lt;/x&gt;
        &lt;y&gt;120&lt;/y&gt;
        &lt;width&gt;91&lt;/width&gt;
        &lt;height&gt;31&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="font"&gt;
       &lt;font&gt;
        &lt;pointsize&gt;10&lt;/pointsize&gt;
        &lt;weight&gt;75&lt;/weight&gt;
        &lt;bold&gt;true&lt;/bold&gt;
       &lt;/font&gt;
      &lt;/property&gt;
      &lt;property name="text"&gt;
       &lt;string&gt;Price&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QSpinBox" name="tax_rate"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;230&lt;/x&gt;
        &lt;y&gt;250&lt;/y&gt;
        &lt;width&gt;42&lt;/width&gt;
        &lt;height&gt;22&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="value"&gt;
       &lt;number&gt;20&lt;/number&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QLabel" name="label_2"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;60&lt;/x&gt;
        &lt;y&gt;250&lt;/y&gt;
        &lt;width&gt;47&lt;/width&gt;
        &lt;height&gt;13&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="text"&gt;
       &lt;string&gt;Tax Rate&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QPushButton" name="calc_tax_button"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;230&lt;/x&gt;
        &lt;y&gt;350&lt;/y&gt;
        &lt;width&gt;111&lt;/width&gt;
        &lt;height&gt;23&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="text"&gt;
       &lt;string&gt;Calculate Tax&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QTextEdit" name="results_window"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;180&lt;/x&gt;
        &lt;y&gt;410&lt;/y&gt;
        &lt;width&gt;131&lt;/width&gt;
        &lt;height&gt;71&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
     &lt;widget class="QLabel" name="label_3"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;170&lt;/x&gt;
        &lt;y&gt;20&lt;/y&gt;
        &lt;width&gt;441&lt;/width&gt;
        &lt;height&gt;41&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="font"&gt;
       &lt;font&gt;
        &lt;pointsize&gt;20&lt;/pointsize&gt;
        &lt;weight&gt;75&lt;/weight&gt;
        &lt;bold&gt;true&lt;/bold&gt;
       &lt;/font&gt;
      &lt;/property&gt;
      &lt;property name="text"&gt;
       &lt;string&gt;Sales Tax Calculator&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/widget&gt;
    &lt;widget class="QMenuBar" name="menubar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;800&lt;/width&gt;
       &lt;height&gt;20&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusbar"/&gt;
   &lt;/widget&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>