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
Forum Updated to NodeBB v4.3 + New Features

Run python script in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
31 Posts 9 Posters 16.7k Views 3 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.
  • 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 Online
      J Online
      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 Offline
        J Offline
        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

                  P J J 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

                    P Offline
                    P 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
                    • N NotYourFan
                      3 Jul 2019, 11:58

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

                      J Online
                      J Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on 3 Jul 2019, 13:54 last edited by
                      #21

                      @NotYourFan It doesn't work like this. You have to put the command as first parameter to start() and all parameters for the command as parameter list.

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

                      G 1 Reply Last reply 3 Jul 2019, 14:08
                      1
                      • J jsulm
                        3 Jul 2019, 13:54

                        @NotYourFan It doesn't work like this. You have to put the command as first parameter to start() and all parameters for the command as parameter list.

                        G Offline
                        G Offline
                        Gojir4
                        wrote on 3 Jul 2019, 14:08 last edited by
                        #22

                        @jsulm Are you sure about that ?
                        from Qt Doc:

                        QProcess process;
                        process.start("del /s *.txt");
                        
                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          NotYourFan
                          wrote on 4 Jul 2019, 05:37 last edited by NotYourFan 7 Apr 2019, 05:38
                          #23

                          @jsulm i readed in the Qt Doc.
                          @Gojir4 you are right.
                          @Pablo-J-Rogina thx for your Suggestion … but i should / want to do it with QProcess …

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 4 Jul 2019, 06:52 last edited by
                            #24

                            Hi,

                            @NotYourFan just in case, did you try to compare the environment variables of your QProcess object and the one you have when running e.g. from a terminal ?

                            @Gojir4 You are right about that specific overload, but the documentation also states that this overload should be avoided if possible.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            G 1 Reply Last reply 4 Jul 2019, 07:24
                            2
                            • S SGaist
                              4 Jul 2019, 06:52

                              Hi,

                              @NotYourFan just in case, did you try to compare the environment variables of your QProcess object and the one you have when running e.g. from a terminal ?

                              @Gojir4 You are right about that specific overload, but the documentation also states that this overload should be avoided if possible.

                              G Offline
                              G Offline
                              Gojir4
                              wrote on 4 Jul 2019, 07:24 last edited by
                              #25

                              @SGaist My bad. Thanks for clarification !

                              1 Reply Last reply
                              0
                              • N NotYourFan
                                3 Jul 2019, 11:58

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

                                J Offline
                                J Offline
                                JonB
                                wrote on 4 Jul 2019, 07:34 last edited by
                                #26

                                @NotYourFan
                                I am lost as to where you are now.

                                Assuming it's still crashing --- is that right? --- surely you want to break it down to simplify now. Start with:

                                    params << "-V";
                                    p.start("C:\\development\\Python\\Python37\\python.exe", params);
                                    p.waitForFinished(-1);
                                    QString p_stdout = p.readAll();
                                

                                What exactly happens?

                                1 Reply Last reply
                                2
                                • N Offline
                                  N Offline
                                  NotYourFan
                                  wrote on 4 Jul 2019, 09:10 last edited by
                                  #27

                                  @JonB, i get a error Message: ….

                                  ModuleNotFoundError: No module named 'encodings'
                                  
                                  J 1 Reply Last reply 4 Jul 2019, 09:18
                                  0
                                  • N NotYourFan
                                    4 Jul 2019, 09:10

                                    @JonB, i get a error Message: ….

                                    ModuleNotFoundError: No module named 'encodings'
                                    
                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 4 Jul 2019, 09:18 last edited by JonB 7 Apr 2019, 09:19
                                    #28

                                    @NotYourFan
                                    OK! So, you can forget about your createJSON.py script and anything it does.

                                    I don't know why you get this when spawning from Qt but not when running your python.exe directly from a command prompt. Either it's something to do with it sending output to a pipe you are reading from, or it's to do with your general Python environment being different between the two situations.

                                    You want now to Google for python ModuleNotFoundError: No module named 'encodings'. There are posts/suggestions. Looks to me like it might be to do with whether you are using a virtual environment or not, but I'm sure one of the hits will sort you out....

                                    A 1 Reply Last reply 4 Jul 2019, 09:23
                                    3
                                    • J JonB
                                      4 Jul 2019, 09:18

                                      @NotYourFan
                                      OK! So, you can forget about your createJSON.py script and anything it does.

                                      I don't know why you get this when spawning from Qt but not when running your python.exe directly from a command prompt. Either it's something to do with it sending output to a pipe you are reading from, or it's to do with your general Python environment being different between the two situations.

                                      You want now to Google for python ModuleNotFoundError: No module named 'encodings'. There are posts/suggestions. Looks to me like it might be to do with whether you are using a virtual environment or not, but I'm sure one of the hits will sort you out....

                                      A Offline
                                      A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on 4 Jul 2019, 09:23 last edited by
                                      #29

                                      @JonB I can imagine that the Environment inside Qt Creator is different (e.g. PATH) and therefore other (incompatible) executables and libraries are found.

                                      @NotYourFan: You should carefully check the Run Environment variables inside Qt Creator, especially PATH. That's all I can say for now.

                                      Regards

                                      Qt has to stay free or it will die.

                                      1 Reply Last reply
                                      3
                                      • N Offline
                                        N Offline
                                        NotYourFan
                                        wrote on 4 Jul 2019, 09:33 last edited by
                                        #30

                                        @JonB, when i run my createJSON.py from the console, everything working fine.
                                        @aha_1980 thanks !! I will checked immediately.

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          NotYourFan
                                          wrote on 4 Jul 2019, 09:40 last edited by
                                          #31

                                          @aha_1980 !!!!!
                                          Thank you :=)

                                          That was the Problem.
                                          I use for "Debug" MinGW (MinGW included python2.7 and set a PYTHON variable)
                                          So now i tried like this:

                                          QProcess cmd;
                                          QProcessEnviroment env = QProcessnviroment::systemEnviroment();
                                          QProcessEnviroment envUpdate;
                                          
                                          envUpdate.inser("PATH", env.value("PATH"));
                                          cmd.setProcessEnviroment(envUpdate)
                                          cmd.start("python.exe C:\\Users\\...\\Desktop\\...\\createJSON.py")
                                          cmd.waitForFinished();
                                          

                                          Now it works :)

                                          Thank you @all for your standby

                                          1 Reply Last reply
                                          5
                                          • J JCBaraza referenced this topic on 27 Nov 2023, 22:48

                                          21/31

                                          3 Jul 2019, 13:54

                                          • Login

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