0

i want to run (after i click a button) a .py Script

I already tried the following Code

    QProcess p;
    QStringList params;

    params << "createJSON.py";
    p.start("python.exe", params);
    p.waitForFinished(-1);
    QString p_stdout = p.readAll();

My Python Script create a JSON-File when it runs successfully. So i can see if the runs successfully.

Kirikkayis
  • 83
  • 7

1 Answers1

0

I have been able to write a more detailed version of your code.

    QProcess p;
    QStringList params;

    params << "createJSON.py";
    QObject::connect(&p, &QProcess::started, []() {
        qInfo() << "Process started!";
    });
    QObject::connect(&p, &QProcess::errorOccurred, [&p]() {
        qWarning() << "Error occurred" << p.errorString();
    });

    p.start("python.exe", params);
    p.waitForFinished(-1);
    QString p_stdout = p.readAllStandardOutput();
    QString p_stderr = p.readAllStandardError();

    qDebug() << "OUT" << p_stdout;
    qDebug() << "ERR" << p_stderr;

This effectively leads to an error. In my case, I get the following:

Process started!

OUT ""

ERR "python.exe: can't open file 'createJSON.py': [Errno 2] No such file or directory\n"

It may be different in your case. Either way, using the errorOccurred signal along with the errorString method will allow you to debug cases where the process actually cannot start. Reading stderr will allow you to debug cases where the process starts, but does not run as expected.

Community
  • 1
  • 1
Thomas Kowalski
  • 1,995
  • 4
  • 20
  • 42
  • Thank you. Now i get: 1. "Process starded!" 2. "Error occured: Process crashed" 3. "OUT "" " 3. ERR "Fatal Python error: initsfseconding: unable to load the file System codec\nModuleNotFoundError: No module named 'encodings'\n Current thread 0x0000005834 (most recent call first): \n – Kirikkayis Jul 02 '19 at 09:54
  • I dont have anything like "enconding" … – Kirikkayis Jul 02 '19 at 09:58
  • Two things. First of all, try invoking the full Python path (if you have several Python versions, that could be the reason): `p.start("C:/Python36/python.exe")` instead of just `"python.exe"`. You may find help [here](https://stackoverflow.com/questions/38132755/importerror-no-module-named-encodings) too (even though the original topic isn't related, many people are posting answers for the issue you're facing). – Thomas Kowalski Jul 02 '19 at 11:12
  • Thank you for your answer, i reinstalled python with the configuration ("Add Python to PATH") and i also make what you say: p.start("C:/…/Python37-32/python.exe, params) but i get the same error. – Kirikkayis Jul 02 '19 at 11:31
  • Stupid question, but have you tried rebooting your computer? – Thomas Kowalski Jul 02 '19 at 11:58
  • yes, same Problem … i reinstalled python now the 3x time and reboot the Computer again – Kirikkayis Jul 02 '19 at 12:14
  • Have you tried using [setProcessEnvironment](https://doc.qt.io/qt-5/qprocess.html#setProcessEnvironment) with [systemEnvironment()](https://doc.qt.io/qt-5/qprocessenvironment.html#systemEnvironment)? – Thomas Kowalski Jul 02 '19 at 12:26
  • Yes: QProcessEnviroment env = QProcessEnviroment::systemEnviroment() then: p.setProcessEnviroment(env) – Kirikkayis Jul 02 '19 at 12:43