PyQt Windows Won't Reopen in Spyder — How to Fix It

Why your PyQt app only runs once in Spyder, and what you can do about it
Heads up! You've already completed this tutorial.

I'm using Spyder in Anaconda to learn PyQt5. The first time I run my code, the Qt window pops up fine. But after I close the window and try to run the code again, nothing happens — no window, no error, nothing. I have to completely restart Spyder every time. Why does this happen?

This is one of the most common problems people run into when starting out with PyQt in Spyder, so if you've hit this wall, you're in good company. The good news is that it's not a problem with your code — it's a known quirk of how Spyder's IPython console interacts with the Qt event loop.

Why This Happens

When you run a PyQt application, a key part of the process is starting the Qt event loop by calling app.exec(). This event loop is what keeps your window open, responds to clicks and keypresses, and generally makes your GUI work. If you're new to this concept, our tutorial on signals, slots, and events covers how the event loop drives a PyQt application.

In a normal Python script run from a terminal, calling app.exec() starts the event loop, and when you close the window, the loop ends and the script finishes cleanly. The next time you run the script, everything starts fresh.

Spyder uses an embedded IPython console, and IPython has its own integration with Qt's event loop. When your script calls QApplication(), it creates an application instance inside that console session. After you close the window and the event loop ends, things aren't fully cleaned up — the old QApplication instance still exists in memory. When you try to run the script again, Qt sees that an application already exists and doesn't create a new one properly, so your window never appears.

Solutions

There are several ways to work around this. Pick the one that fits your workflow best.

Use %gui qt magic in the IPython Console

Spyder's IPython console supports a special command that tells IPython to manage the Qt event loop for you. Before running your script, type the following into Spyder's IPython console:

python
%gui qt

This tells IPython to integrate with Qt's event loop. With this active, you may also need to adjust your script so it doesn't call app.exec() itself (since IPython is already running the event loop). For example:

python
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel

app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = QMainWindow()
window.setCentralWidget(QLabel("Hello, PyQt!"))
window.show()

The QApplication.instance() check is the important part here — it reuses the existing application object if one already exists, instead of trying to create a second one (which Qt doesn't allow).

Change the Run Configuration

You can tell Spyder to run your script in a fresh, dedicated Python process instead of the built-in IPython console. This avoids the event loop conflict entirely.

  1. Go to Run → Configuration per file (or press Ctrl+F6).
  2. Under Console, select Execute in an external system terminal.
  3. Click OK and run your script.

Now each time you run the script, it starts in a clean process — just like running it from a terminal. The window will appear in its own process, and you can re-run as many times as you like.

Use Run → Profile

As discovered in the original forum thread, using Run → Profile instead of the normal Run command also works around this issue. Profiling runs the script in a separate process, which avoids the stale QApplication problem. This is a quick workaround, though it's not really what profiling is designed for.

Run from the Terminal

The most reliable way to develop PyQt applications is to run them directly from a terminal or command prompt. In your terminal, navigate to the folder containing your script and run:

bash
python my_app.py

Each run starts a completely fresh Python process, so you'll never hit the stale event loop issue. You can still use Spyder (or any other editor) to write and edit your code — just switch to the terminal to run it.

A Complete Working Example

Here's a minimal PyQt6 application that you can use to test any of the solutions above. If you need help setting up PyQt6 first, see our guide on creating your first PyQt6 window.

python
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt in Spyder — Test")

        button = QPushButton("Click me!")
        button.clicked.connect(self.on_button_clicked)

        layout = QVBoxLayout()
        layout.addWidget(button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def on_button_clicked(self):
        print("Button was clicked!")


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

If you're running this inside Spyder's IPython console and want to be able to re-run without restarting, replace the last three lines with:

python
app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = MainWindow()
window.show()

Which Approach Should I Use Long-Term?

For learning and day-to-day development, running your scripts from a terminal is the most straightforward option. It eliminates any IDE-specific quirks and ensures your application behaves exactly the way it will for your users. You can use Spyder (or VS Code, PyCharm, or any text editor you like) for writing code, and simply keep a terminal window open alongside it for running your scripts.

If you prefer to stay entirely within Spyder, using the external terminal run configuration is the cleanest fix, since it doesn't require any changes to your actual code.

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick

(PyQt6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!

More info Get the book

Martin Fitzpatrick

PyQt Windows Won't Reopen in Spyder — How to Fix It was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.