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 can't start python script
Forum Updated to NodeBB v4.3 + New Features

QProcess can't start python script

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.9k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    testV
    wrote on last edited by
    #1

    I am using QT and trying to call a python script

    QProcess *my_process= new QProcess;
    QString path = QCoreApplication::applicationDirPath()+"/PASTE/";
    QStringList arguments = QStringList() << path+"script.py"  << arg1 << arg2<< arg3;
    my_process->start("python", arguments);
    my_process->waitForFinished();
    

    The problem is that the python script doesn't run, and if i instead use startDetached() it actually runs, but i need to use start() to because later in the code i need to use a finished signal, and startDetached() can't emit this signal.

    So i want to run the script with start(), but it isn't working. Can anyone help me?

    JonBJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      try the other overloaded version of start(QString& program)
      It's most likely a problem parsing the interpretter and script parameters.
      proc.start("python /dir/myscript.py param1 \"param2 with spaces\"");

      better yet, make sure the pythong script is executable and execute it directly with
      proc.start("/dir/myscript.py param1 \"param2 with spaces\""); which works in nix but I'm not sure if you need to preface with the interpretter in windoze.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Kent-Dorfman said in QProcess can't start python script:

        try the other overloaded version of start(QString& program)
        It's most likely a problem parsing the interpretter and script parameters.

        No, I doubt this since the overloaded version is worse and tries to figure out the params.
        The version the OP is using is correct since then all parameters are correctly passed to the executable, there is no interpreter involved.

        Take a look on what QProcess:exitCode()/exitStatus()/error()/readAllStandarError()/... is telling you. Never simply start a process and expect all is going well. There are return values and error functions for a reason - that you use them.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        4
        • T testV

          I am using QT and trying to call a python script

          QProcess *my_process= new QProcess;
          QString path = QCoreApplication::applicationDirPath()+"/PASTE/";
          QStringList arguments = QStringList() << path+"script.py"  << arg1 << arg2<< arg3;
          my_process->start("python", arguments);
          my_process->waitForFinished();
          

          The problem is that the python script doesn't run, and if i instead use startDetached() it actually runs, but i need to use start() to because later in the code i need to use a finished signal, and startDetached() can't emit this signal.

          So i want to run the script with start(), but it isn't working. Can anyone help me?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @testV
          I don't know why startDetached() does work (if indeed it really does) while start() does not. As @Christian-Ehrlicher has said, before you do anything else you must test/capture error codes/stderr etc.

          Given that you say it does work with startDetached the following may not be relevant, but:

          • If you are under Windows you are passing the path with /s not \s on the command line. That may or may not work, depending on the receiving script. I would always recommend using https://doc.qt.io/qt-5/qdir.html#toNativeSeparators (for all OSes) on any argument which you know to be a path name.

          • OTOH if you are under Linux, python command will invoke Python2 interpreter. If you intend to use Python3, you must specify python3 instead.

          1 Reply Last reply
          3
          • T Offline
            T Offline
            testV
            wrote on last edited by
            #5

            I found out what was the problem...

            in the python code i had the following line:

            savefig("./archives/"+name+'.pdf')
            

            and i changed it to

            savefig(os.path.dirname(os.path.abspath(__file__))+"/archives/"+name+'.pdf')
            

            And then it all worked.

            Don't know exactly why this worked, but i have a theory. My startDetached code was like this:

            QProcess *my_process= new QProcess;
            QString path = QCoreApplication::applicationDirPath()+"/PASTE";
            QStringList arguments = QStringList() << "script.py"  << arg1 << arg2<< arg3;
            my_process->startDetached("python", arguments, path);
            my_process->waitForFinished();
            

            so then the python got the QCoreApplication::applicationDirPath()+"/PASTE as the path.

            When i called just start, the path that python got was QCoreApplication::applicationDirPath()+"/PASTE/script.py, and maybe this is the reason why that didn't work.

            Well, just a hyphotesis. Thanks for all replys.

            1 Reply Last reply
            1

            • Login

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