Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyQt code cannot run twice #2970

Closed
mvkrdndm opened this issue Feb 8, 2016 · 27 comments · Fixed by spyder-ide/spyder-kernels#189
Closed

PyQt code cannot run twice #2970

mvkrdndm opened this issue Feb 8, 2016 · 27 comments · Fixed by spyder-ide/spyder-kernels#189

Comments

@mvkrdndm
Copy link

mvkrdndm commented Feb 8, 2016

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    sys.exit(app.exec_())  

i can not run the code twice it says kernel stopped.but in other ide's it runs smoothly again and again in web there are other ppl mention about this please fix this bug
http://stackoverflow.com/questions/19459299/running-a-pyqt-application-twice-from-one-prompt-in-spyder
http://stackoverflow.com/questions/33527108/pyqt-examples-not-running-second-time

this bug is general it covers all spyder versions i think ,i tried a few but in all , kernel crashes

@ccordoba12
Copy link
Member

Yes, this is a known issue and we don't know how to fix it yet.

@goanpeca
Copy link
Member

goanpeca commented Feb 9, 2016

@mvkrdndm, I would advice that you use F6 option Execute in a new dedicated Python console when running Qt Code.

image

@eendebakpt
Copy link
Contributor

The following code works for me. Note the check on QApplication already running and not executing the exit statement at the end.

import sys
import PyQt4.QtGui as QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

@cvanelteren
Copy link

The solution provided by @eendebakp does not work for me on windows, but does on linux
Windows machine uses anaconda w/ python 3.5
Linux machine uses python 3.6 (no anaconda)

@TheHugeManatee
Copy link

based on @eendebakpt 's code, the following workaround works for me on windows/anaconda 4.4.0/python 3.6:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *


if __name__ == '__main__':
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QtWidgets.QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

can't say anything about other platforms currently

@ccordoba12 ccordoba12 modified the milestones: v3.2.1, v4.0beta2 Jul 5, 2017
@ccordoba12 ccordoba12 modified the milestones: v3.2.1, v3.2.2 Jul 28, 2017
tentekal added a commit to tentekal/backgroundSubtraction that referenced this issue Aug 2, 2017
@ccordoba12 ccordoba12 modified the milestones: v3.2.2, v3.2.3 Aug 26, 2017
@youtanyouzhen
Copy link

youtanyouzhen commented Sep 1, 2017

I get rid of it by running the program with hot key F10 (Run→Profile).

@ccordoba12
Copy link
Member

@dalthviz, can you reproduce this on Windows?

@dalthviz
Copy link
Member

dalthviz commented Nov 28, 2019

@ccordoba12 testing this on Windows, Python 3.7.3/Anaconda:

Code versions that work:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *


if __name__ == '__main__':
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))

    window = QtWidgets.QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

image

# Similar code to the original post but using PyQt5
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    sys.exit(app.exec_()) 

image

Code version with strange behaviors:

Running the following code from a file no issue raises but seems like the kernel is restarting when running the second time:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    app.exec_()

image

Also, should the wiki page we have about running PyQt applications be updated?

@ccordoba12
Copy link
Member

@dalthviz, so what's your conclusion? I mean, when do things work and when do they fail?

@impact27
Copy link
Contributor

impact27 commented Nov 29, 2019

I can reproduce if I paste the code in the console directly. I get a segfault when creating the widget:

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [26752]

VM Regions Near 0:
--> 
    __TEXT                 0000000101037000-0000000101039000 [    8K] r-x/r-x SM=COW  /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   QtGui                         	0x0000000114c30e3f QGuiApplication::font() + 127
1   QtGui                         	0x0000000114ccd984 QFont::QFont() + 36
2   QtWidgets                     	0x000000011576190b QWidgetPrivate::QWidgetPrivate(int) + 331
3   QtWidgets                     	0x000000011576270e QWidget::QWidget(QWidget*, QFlags<Qt::WindowType>) + 46
4   QtWidgets.so                  	0x000000011532116d init_type_QWidget(_sipSimpleWrapper*, _object*, _object*, _object**, _object**, _object**) + 173
5   sip.cpython-37m-darwin.so     	0x0000000114866ac5 sipSimpleWrapper_init + 181

