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. With QProcess startet python script not working
QtWS25 Last Chance

With QProcess startet python script not working

Scheduled Pinned Locked Moved Solved General and Desktop
qprocess
10 Posts 4 Posters 1.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.
  • F Offline
    F Offline
    firen
    wrote on 9 Aug 2021, 17:32 last edited by firen 8 Sept 2021, 17:42
    #1

    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!

    E J 2 Replies Last reply 9 Aug 2021, 17:55
    0
    • F firen
      9 Aug 2021, 18:00

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

      E Offline
      E Offline
      eyllanesc
      wrote on 9 Aug 2021, 18:20 last edited by
      #8

      @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

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 9 Aug 2021, 17:37 last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        F 1 Reply Last reply 9 Aug 2021, 18:00
        1
        • F firen
          9 Aug 2021, 17:32

          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!

          E Offline
          E Offline
          eyllanesc
          wrote on 9 Aug 2021, 17:55 last edited by
          #3

          @firen Recommendation: Never but never use relative paths, if you can't use explicit absolute paths then build the absolute path. For example test.csv is a relative path, now the big question is, with respect to which directory?

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          1
          • S SGaist
            9 Aug 2021, 17:37

            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.

            F Offline
            F Offline
            firen
            wrote on 9 Aug 2021, 18:00 last edited by
            #4

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

            E 1 Reply Last reply 9 Aug 2021, 18:20
            0
            • F firen
              9 Aug 2021, 17:32

              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!

              J Offline
              J Offline
              JonB
              wrote on 9 Aug 2021, 18:01 last edited by
              #5

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

              F 1 Reply Last reply 9 Aug 2021, 18:03
              0
              • J JonB
                9 Aug 2021, 18:01

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

                F Offline
                F Offline
                firen
                wrote on 9 Aug 2021, 18:03 last edited by
                #6

                @JonB sorry my "problem" is/was that the output files were not where i expected them (see my other answer)

                :-)

                J 1 Reply Last reply 9 Aug 2021, 18:22
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Aug 2021, 18:17 last edited by
                  #7

                  Does your Python script contain only hardcoded paths ?
                  Are they all relative ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • F firen
                    9 Aug 2021, 18:00

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

                    E Offline
                    E Offline
                    eyllanesc
                    wrote on 9 Aug 2021, 18:20 last edited by
                    #8

                    @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

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    1 Reply Last reply
                    1
                    • F firen
                      9 Aug 2021, 18:03

                      @JonB sorry my "problem" is/was that the output files were not where i expected them (see my other answer)

                      :-)

                      J Offline
                      J Offline
                      JonB
                      wrote on 9 Aug 2021, 18:22 last edited by
                      #9

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

                      F 1 Reply Last reply 9 Aug 2021, 19:00
                      0
                      • J JonB
                        9 Aug 2021, 18:22

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

                        F Offline
                        F Offline
                        firen
                        wrote on 9 Aug 2021, 19:00 last edited by
                        #10

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

                        1 Reply Last reply
                        0

                        8/10

                        9 Aug 2021, 18:20

                        • Login

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