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 exiting with exit code 255 when trying to run python script

Qprocess exiting with exit code 255 when trying to run python script

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessqt c++python
22 Posts 4 Posters 5.5k Views
  • 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 J.Hilk
    2 Apr 2024, 08:09

    @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() or applicationDirPath() 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");
    
    A Offline
    A Offline
    Adithya
    wrote on 2 Apr 2024, 08:20 last edited by
    #13

    @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.h

    I reallt checked multiple time .The oath is not the issue . You are not gettiing the € this time :p

    1 Reply Last reply
    0
    • J jsulm
      2 Apr 2024, 08:08

      @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.

      A Offline
      A Offline
      Adithya
      wrote on 2 Apr 2024, 08:27 last edited by Adithya 4 Feb 2024, 08:29
      #14

      @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

      J 1 Reply Last reply 2 Apr 2024, 08:33
      0
      • A Adithya
        2 Apr 2024, 08:27

        @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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 2 Apr 2024, 08:33 last edited by
        #15

        @Adithya Please share your current code, so we can see what exactly you're trying now.
        Also try with an absolute path to the Python executable to make sure that is not the issue.

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

        A 1 Reply Last reply 2 Apr 2024, 08:43
        0
        • A Adithya
          2 Apr 2024, 06:35

          @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/bin

          So ideally python should take from PATH

          J Offline
          J Offline
          JonB
          wrote on 2 Apr 2024, 08:41 last edited by JonB 4 Feb 2024, 08:42
          #16

          @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 named python?

          #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() on python complains No such file or directory under Ubuntu 22.04, as I would expect.

          Changing to process.start("python3", ...); produces no such error and runs the python3 interpreter....

          1 Reply Last reply
          3
          • J jsulm
            2 Apr 2024, 08:33

            @Adithya Please share your current code, so we can see what exactly you're trying now.
            Also try with an absolute path to the Python executable to make sure that is not the issue.

            A Offline
            A Offline
            Adithya
            wrote on 2 Apr 2024, 08:43 last edited by
            #17

            @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")

            J J 2 Replies Last reply 2 Apr 2024, 08:45
            0
            • A Adithya
              2 Apr 2024, 08:43

              @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")

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 2 Apr 2024, 08:45 last edited by
              #18

              @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...

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

              1 Reply Last reply
              0
              • A Adithya
                2 Apr 2024, 08:43

                @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")

                J Offline
                J Offline
                JonB
                wrote on 2 Apr 2024, 08:48 last edited by JonB 4 Feb 2024, 08:54
                #19

                @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.

                A 1 Reply Last reply 2 Apr 2024, 09:02
                2
                • J JonB
                  2 Apr 2024, 08:48

                  @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.

                  A Offline
                  A Offline
                  Adithya
                  wrote on 2 Apr 2024, 09:02 last edited by Adithya 4 Feb 2024, 09:04
                  #20

                  @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::Running

                  But any idea why I am not getting the log .

                  J 1 Reply Last reply 2 Apr 2024, 09:05
                  0
                  • A Adithya
                    2 Apr 2024, 09:02

                    @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::Running

                    But any idea why I am not getting the log .

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 2 Apr 2024, 09:05 last edited by
                    #21

                    @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.

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

                    1 Reply Last reply
                    2
                    • A Offline
                      A Offline
                      Adithya
                      wrote on 2 Apr 2024, 09:13 last edited by
                      #22

                      Thanks guys it worked. python3 and readyReadStandardOutput(showed the log ,was expecting it to work like qDebig() and cout .My bad) . Sorry for bothering with wrong misconception.

                      1 Reply Last reply
                      1
                      • A Adithya has marked this topic as solved on 2 Apr 2024, 09:13

                      22/22

                      2 Apr 2024, 09:13

                      • Login

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