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. QProcess seems not to work with python calls
Forum Updated to NodeBB v4.3 + New Features

QProcess seems not to work with python calls

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 654 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.
  • J Offline
    J Offline
    JCBaraza
    wrote on 26 Nov 2023, 18:57 last edited by
    #1

    Hi all,

    I'm trying to execute a batch file created with a tool I've developed. I'm using Qt Creator 4.9.1 ( Qt 5.12.3 ) in Windows 10.

    The code in the batch file (let's name "mybatch.bat", which is located in the same directory as "myscript.py" file) is:

    cd mypath1
    pythonw myscript.py > mylog.log
    cd mypath2
    

    The "cd" commands are to change the working directory from that of the tool to the one containing the script and batch file, and then return to the original one. Note that I run pythonw.exe instead of python.exe because I want it to run in background.

    If I either execute "mybatch.bat" from the MS-DOS prompt:

    mypath2\> mypath1\mybatch
    

    or double-click "mybatch.bat" in Windows File Explorer, the batch executes correctly.

    But, if I run from my tool:

    ...
    QString local_macro_bat = " 'mypath1' " + "\\mybatch.bat";
    ...
    QProcess proc;
    proc.start(local_macro_bat);
    proc.waitForFinished(-1);
    proc.close();
    

    The directories do change correctly, but the call to pythonw does nothing. Not even the output redirection (" > mylog.log") works, as no "mylog.log" file is created.

    I've tried other possibilities to run the batch file and the python script (obviously, the "execute" below replace "start" + "waitForFinished" + "close"):

    proc.start("\"" + local_macro_bat + "\"");
    
    proc.setWorkingDirectory(" 'mypath1' ");
    proc.start("pythonw.exe", QStringList() << "myscript.py > mylog.log"); 
    
    proc.execute(local_macro_bat);
    
    proc.execute("\"" + local_macro_bat + "\"");
    
    proc.setWorkingDirectory(" 'mypath1' ");
    proc.execute("pythonw.exe", QStringList() << "myscript.py > mylog.log"); 
    

    and the call to pythonw is always ignored, even if I remove the output redirection.

    What am I doing wrong? I've been running batch files and external applications (7x.exe, modelsim.exe, etc) in my tool using QProcess in some of these ways for many years, and I have never experienced such problem.

    Note that python is in the path (that's why it works when executed from the prompt and Explorer).

    In case the problem is my knowledge of Python (I'm novel in it), I should clarify that "myscript.py" is not the real script that I want to run. Indeed, I want to run another script ("myrealscript.py"), located in "mypath1\myrealpath1", and the contents of "myscript.py" is:

    import myrealpath1.myrealscript
    

    Any ideas? Thanks in advance,

    J 1 Reply Last reply 26 Nov 2023, 19:09
    0
    • J JCBaraza
      26 Nov 2023, 18:57

      Hi all,

      I'm trying to execute a batch file created with a tool I've developed. I'm using Qt Creator 4.9.1 ( Qt 5.12.3 ) in Windows 10.

      The code in the batch file (let's name "mybatch.bat", which is located in the same directory as "myscript.py" file) is:

      cd mypath1
      pythonw myscript.py > mylog.log
      cd mypath2
      

      The "cd" commands are to change the working directory from that of the tool to the one containing the script and batch file, and then return to the original one. Note that I run pythonw.exe instead of python.exe because I want it to run in background.

      If I either execute "mybatch.bat" from the MS-DOS prompt:

      mypath2\> mypath1\mybatch
      

      or double-click "mybatch.bat" in Windows File Explorer, the batch executes correctly.

      But, if I run from my tool:

      ...
      QString local_macro_bat = " 'mypath1' " + "\\mybatch.bat";
      ...
      QProcess proc;
      proc.start(local_macro_bat);
      proc.waitForFinished(-1);
      proc.close();
      

      The directories do change correctly, but the call to pythonw does nothing. Not even the output redirection (" > mylog.log") works, as no "mylog.log" file is created.

      I've tried other possibilities to run the batch file and the python script (obviously, the "execute" below replace "start" + "waitForFinished" + "close"):

      proc.start("\"" + local_macro_bat + "\"");
      
      proc.setWorkingDirectory(" 'mypath1' ");
      proc.start("pythonw.exe", QStringList() << "myscript.py > mylog.log"); 
      
      proc.execute(local_macro_bat);
      
      proc.execute("\"" + local_macro_bat + "\"");
      
      proc.setWorkingDirectory(" 'mypath1' ");
      proc.execute("pythonw.exe", QStringList() << "myscript.py > mylog.log"); 
      

      and the call to pythonw is always ignored, even if I remove the output redirection.

      What am I doing wrong? I've been running batch files and external applications (7x.exe, modelsim.exe, etc) in my tool using QProcess in some of these ways for many years, and I have never experienced such problem.

      Note that python is in the path (that's why it works when executed from the prompt and Explorer).

      In case the problem is my knowledge of Python (I'm novel in it), I should clarify that "myscript.py" is not the real script that I want to run. Indeed, I want to run another script ("myrealscript.py"), located in "mypath1\myrealpath1", and the contents of "myscript.py" is:

      import myrealpath1.myrealscript
      

      Any ideas? Thanks in advance,

      J Offline
      J Offline
      JonB
      wrote on 26 Nov 2023, 19:09 last edited by JonB
      #2

      @JCBaraza said in QProcess seems not to work with python calls:

      proc.start("pythonw.exe", QStringList() << "myscript.py > mylog.log");

      You can't do stuff like this.

      If you are having trouble with .bat file and/or redirection (> etc.) run as:

      proc.start("cmd.exe", { "/c", "pythonw myscript.py > mylog.log" } );
      proc.start("cmd.exe", { "/c", "/path/to/bat_file [args] " } );
      

      Make sure you know whether it makes any difference what the current directory is, for redirection and the cd of relative paths in your script.

      Note that QProcess::setWorkingDirectory() sets the working directory once the command is found, just be aware.

      First get it working for the base Python command (so that's not an issue), then introduce whatever your .bat files are doing. Your querstion is complicated by mixing both of these issues, if there is a problem running pythonw.exe or something, sort that out separately from .bat files.

      Try specifying the full path to pythonw.exe.

      Importantly, put slots on errorrOccurred & finished, and do print out anything received on stdout/err. Maybe pythonw.exe is trying to tell you something?

      J J 2 Replies Last reply 26 Nov 2023, 20:15
      1
      • J JonB
        26 Nov 2023, 19:09

        @JCBaraza said in QProcess seems not to work with python calls:

        proc.start("pythonw.exe", QStringList() << "myscript.py > mylog.log");

        You can't do stuff like this.

        If you are having trouble with .bat file and/or redirection (> etc.) run as:

        proc.start("cmd.exe", { "/c", "pythonw myscript.py > mylog.log" } );
        proc.start("cmd.exe", { "/c", "/path/to/bat_file [args] " } );
        

        Make sure you know whether it makes any difference what the current directory is, for redirection and the cd of relative paths in your script.

        Note that QProcess::setWorkingDirectory() sets the working directory once the command is found, just be aware.

        First get it working for the base Python command (so that's not an issue), then introduce whatever your .bat files are doing. Your querstion is complicated by mixing both of these issues, if there is a problem running pythonw.exe or something, sort that out separately from .bat files.

        Try specifying the full path to pythonw.exe.

        Importantly, put slots on errorrOccurred & finished, and do print out anything received on stdout/err. Maybe pythonw.exe is trying to tell you something?

        J Offline
        J Offline
        JCBaraza
        wrote on 26 Nov 2023, 20:15 last edited by
        #3

        @JonB , thanks for your quick answer.

        Well, according to the "stuff", it was just another desperate try...

        As I mention in my post, the batch file is correct, as it runs correctly when run standalone from the prompt and explorer. Hence, also the call to pythonw in it, and the indirect call to the real python script are correct.

        But I don't understant why when running the batch file as a QProcess, the python call doesn't work anymore.

        I thought that the most logical (and simplest and easiest) way to do was to start or execute (" 'mypath1' + "\mybatch.bat"), but it seems I was wrong...

        I've tried your suggestion:

        proc.start("cmd.exe", { "/c", "/path/to/bat_file [args] " } );
        

        and the result is the same: nothing about python call.

        Oh, I've used exitCode and exitStatus to get the result of the batch execution, but the process seems to finish ok. I guess that the pythonw call doesn't crash the batch execution, as the subsequent cd is executed correctly (I've verified that point...).

        I'tried using the full path to Pythonw in the batch file, but no changes...

        Probably, the problem is with python itself (well, with my knowledge of Python, I mean), but I can't guess what the problem is.

        I'll keep thinking...

        Thanks again, anyway.

        J 1 Reply Last reply 26 Nov 2023, 20:38
        0
        • J JCBaraza
          26 Nov 2023, 20:15

          @JonB , thanks for your quick answer.

          Well, according to the "stuff", it was just another desperate try...

          As I mention in my post, the batch file is correct, as it runs correctly when run standalone from the prompt and explorer. Hence, also the call to pythonw in it, and the indirect call to the real python script are correct.

          But I don't understant why when running the batch file as a QProcess, the python call doesn't work anymore.

          I thought that the most logical (and simplest and easiest) way to do was to start or execute (" 'mypath1' + "\mybatch.bat"), but it seems I was wrong...

          I've tried your suggestion:

          proc.start("cmd.exe", { "/c", "/path/to/bat_file [args] " } );
          

          and the result is the same: nothing about python call.

          Oh, I've used exitCode and exitStatus to get the result of the batch execution, but the process seems to finish ok. I guess that the pythonw call doesn't crash the batch execution, as the subsequent cd is executed correctly (I've verified that point...).

          I'tried using the full path to Pythonw in the batch file, but no changes...

          Probably, the problem is with python itself (well, with my knowledge of Python, I mean), but I can't guess what the problem is.

          I'll keep thinking...

          Thanks again, anyway.

          J Offline
          J Offline
          JonB
          wrote on 26 Nov 2023, 20:38 last edited by JonB
          #4

          @JCBaraza
          I don't see where you have established whether running the pythonw executable at all is the issue or whether it is (trying to) interpret your .py script that is not behaving as you expect.

          J 1 Reply Last reply 26 Nov 2023, 20:55
          0
          • J JonB
            26 Nov 2023, 20:38

            @JCBaraza
            I don't see where you have established whether running the pythonw executable at all is the issue or whether it is (trying to) interpret your .py script that is not behaving as you expect.

            J Offline
            J Offline
            JCBaraza
            wrote on 26 Nov 2023, 20:55 last edited by
            #5

            @JonB said in QProcess seems not to work with python calls:

            I don't see where you have established whether running the pythonw executable at all is the issue or whether it is (trying to) interpret your .py script that is not behaving as you expect.

            Well, if the batch file (and the python direct and indirect calls) works properly when executed standalone (outside my application), and if I can run using QProcess other batch files not calling python(w), so I assume that the problem must be when executing with QProcess a batch file that does contains a call to python(w).

            So, I guess that I should change something in my batch file so that python(w) runs when executing the batch file from a QProcess. But I still don't know what...

            J 1 Reply Last reply 26 Nov 2023, 21:20
            0
            • J JCBaraza
              26 Nov 2023, 20:55

              @JonB said in QProcess seems not to work with python calls:

              I don't see where you have established whether running the pythonw executable at all is the issue or whether it is (trying to) interpret your .py script that is not behaving as you expect.

              Well, if the batch file (and the python direct and indirect calls) works properly when executed standalone (outside my application), and if I can run using QProcess other batch files not calling python(w), so I assume that the problem must be when executing with QProcess a batch file that does contains a call to python(w).

              So, I guess that I should change something in my batch file so that python(w) runs when executing the batch file from a QProcess. But I still don't know what...

              J Offline
              J Offline
              JonB
              wrote on 26 Nov 2023, 21:20 last edited by
              #6

              @JCBaraza
              As mentioned earlier, where do you gather any output which may be produced while executing the batch file? If the cd line you showed or the pythonw line produced, say, an error message, how would we know?

              J 1 Reply Last reply 26 Nov 2023, 22:09
              0
              • J JonB
                26 Nov 2023, 21:20

                @JCBaraza
                As mentioned earlier, where do you gather any output which may be produced while executing the batch file? If the cd line you showed or the pythonw line produced, say, an error message, how would we know?

                J Offline
                J Offline
                JCBaraza
                wrote on 26 Nov 2023, 22:09 last edited by JCBaraza
                #7

                @JonB , that's a good question. I don't know how to get such information. I've redirected the output of the execution of the batch file, in case I could receive any information about the result of the execution of each line in the batch file.:

                proc.start(local_macro_bat + " > " + Design_Path + "\\LOG.log");
                

                This is the REAL output:

                D:\VFIT3-Qt5\build-VFIT_Mth_NC-Desktop_Qt_5_13_0_MinGW_64_bit-Debug>cd "D:__MiniLenet\MiniLenet_Python"

                D:__MiniLenet\MiniLenet_Python>pythonw InjectionScript_inj__TESTS_Test5.py 1>InjectionScript_inj__TESTS_Test5.log

                D:__MiniLenet\MiniLenet_Python>cd "D:\VFIT3-Qt5\build-VFIT_Mth_NC-Desktop_Qt_5_13_0_MinGW_64_bit-Debug"

                It's simply the shadow of the three commands run in the batch file, with no longer information in case of the pythonw call.

                It's just like python ignoring the call, but I don't know why?

                1 Reply Last reply
                0
                • J JonB
                  26 Nov 2023, 19:09

                  @JCBaraza said in QProcess seems not to work with python calls:

                  proc.start("pythonw.exe", QStringList() << "myscript.py > mylog.log");

                  You can't do stuff like this.

                  If you are having trouble with .bat file and/or redirection (> etc.) run as:

                  proc.start("cmd.exe", { "/c", "pythonw myscript.py > mylog.log" } );
                  proc.start("cmd.exe", { "/c", "/path/to/bat_file [args] " } );
                  

                  Make sure you know whether it makes any difference what the current directory is, for redirection and the cd of relative paths in your script.

                  Note that QProcess::setWorkingDirectory() sets the working directory once the command is found, just be aware.

                  First get it working for the base Python command (so that's not an issue), then introduce whatever your .bat files are doing. Your querstion is complicated by mixing both of these issues, if there is a problem running pythonw.exe or something, sort that out separately from .bat files.

                  Try specifying the full path to pythonw.exe.

                  Importantly, put slots on errorrOccurred & finished, and do print out anything received on stdout/err. Maybe pythonw.exe is trying to tell you something?

                  J Offline
                  J Offline
                  JonB
                  wrote on 26 Nov 2023, 22:23 last edited by JonB
                  #8

                  @JonB said in QProcess seems not to work with python calls:

                  Importantly, put slots on errorrOccurred & finished, and do print out anything received on stdout/err. Maybe pythonw.exe is trying to tell you something?

                  See QProcess::readAll...() methods.

                  proc.waitForFinished();
                  qDebug() << proc.readAll();
                  

                  will suffice here.

                  J 1 Reply Last reply 27 Nov 2023, 22:48
                  1
                  • J JCBaraza has marked this topic as solved on 27 Nov 2023, 22:45
                  • J JonB
                    26 Nov 2023, 22:23

                    @JonB said in QProcess seems not to work with python calls:

                    Importantly, put slots on errorrOccurred & finished, and do print out anything received on stdout/err. Maybe pythonw.exe is trying to tell you something?

                    See QProcess::readAll...() methods.

                    proc.waitForFinished();
                    qDebug() << proc.readAll();
                    

                    will suffice here.

                    J Offline
                    J Offline
                    JCBaraza
                    wrote on 27 Nov 2023, 22:48 last edited by
                    #9

                    @JonB , thank you for your interest.

                    I've found an old post where a similar problem to mine was posed (Run python script in Qt).

                    In that case, a user wanted to run Python directly from a QProcess, while I intend to run indirectly (through a batch file). But the problem was exactly the same in both cases, and hence @NotYourFan 's solution has worked for me too.

                    Regards,

                    1 Reply Last reply
                    0

                    1/9

                    26 Nov 2023, 18:57

                    • Login

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