Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Run python script in Qt

Run python script in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
31 Posts 9 Posters 16.6k Views
  • 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.
  • N Offline
    N Offline
    NotYourFan
    wrote on 3 Jul 2019, 05:38 last edited by NotYourFan 7 Mar 2019, 05:38
    #1

    Hey,

    im trying to run a python Script in Qt after clicking a button, with the following Code:

    void TestWindow::loadFile()
        QProcess p;
        QStringList params;
    
        params << "createJSON.py";
        p.start("python.exe", params);
        p.waitForFinished(-1);
        QString p_stdout = p.readAll();
    

    i get the following error:

    1. Process started!
    2. Error occurred "Process crashed"
    3. OUT ""
    4. ERR "Fatal Python error: initfsencoding: unable to load the file system codec\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00005b50 (most recent call first):\n"

    If i run the script in my python IDE it works fine.
    My "createJSON.py" create a JSON-File so i can checked if the Qt run my script successfully.

    Do you have any idea how i can run otherwise a python script in qt?

    1 Reply Last reply
    1
    • C Offline
      C Offline
      closx
      wrote on 3 Jul 2019, 05:59 last edited by
      #2

      I think it is a internal python error.
      According to Jayground said here, you can try to check Add Python to environment variables selection on advanced options when custom installation of the Python.

      bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
      tag me (like @closx) if you are answering to me, so I can notice :D

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NotYourFan
        wrote on 3 Jul 2019, 06:05 last edited by
        #3

        i reinstalled python and checked the variables … all fine.
        And as i said, when i run the Script with cmd ( cmd -> python.exe "path/createJSON.py") it works perfect

        J 1 Reply Last reply 3 Jul 2019, 06:07
        0
        • N NotYourFan
          3 Jul 2019, 06:05

          i reinstalled python and checked the variables … all fine.
          And as i said, when i run the Script with cmd ( cmd -> python.exe "path/createJSON.py") it works perfect

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 3 Jul 2019, 06:07 last edited by
          #4

          @NotYourFan
          well, have you tried giving QProcess the absolute path to the python file, as you did in your cmd prompt ?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • N Offline
            N Offline
            NotYourFan
            wrote on 3 Jul 2019, 07:33 last edited by
            #5

            yes of coruse, like:

            void TestWindow::loadFile()
                QProcess p;
                QStringList params;
            
                params << "createJSON.py";
                p.start("C:/development/Python/Python37/python.exe", params);
                p.waitForFinished(-1);
                QString p_stdout = p.readAll();
            
            J 1 Reply Last reply 3 Jul 2019, 07:37
            0
            • N NotYourFan
              3 Jul 2019, 07:33

              yes of coruse, like:

              void TestWindow::loadFile()
                  QProcess p;
                  QStringList params;
              
                  params << "createJSON.py";
                  p.start("C:/development/Python/Python37/python.exe", params);
                  p.waitForFinished(-1);
                  QString p_stdout = p.readAll();
              
              J Online
              J Online
              J.Hilk
              Moderators
              wrote on 3 Jul 2019, 07:37 last edited by J.Hilk 7 Mar 2019, 07:37
              #6

              @NotYourFan said in Run python script in Qt:
              I actually meant the absolute path to

              createJSON.py

              I assume createJSON.py is inside your project folder and you start the program via QtCreator but, the created exe is not in your project folder -> so it can't find the python script


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • N Offline
                N Offline
                NotYourFan
                wrote on 3 Jul 2019, 08:11 last edited by
                #7

                Sry @J-Hilk i missunderstood.

                But i try this also …

                like:

                void TestWindow::loadFile()
                    QProcess p;
                    QStringList params;
                
                    params << "C:/development/Qt/ProjektXY/createJSON.py";
                    p.start("C:/development/Python/Python37/python.exe", params);
                    p.waitForFinished(-1);
                    QString p_stdout = p.readAll();
                

                i´m desperately … i dont know what to do, i try everything.

                J 1 Reply Last reply 3 Jul 2019, 08:20
                0
                • N NotYourFan
                  3 Jul 2019, 08:11

                  Sry @J-Hilk i missunderstood.

                  But i try this also …

                  like:

                  void TestWindow::loadFile()
                      QProcess p;
                      QStringList params;
                  
                      params << "C:/development/Qt/ProjektXY/createJSON.py";
                      p.start("C:/development/Python/Python37/python.exe", params);
                      p.waitForFinished(-1);
                      QString p_stdout = p.readAll();
                  

                  i´m desperately … i dont know what to do, i try everything.

                  J Online
                  J Online
                  J.Hilk
                  Moderators
                  wrote on 3 Jul 2019, 08:20 last edited by
                  #8

                  @NotYourFan since you're on windows, you're using the wrong path separator


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  J 1 Reply Last reply 3 Jul 2019, 10:35
                  0
                  • N Offline
                    N Offline
                    NotYourFan
                    wrote on 3 Jul 2019, 08:22 last edited by
                    #9

                    @J-Hilk whats the Right path separator?

                    J 1 Reply Last reply 3 Jul 2019, 08:25
                    0
                    • N NotYourFan
                      3 Jul 2019, 08:22

                      @J-Hilk whats the Right path separator?

                      J Online
                      J Online
                      J.Hilk
                      Moderators
                      wrote on 3 Jul 2019, 08:25 last edited by J.Hilk 7 Mar 2019, 08:27
                      #10

                      @NotYourFan
                      https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words


                      backslash or probably double backslash


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NotYourFan
                        wrote on 3 Jul 2019, 08:26 last edited by
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          NotYourFan
                          wrote on 3 Jul 2019, 08:29 last edited by
                          #12

                          okay, thx for your Suggestion.

                          void TestWindow::loadFile()
                              QProcess p;
                              QStringList params;
                          
                              params << "C:\\development\\Qt\\ProjektXY\\createJSON.py";
                              p.start("C:\\development\\Python\\Python37\\python.exe", params);
                              p.waitForFinished(-1);
                              QString p_stdout = p.readAll();
                          

                          i tried one time with Backslash and one time with double Backslash … same Problem.

                          1 Reply Last reply
                          0
                          • J J.Hilk
                            3 Jul 2019, 08:20

                            @NotYourFan since you're on windows, you're using the wrong path separator

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 3 Jul 2019, 10:35 last edited by
                            #13

                            @J.Hilk said in Run python script in Qt:

                            since you're on windows, you're using the wrong path separator

                            With Qt / should work on Windows as well, shouldn't it?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            J A 2 Replies Last reply 3 Jul 2019, 10:45
                            0
                            • J jsulm
                              3 Jul 2019, 10:35

                              @J.Hilk said in Run python script in Qt:

                              since you're on windows, you're using the wrong path separator

                              With Qt / should work on Windows as well, shouldn't it?

                              J Online
                              J Online
                              J.Hilk
                              Moderators
                              wrote on 3 Jul 2019, 10:45 last edited by
                              #14

                              @jsulm I'm unsure

                              usually yes, but in this case you're trying to start a System program from the OS. Until now, I've always only used it to start an external program, that I shipped with the other Qt installation and therefore was always at the same depth as the main executable or it was on a unix system...

                              @NotYourFan
                              have you seen this thread ?
                              https://stackoverflow.com/questions/15127047/qt-calling-external-python-script

                              It may help you


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              A 1 Reply Last reply 3 Jul 2019, 10:47
                              0
                              • J jsulm
                                3 Jul 2019, 10:35

                                @J.Hilk said in Run python script in Qt:

                                since you're on windows, you're using the wrong path separator

                                With Qt / should work on Windows as well, shouldn't it?

                                A Offline
                                A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on 3 Jul 2019, 10:46 last edited by
                                #15

                                @jsulm

                                With Qt / should work on Windows as well, shouldn't it?

                                It is really important to use forward slashes for Qt functions, yes. However, the parameter for python (the path to the script) is not evaluated from Qt but from the python interpreter, so here backslashes might be needed.

                                That gives:

                                params << "C:\\development\\Qt\\ProjektXY\\createJSON.py";
                                p.start("C:/Development/Python/Python37/python.exe", params);
                                

                                Looks funny, though.

                                p.waitForFinished(-1); I would use a SLOT here.

                                And for testing: can you just try python -c 'print "Hello World"' first?

                                Qt has to stay free or it will die.

                                1 Reply Last reply
                                3
                                • J J.Hilk
                                  3 Jul 2019, 10:45

                                  @jsulm I'm unsure

                                  usually yes, but in this case you're trying to start a System program from the OS. Until now, I've always only used it to start an external program, that I shipped with the other Qt installation and therefore was always at the same depth as the main executable or it was on a unix system...

                                  @NotYourFan
                                  have you seen this thread ?
                                  https://stackoverflow.com/questions/15127047/qt-calling-external-python-script

                                  It may help you

                                  A Offline
                                  A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on 3 Jul 2019, 10:47 last edited by
                                  #16

                                  @J.Hilk said in Run python script in Qt:

                                  usually yes, but in this case you're trying to start a System program from the OS

                                  As said, the path for QProcess should work with forward slash. The path for the parameter (the script) probably not.

                                  Qt has to stay free or it will die.

                                  1 Reply Last reply
                                  0
                                  • N Offline
                                    N Offline
                                    NotYourFan
                                    wrote on 3 Jul 2019, 11:03 last edited by
                                    #17

                                    @J-Hilk thank you for the stackoverflow link, i've seen it and try it.
                                    @aha_1980 i test python -c 'print "Hello World"' --> i get Nothing.

                                    A 1 Reply Last reply 3 Jul 2019, 11:32
                                    0
                                    • N NotYourFan
                                      3 Jul 2019, 11:03

                                      @J-Hilk thank you for the stackoverflow link, i've seen it and try it.
                                      @aha_1980 i test python -c 'print "Hello World"' --> i get Nothing.

                                      A Offline
                                      A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on 3 Jul 2019, 11:32 last edited by
                                      #18

                                      @NotYourFan said in Run python script in Qt:

                                      @aha_1980 i test python -c 'print "Hello World"' --> i get Nothing.

                                      How did you test it?

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      0
                                      • N Offline
                                        N Offline
                                        NotYourFan
                                        wrote on 3 Jul 2019, 11:58 last edited by
                                        #19

                                        put python -c 'print "Hello World"' in p.start

                                        Pablo J. RoginaP J JonBJ 3 Replies Last reply 3 Jul 2019, 12:29
                                        0
                                        • N NotYourFan
                                          3 Jul 2019, 11:58

                                          put python -c 'print "Hello World"' in p.start

                                          Pablo J. RoginaP Offline
                                          Pablo J. RoginaP Offline
                                          Pablo J. Rogina
                                          wrote on 3 Jul 2019, 12:29 last edited by
                                          #20

                                          @NotYourFan just in case, you may want to use Python embedded in your Qt application...

                                          Upvote the answer(s) that helped you solve the issue
                                          Use "Topic Tools" button to mark your post as Solved
                                          Add screenshots via postimage.org
                                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                          1 Reply Last reply
                                          0

                                          7/31

                                          3 Jul 2019, 08:11

                                          topic:navigator.unread, 24
                                          • Login

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