QProcess seems not to work with python calls
-
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,
-
@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 runningpythonw.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. Maybepythonw.exe
is trying to tell you something? -
@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.
-
@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...
-
@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?
-
@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.
-
-
@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,