Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProcess can't start python script



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



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


  • Qt Champions 2019

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



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



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


Log in to reply