Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt program crashes without error when run from the command line
Forum Updated to NodeBB v4.3 + New Features

PyQt program crashes without error when run from the command line

Scheduled Pinned Locked Moved Solved Qt for Python
10 Posts 3 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Donald Duck
    wrote on 18 Nov 2021, 15:26 last edited by Donald Duck
    #1

    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.

    J E 2 Replies Last reply 18 Nov 2021, 16:08
    0
    • D Donald Duck
      18 Nov 2021, 15:26

      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.

      E Offline
      E Offline
      eyllanesc
      wrote on 18 Nov 2021, 16:13 last edited by eyllanesc
      #3

      @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)

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      J D 3 Replies Last reply 18 Nov 2021, 16:16
      3
      • D Donald Duck
        18 Nov 2021, 15:26

        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.

        J Offline
        J Offline
        JonB
        wrote on 18 Nov 2021, 16:08 last edited by JonB
        #2

        @Donald-Duck
        I just tried it under Ubuntu 20.04 and it does a Segmentation fault (core dumped) without putting up a dialog. It does that for python3 script.py and also for python3 < script.py. However, as you observe it does not do that if I go python3 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 where python3 < 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 with

        Thread 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.

        1 Reply Last reply
        0
        • D Donald Duck
          18 Nov 2021, 15:26

          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.

          E Offline
          E Offline
          eyllanesc
          wrote on 18 Nov 2021, 16:13 last edited by eyllanesc
          #3

          @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)

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          J D 3 Replies Last reply 18 Nov 2021, 16:16
          3
          • E eyllanesc
            18 Nov 2021, 16:13

            @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)

            J Offline
            J Offline
            JonB
            wrote on 18 Nov 2021, 16:16 last edited by JonB
            #4

            @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.)

            E 1 Reply Last reply 18 Nov 2021, 16:21
            0
            • J JonB
              18 Nov 2021, 16:16

              @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.)

              E Offline
              E Offline
              eyllanesc
              wrote on 18 Nov 2021, 16:21 last edited by
              #5

              @JonB My comment has 2 parts,

              1. 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.
              2. 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.

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              1 Reply Last reply
              1
              • E eyllanesc
                18 Nov 2021, 16:13

                @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)

                J Offline
                J Offline
                JonB
                wrote on 18 Nov 2021, 16:22 last edited by JonB
                #6

                @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 the QApplication. 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!

                E 1 Reply Last reply 18 Nov 2021, 16:24
                0
                • J JonB
                  18 Nov 2021, 16:22

                  @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 the QApplication. 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!

                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 18 Nov 2021, 16:24 last edited by eyllanesc
                  #7

                  @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)
                  

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  J 1 Reply Last reply 18 Nov 2021, 16:29
                  0
                  • E eyllanesc
                    18 Nov 2021, 16:24

                    @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)
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 18 Nov 2021, 16:29 last edited by JonB
                    #8

                    @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 go python3 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.

                    1 Reply Last reply
                    0
                    • E eyllanesc
                      18 Nov 2021, 16:13

                      @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)

                      D Offline
                      D Offline
                      Donald Duck
                      wrote on 18 Nov 2021, 16:40 last edited by Donald Duck
                      #9

                      @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.

                      J 1 Reply Last reply 18 Nov 2021, 16:47
                      0
                      • D Donald Duck
                        18 Nov 2021, 16:40

                        @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.

                        J Offline
                        J Offline
                        JonB
                        wrote on 18 Nov 2021, 16:47 last edited by
                        #10

                        @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 Reply Last reply
                        1

                        1/10

                        18 Nov 2021, 15:26

                        • Login

                        • Login or register to search.
                        1 out of 10
                        • First post
                          1/10
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved