With QProcess startet python script not working
-
Hi everyone,
I am trying to start from c++/qt a python script.
In some other thread here there was a suggestion to do this with QProcess. The python script is doing some stuff and at the end is writing the results into some csv-files.
My problem is, that the script is started correctly (I can see that in the debug output) , but the final step ( the reading and writing of the csv-files) is not done.
My simplified c++ code is
QProcess* process = new QProcess(); QStringList arguments; arguments << getWorkingPath() + "/Feedback_App/Analysis_files/writeByPython.py" << "11"; process->start("python", arguments); if(!process->waitForFinished(10000)) { qDebug() << "Error: " <<process->errorString(); return false; } else { qDebug() << "Analyzing done. " << process->readAll(); emit analysingFinised(QString::number(weeksDiff)); return true; } return false;
And the simplified python code:
import datetime import sys def main(): argsList = sys.argv[1:] print(argsList[0]) print("Yes startet") newFile = open('test.csv','w') newFile.write("Test123") newFile.close() print("Yes end") if __name__ == "__main__": main()
The output in qt-creator is:
Analyzing done. "11\r\nYes startet\r\nYes end\r\n"
Is it not possible that the python script writes to files, or what am I missing? Even this simplified version is not working so there must be something general wrong...
Starting from console is no problem, only from Qt/QProcess it is not working!
Would there be another simple solution if QProcess is not a good idea?
Thanks!
-
@firen The explanation is simple, the relative path is relative to where the executable is, and where is the executable? Well, in the folder that you indicate. In the real application I don't think you will continue using QtCreator so you will see the .csv next to the executable
-
Hi,
Where are you looking for for the file ?
If you want to unsure it writes at a given place (for testing purposes) use a canonical path so you know where it is.
-
@SGaist ok now i found the files from the simplified version, they are all in the
"build-Feedback_App-Desktop_Qt_5_15_2_MinGW_64_bit-Debug" folder, not in the folder where the *.qml etc. are.Thanks so far! :-)
In the real version it is also working if I put everything to this folder and start it from there.
But that is realy ugly in my opinion, because that is not a good place for that or?How can I change this, so that I dont have to put it in the Debug folder? I cant change the python script so I have to do it from c++ side...
-
@firen said in With QProcess startet python script not working:
My problem is, that the script is started correctly (I can see that in the debug output) , but the final step ( the reading and writing of the csv-files) is not done.
In addition to what my colleagues said. All your code shows is that the Python script writes a line to an external file. Your calling program doesn't do any reading of that file, so what is exactly is "not working" here?
-
Does your Python script contain only hardcoded paths ?
Are they all relative ? -
@firen The explanation is simple, the relative path is relative to where the executable is, and where is the executable? Well, in the folder that you indicate. In the real application I don't think you will continue using QtCreator so you will see the .csv next to the executable
-
@firen
Yeah, my other two colleagues managed to gather that, apparently, but I did not! :)As they say: your current working directory when it works from a console will be the same for both, so where the Python script is. But when you are in Qt Creator it is elsewhere, like the project or executable directory. You can set that in Creator for when it runs your program. But best is as the others say: use an absolute location for the path to your script. See also QStandardPaths Class, and the choices in enum QStandardPaths::StandardLocation.
-
@JonB Thanks. I like the QStandardPaths::StandardLocation but I did a deploy and after that I put just everythink in the folder with the *.exe file like @eyllanesc suggested and it works fine.
I think my problem was that i missunderstood QProcess. I thought it is just a trigger to start the program and it is only important to know, where the path of the .py is.
But thanks guys anyway :-)