PyQt program crashes without error when run from the command line
-
wrote on 18 Nov 2021, 15:26 last edited by Donald Duck
I have the following simple PyQt program:
from PyQt5.QtWidgets import QApplication, QFileDialog import sys QApplication(sys.argv) chosenFile = QFileDialog.getSaveFileName(caption="Save file")[0] print("Chosen file: " + chosenFile)
When I run this from Spyder, it works just fine. It also works if I run python.exe with no arguments and enter each line one by one.
But when I run it from the command line using
python.exe path/to/script.py
it crashes on line 5 with the exit code -1073741819 without throwing any Python error.I'm using Python 3.8 and Anaconda 4.10 on Windows 11.
-
I have the following simple PyQt program:
from PyQt5.QtWidgets import QApplication, QFileDialog import sys QApplication(sys.argv) chosenFile = QFileDialog.getSaveFileName(caption="Save file")[0] print("Chosen file: " + chosenFile)
When I run this from Spyder, it works just fine. It also works if I run python.exe with no arguments and enter each line one by one.
But when I run it from the command line using
python.exe path/to/script.py
it crashes on line 5 with the exit code -1073741819 without throwing any Python error.I'm using Python 3.8 and Anaconda 4.10 on Windows 11.
wrote on 18 Nov 2021, 16:13 last edited by eyllanesc@Donald-Duck When you say command line do you mean the CMD or the terminal of some IDE? Also change to
app = QApplication(sys.argv)
-
I have the following simple PyQt program:
from PyQt5.QtWidgets import QApplication, QFileDialog import sys QApplication(sys.argv) chosenFile = QFileDialog.getSaveFileName(caption="Save file")[0] print("Chosen file: " + chosenFile)
When I run this from Spyder, it works just fine. It also works if I run python.exe with no arguments and enter each line one by one.
But when I run it from the command line using
python.exe path/to/script.py
it crashes on line 5 with the exit code -1073741819 without throwing any Python error.I'm using Python 3.8 and Anaconda 4.10 on Windows 11.
wrote on 18 Nov 2021, 16:08 last edited by JonB@Donald-Duck
I just tried it under Ubuntu 20.04 and it does aSegmentation fault (core dumped)
without putting up a dialog. It does that forpython3 script.py
and also forpython3 < script.py
. However, as you observe it does not do that if I gopython3
and then paste the same lines in.I agree this seems brain-damaged :) The only difference is that
python3
has a stdin attached to an interactive terminal wherepython3 < script.py
does not. Why that should be of consequence I do not know. Good luck finding out, I will be surprised (but interested) if anyone can give you a definitive explanation....P.S.
Running under gdb it segments withThread 1 "python3" received signal SIGSEGV, Segmentation fault. 0x00007ffff63edede in QGuiApplication::font() () from /lib/x86_64-linux-gnu/libQt5Gui.so.5
and it is calling that from
QFileDialog::QFileDialog()
constructor. So that's your only clue --- it's a font-y issue. -
I have the following simple PyQt program:
from PyQt5.QtWidgets import QApplication, QFileDialog import sys QApplication(sys.argv) chosenFile = QFileDialog.getSaveFileName(caption="Save file")[0] print("Chosen file: " + chosenFile)
When I run this from Spyder, it works just fine. It also works if I run python.exe with no arguments and enter each line one by one.
But when I run it from the command line using
python.exe path/to/script.py
it crashes on line 5 with the exit code -1073741819 without throwing any Python error.I'm using Python 3.8 and Anaconda 4.10 on Windows 11.
wrote on 18 Nov 2021, 16:13 last edited by eyllanesc@Donald-Duck When you say command line do you mean the CMD or the terminal of some IDE? Also change to
app = QApplication(sys.argv)
-
@Donald-Duck When you say command line do you mean the CMD or the terminal of some IDE? Also change to
app = QApplication(sys.argv)
wrote on 18 Nov 2021, 16:16 last edited by JonB@eyllanesc
You can see my finding, so this happens even under Ubuntu. But not if you type the script lines in interactively. Very odd.If you can explain why I get the segmentation fault I show during font creation in the specific circumstances mentioned you will have solved.... I am beginning to wonder about a timing issue, but only a wild guess. (Nope, a
time.sleep()
did not cure.) -
@eyllanesc
You can see my finding, so this happens even under Ubuntu. But not if you type the script lines in interactively. Very odd.If you can explain why I get the segmentation fault I show during font creation in the specific circumstances mentioned you will have solved.... I am beginning to wonder about a timing issue, but only a wild guess. (Nope, a
time.sleep()
did not cure.)wrote on 18 Nov 2021, 16:21 last edited by@JonB My comment has 2 parts,
- I want to know why the OP gets a numeric code and not an error message or at least a Segmentation fault, hence my question.
- I suspect that the error is caused by the life cycle of the variables, in special environments like spyder this creates a QAppliction by default, other times in the interactive python session the objects that are explicitly assigned to variables are implicitly assigned but that does not happen in other cases, hence my suggestion that you assign QApplication to a variable, and if that is why then it also explains that the error is reproduced in Linux.
-
@Donald-Duck When you say command line do you mean the CMD or the terminal of some IDE? Also change to
app = QApplication(sys.argv)
wrote on 18 Nov 2021, 16:22 last edited by JonB@eyllanesc said in PyQt program crashes without error when run from the command line:
Also change to
app = QApplication(sys.argv)
Yeah, that makes it work! Sigh...
@Donald-Duck
It is kind of a timing thing! As @eyllanesc showed you must keep a reference around to theQApplication
. Else it gets released, and timing-wise for whatever reason this shows up in certain cases....@eyllanesc
Your last post crossed with mine. I didn't look at OP's code, but your theory was correct. Good spot! -
@eyllanesc said in PyQt program crashes without error when run from the command line:
Also change to
app = QApplication(sys.argv)
Yeah, that makes it work! Sigh...
@Donald-Duck
It is kind of a timing thing! As @eyllanesc showed you must keep a reference around to theQApplication
. Else it gets released, and timing-wise for whatever reason this shows up in certain cases....@eyllanesc
Your last post crossed with mine. I didn't look at OP's code, but your theory was correct. Good spot!wrote on 18 Nov 2021, 16:24 last edited by eyllanesc@JonB There is widespread bad practice in not assigning objects to variables, so they often cause silent bugs, and they forget the concept of scope that is related to the life cycle of objects. By not assigning a variable then the object is eliminated, and when the python object is eliminated the C++ object is also eliminated.
Another option that pleases both environments can be:
app = QApplication.instance() if app is None: app = QApplication(sys.argv)
-
@JonB There is widespread bad practice in not assigning objects to variables, so they often cause silent bugs, and they forget the concept of scope that is related to the life cycle of objects. By not assigning a variable then the object is eliminated, and when the python object is eliminated the C++ object is also eliminated.
Another option that pleases both environments can be:
app = QApplication.instance() if app is None: app = QApplication(sys.argv)
wrote on 18 Nov 2021, 16:29 last edited by JonB@eyllanesc
Indeed. I do more C++ than Python, so I don't come across the Python-lifetimes so often.I had a kind of related one ages ago: from Python, not C++, how to create a modeless application window/dialog. Because it had no parent and I had no need to reference to it after creation I just never saw any dialog getting created, and didn't know what was going on. I had to create a "dummy global variable" to assign to it, just to keep it in existence :)
What is amusing/confusing in @Donald-Duck's case is that if you go
python3 < script.py
it crashes, but if you just gopython3
and paste those 5 lines as one "blob" (single paste) it works. It is not evident why those two are sufficiently distinct to show up the lifetime issue. -
@Donald-Duck When you say command line do you mean the CMD or the terminal of some IDE? Also change to
app = QApplication(sys.argv)
wrote on 18 Nov 2021, 16:40 last edited by Donald Duck@eyllanesc I tried
app = QApplication(sys.argv)
and it worked. It seems like Python thought I didn't need that object anymore and deleted it from memory before the code was done running.I didn't think it was necessary to assign a variable to that object if I never use it in my own code, but apparently I was wrong.
I would guess the reason it worked in Spyder and in the Python console is that they don't delete variables as often since in both cases the user can type commands dynamically and re-use variables again.
-
@eyllanesc I tried
app = QApplication(sys.argv)
and it worked. It seems like Python thought I didn't need that object anymore and deleted it from memory before the code was done running.I didn't think it was necessary to assign a variable to that object if I never use it in my own code, but apparently I was wrong.
I would guess the reason it worked in Spyder and in the Python console is that they don't delete variables as often since in both cases the user can type commands dynamically and re-use variables again.
wrote on 18 Nov 2021, 16:47 last edited by@Donald-Duck
That's exactly what @eyllanesc was saying I was talking about.In Python (very much Python) if you do not keep a reference to
QApplication()
it releases that object. This applies to any object.
1/10