Qprocess exiting with exit code 255 when trying to run python script
-
@Adithya What does https://doc.qt.io/qt-6/qprocess.html#error return? On what platform are you? Is Python executable in PATH?
-
@jsulm Hi , the start() exit code is 255 and I am not using execute() . I am trying this in ubuntu 22 qt c++ .Python interpreter is in the path /usr/bin/python3.exe
echo $PATH
/opt/ros/noetic/bin:/home/adithya/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/binSo ideally python should take from PATH
-
@Adithya said in Qprocess exiting with exit code 255 when trying to run python script:
Can you please answer my first question?
"Python interpreter is in the path /usr/bin/python3.exe" - in your code you're using "python" not python3 - is there a "python" executable?
-
Exit code 255 generally indicates that the program couldn't start. This can happen due to various reasons.
in this case, I think its a typical case of a wrong path.
currentPath() should be the location of your generated executable, not your source files.
Check thatQDir::currentPath() + "/Tools/script/main.py"
is actually a valid file and the correct file. -
@jsulm I dont have python.exe , but I have python3.exe .I changed process.start("python", arguments); to process.start("python3", arguments); aswell .I was not getting the log from py file but there was no 255 exit code . But -2 when I used QProcess::execute().
-
@Adithya I will ask for the third time (I will stop here if you do not answer): What does https://doc.qt.io/qt-6/qprocess.html#error return?
-
@Adithya Then maybe your Python script exits with -2?
You can also connect a slot to https://doc.qt.io/qt-6/qprocess.html#stateChanged and log state changes.
And you can also connect slots to https://doc.qt.io/qt-6/qprocess.html#readyReadStandardError and https://doc.qt.io/qt-6/qprocess.html#readyReadStandardOutput and print out stderr/stdout from your process there. -
@Adithya did you really check, or did you just type this answer ?
If I had a € for every time I heard "ThE PaTh Is CoRrEcT!!111eleven" and it turns out it's not, because
currentPath()
orapplicationDirPath()
etc. did not actually point to the assumed directory, I would have a surprising amount of money :D.qDebug() << (QDir::currentPath() + "/Tools/script/main.py");
-
@J-Hilk // Path to the Python script you want to execute
QString pythonScript = QDir::currentPath() + "/Tools/Extractor_OD/main_od_for_rci.py";
qDebug() << "Script Path = " <<pythonScript;Log : Script Path = "/home/adithya/DVT/DVT-3.0/DistanceVerificationTool/Tools/Extractor_OD/main_od_for_rci.py"
adithya@adithya:~/DVT/DVT-3.0/DistanceVerificationTool/Tools/Extractor_OD$ pwd
/home/adithya/DVT/DVT-3.0/DistanceVerificationTool/Tools/Extractor_OD
adithya@adithya:~/DVT/DVT-3.0/DistanceVerificationTool/Tools/Extractor_OD$ ls
copy_script RCI_Front_LUT.h RCI_Rear_LUT.h src
main_od_for_rci.py RCI_Left_LUT.h RCI_Right_LUT.hI reallt checked multiple time .The oath is not the issue . You are not gettiing the € this time :p
-
@jsulm I tried using stateChanged something like this
QObject::connect(&process, &QProcess::stateChanged, [](QProcess::ProcessState newState) {
qDebug() << "Process = "<< newState;
});
Log :Process = QProcess::Starting
ERROR: QProcess::UnknownError
Process = QProcess::NotRunning
Failed to start Python process
Python script execution failed with exit code: 255 -
@Adithya said in Qprocess exiting with exit code 255 when trying to run python script:
I am trying this in ubuntu 22 qt c++ .Python interpreter is in the path /usr/bin/python3.exe
You are under Ubuntu/Linux and the executable you want to run ends in
.exe
, right? Even though that is for Windows? So you can type/usr/bin/python3.exe
in a terminal and that executes fine, right?That aside. Since the Ubuntu executable is indeed
python3
why are you trying to start a process namedpython
?#include <QCoreApplication> #include <QDebug> #include <QProcess> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QProcess process; process.start("python", QStringList()); QObject::connect(&process, &QProcess::errorOccurred, &process, [&process]() { qDebug() << process.errorString(); } ); if (!process.waitForStarted()) { qDebug() << "Failed to start Python process"; return 1; } process.waitForFinished(); int exitCode = process.exitCode(); if (exitCode != 0) { qDebug() << "Python script execution failed with exit code:" << exitCode; return 1; } return a.exec(); }
shows me
"execvp: No such file or directory" Failed to start Python process
So
execvp()
onpython
complainsNo such file or directory
under Ubuntu 22.04, as I would expect.Changing to
process.start("python3", ...);
produces no such error and runs thepython3
interpreter.... -
@jsulm
//trying it in main funtion itself for now , have commented everything else.
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);// Path to the Python interpreter QString pythonInterpreter = "/usr/bin/python3.exe"; // or the full path to python.exe // Path to the directory containing your Python script and other files QString workingDirectory = QDir::currentPath() + "/Tools/Extractor_OD"; qDebug() << "Path = " <<workingDirectory; // Path to the Python script you want to execute QString pythonScript = QDir::currentPath() + "/Tools/Extractor_OD/main_od_for_rci.py"; qDebug() << "Script Path = " <<pythonScript; // Arguments to pass to the Python script QStringList arguments; arguments << pythonScript << "1" << "2"; QString command = pythonScript + " 1 2"; // Create a QProcess instance QProcess process; QObject::connect(&process, &QProcess::stateChanged, [](QProcess::ProcessState newState) { qDebug() << "Process = "<< newState; }); // Set the working directory //process.setWorkingDirectory(workingDirectory); // Set the command to execute process.start(pythonInterpreter, arguments); QProcess::ProcessError error = process.error(); qDebug() << "ERROR: " << error; if (!process.waitForStarted()) { qDebug() << "Failed to start Python process"; //return 1; } // Wait for the process to finish (optional) process.waitForFinished(); // Get the exit code of the process int exitCode = process.exitCode(); // Handle the exit code if needed if (exitCode != 0) { qDebug() << "Python script execution failed with exit code:" << exitCode; //return 1; } return a.exec();
}
//Have commented out everything just trying to log for now
main_od_for_rci.py
if name == 'main':
print("Hey there") -
@Adithya said in Qprocess exiting with exit code 255 when trying to run python script:
/usr/bin/python3.exe
This is for sure wrong.
You're on Linux, not Windows... -
@Adithya said in Qprocess exiting with exit code 255 when trying to run python script:
QString pythonInterpreter = "/usr/bin/python3.exe"; // or the full path to python.exe
You read my answer where it said
.exe
is for Windows not Linux, didn't you? What happened when you tried my:So you can type
/usr/bin/python3.exe
in a terminal and that executes fine, right?In a terminal please copy & paste:
ls -l /usr/bin/python3.exe
and paste the output here.
-
@JonB @jsulm using python3 actually worked .But not sure whether the script is being executed or not
I changed .py file something like this (running in infinite loop)if name == 'main':
while True:
print("Hey there")The process is still running
Log :
Process = QProcess::Starting
ERROR: QProcess::UnknownError
Process = QProcess::RunningBut any idea why I am not getting the log .
-
@Adithya said in Qprocess exiting with exit code 255 when trying to run python script:
running in infinite loop
Please do NOT do such infinite loops in an event driven frameworks like Qt! There is really no need for that and it blocks Qt event loop.
"I am not getting the log" - please explain how you're getting this log and what do you actually mean exactly by "log". If you mean the output of your script then I already suggested to read its stderr/stdout.