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.6k 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.
  • AdithyaA Adithya

    Hi @J-Hilk , This is not the issue path. the py file preset in the specified path

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #12

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

    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    AdithyaA 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @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");
      
      AdithyaA Offline
      AdithyaA Offline
      Adithya
      wrote on 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
      • jsulmJ jsulm

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

        AdithyaA Offline
        AdithyaA Offline
        Adithya
        wrote on last edited by Adithya
        #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

        jsulmJ 1 Reply Last reply
        0
        • AdithyaA Adithya

          @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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

          AdithyaA 1 Reply Last reply
          0
          • AdithyaA Adithya

            @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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #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
            • jsulmJ jsulm

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

              AdithyaA Offline
              AdithyaA Offline
              Adithya
              wrote on 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")

              jsulmJ JonBJ 2 Replies Last reply
              0
              • AdithyaA Adithya

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                • AdithyaA Adithya

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

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #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.

                  AdithyaA 1 Reply Last reply
                  2
                  • JonBJ JonB

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

                    AdithyaA Offline
                    AdithyaA Offline
                    Adithya
                    wrote on last edited by Adithya
                    #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 .

                    jsulmJ 1 Reply Last reply
                    0
                    • AdithyaA Adithya

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

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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
                      • AdithyaA Offline
                        AdithyaA Offline
                        Adithya
                        wrote on 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
                        • AdithyaA Adithya has marked this topic as solved on

                        • Login

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