A solution to the problem that kernel restarting frequently occurs when running PyQt system with jupyter or Spyder IDE

Even when doing trial and error with a GUI program such as PyQt or PyQtGraph I often use jupyter etc. because it is convenient, There is one of the most annoying points.

When I run a PyQt GUI program several times,

** The kernel will definitely die. ** **

I finally found a workaround.

It didn't matter if sys.exit () was present at the end.

This → sys.exit (app.exec_ ())

I thought it was because sys.exit ended strangely, but it was innocent. The presence or absence of sys.exit () at exit was irrelevant (at least in jupyter). (Spyder is unconfirmed)

Just overwrite the app with something else just before! (PyQt4 only. Cannot be used with PySide)

app = 0 # Add this line app = QtGui.QApplication(sys.argv)

With this alone, even if you run the same code with jupyter many times, The annoying kernel restart is gone! Yay!

reference https://www.reddit.com/r/learnpython/comments/45h05k/solved_kernel_crashing_when_closing_gui_spyder/


2016/09/01 postscript

I could do it with mojaie's method in the comment. No matter how you think about it, this method is right.

app = QtGui.QApplication.instance()
if app is None:
    app = QtGui.QApplication(()) 

Also, while PyQt4 runs silently and the kernel dies, I was shocked that PySide prevented the creation of multiple instances of QApplication in advance and returned a runtime error. It's the difference between PyQt4 and PySide. .. ..

Also, PySide causes a runtime error, so app = 0 cannot be used. app = 0 is a magic for PyQt4 ...

Recommended Posts

A solution to the problem that kernel restarting frequently occurs when running PyQt system with jupyter or Spyder IDE
[Python] Solution to the problem that elements are linked when copying a list
Finding a solution to the N-Queen problem with a genetic algorithm (2)
The usual way to add a Kernel with Jupyter Notebook
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
Finding a solution to the N-Queen problem with a genetic algorithm (1)
A solution to the problem that the Python version in Conda cannot be changed
Solution to the problem that build does not end when installing OpenCV (PEP517)
A story that failed when trying to remove the suffix from the string with rstrip
A solution to the problem that files containing [and] are not listed in glob.glob ()
Two solutions to the problem that it is hard to see the vector field when writing a vector field with quiver () of matplotlib pyplot
How to deal with the problem that pandas 1.1.0 or later build fails on Alpine Linux
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
Temporary solution to the problem that the exe file created by PyInstaller is mistaken as a Trojan horse virus by AVAST or AVG
A story about how to deal with the CORS problem
Solution to the problem that the display is corrupted when the .exe command is included in the while loop in wsl2
How to deal with the problem that the current directory moves when Python is executed from Atom