Important: Please read the 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+""  << arg1 << arg2<< arg3;
    my_process->start("python", arguments);

    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/ param1 \"param2 with spaces\"");

    better yet, make sure the pythong script is executable and execute it directly with
    proc.start("/dir/ 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 (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:


    and i changed it to


    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() << ""  << arg1 << arg2<< arg3;
    my_process->startDetached("python", arguments, path);

    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/, and maybe this is the reason why that didn't work.

    Well, just a hyphotesis. Thanks for all replys.

Log in to reply