Without the widget:

In [1]: from PyQt5 import QtWidgets
   ...: app = QtWidgets.QApplication([])
   ...: app.exec_()
Out[1]: 0

In [2]: from PyQt5 import QtWidgets
   ...: app = QtWidgets.QApplication([])
   ...: app.exec_()
QApplication::exec: Please instantiate the QApplication object first
Out[2]: -1

In [3]: 

Without executing: (Crashes)

In [1]: from PyQt5 import QtWidgets
   ...: app = QtWidgets.QApplication([])
   ...: window = QtWidgets.QWidget()
   ...: window.show()

In [2]: from PyQt5 import QtWidgets
   ...: app = QtWidgets.QApplication([])
   ...: window = QtWidgets.QWidget()
   ...: window.show()

In [1]: 

Without an app: (Crashes)

In [1]: from PyQt5 import QtWidgets
   ...: window = QtWidgets.QWidget()
   ...: window.show()

In [1]: 

But I can reproduce that in qtconsole and in ipython and in python:

quentinpeter@MacBook-Pro-de-Quentin ~ % python3
Python 3.7.5 (default, Nov  1 2019, 02:16:23) 
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget()
<PyQt5.QtWidgets.QWidget object at 0x10fc2c370>
>>> from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget()
zsh: segmentation fault  python3

Some tests in python:

# Crashes after two times with SEGV
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget()
# Doesn't crash
from PyQt5 import QtWidgets; QtWidgets.QApplication([]); QtWidgets.QWidget()
# Crashes immediately with ABRT
from PyQt5 import QtWidgets; QtWidgets.QWidget()
# Doesn't crash
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([])
# Doesn't crash
from PyQt5 import QtWidgets; app = QtWidgets.QApplication([]); QtWidgets.QWidget(); del app

@impact27
Copy link
Contributor

impact27 commented Nov 29, 2019

My guess is the following: when QtWidgets.QApplication([]) is called, a new python wrapper is created, but it wraps a singleton. So when the second time app = QtWidgets.QApplication([]) is called:

  • python has an old app object that is losing its name, so it must be destroyed because:
  • A new python object is taking the app name

But the new python object is really a wrapper around the same C++ object. Therefore the second wrapper ends up empty.

@impact27
Copy link
Contributor

Could you try spyder-ide/spyder-kernels#189 to see if this fixes it?

@goanpeca
Copy link
Member

Will take a look @impact27 thanks!

@goanpeca goanpeca modified the milestones: not sorted, 4.1.0 Nov 29, 2019
@dalthviz
Copy link
Member

@impact27 I left a review at spyder-ide/spyder-kernels#189 . Thanks! :)

@goanpeca
Copy link
Member

@dalthviz does it work for you with the fix?

@goanpeca
Copy link
Member

Works like a charm on Mac @impact27 !

# Standard library imports
import sys

# Third party imports
from qtpy.QtWidgets import QApplication, QWidget


if __name__ == '__main__':
    app = QApplication([])
    window = QWidget()
    window.setGeometry(100, 100, 500, 300)
    window.setWindowTitle("PyQT Tuts!")
    window.show()
    window.raise_()
    sys.exit(app.exec_())

@ccordoba12 ccordoba12 modified the milestones: v4.1.0, v4.0.2 Jan 16, 2020
@goanpeca goanpeca modified the milestones: v4.0.2, v4.1.0 Jan 24, 2020
@ccordoba12 ccordoba12 modified the milestones: v4.2.0, v4.1.0 Jan 24, 2020
@goanpeca goanpeca modified the milestones: v4.1.0, Sprint 2 (February) Feb 18, 2020
@ccordoba12 ccordoba12 changed the title PyQT code can not run twice : Kernel stops PyQt code cannot run twice Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.