Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. Python-Script ausführen

Python-Script ausführen

Scheduled Pinned Locked Moved Unsolved German
10 Posts 3 Posters 2.1k 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.
  • N Offline
    N Offline
    NotYourFan
    wrote on 2 Jul 2019, 07:26 last edited by
    #1

    Hey,

    ich würde gerne nach einem Button-click ein Python Skript ausführen.
    Dieses Skript erzeugt eine JSON-Datei und speichert mir diese in D:
    Folgendes habe ich probiert:

    void TestWindow::loadFile()
        QProcess p;
        QStringList params;
    
        params << "createJSON.py";
        p.start("python", params);
        p.waitForFinished(-1);
        QString p_stdout = p.readAll();
    

    Leider wird das Skript nicht ausgeführt (meine JSON-Datei wird nicht aktualisiert)
    Sieht jemand meinen Fehler ? bzw. weis weshalb mein Code nicht funktioniert?

    J 1 Reply Last reply 2 Jul 2019, 07:32
    0
    • N NotYourFan
      2 Jul 2019, 07:26

      Hey,

      ich würde gerne nach einem Button-click ein Python Skript ausführen.
      Dieses Skript erzeugt eine JSON-Datei und speichert mir diese in D:
      Folgendes habe ich probiert:

      void TestWindow::loadFile()
          QProcess p;
          QStringList params;
      
          params << "createJSON.py";
          p.start("python", params);
          p.waitForFinished(-1);
          QString p_stdout = p.readAll();
      

      Leider wird das Skript nicht ausgeführt (meine JSON-Datei wird nicht aktualisiert)
      Sieht jemand meinen Fehler ? bzw. weis weshalb mein Code nicht funktioniert?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 2 Jul 2019, 07:32 last edited by
      #2

      hi @NotYourFan

      welches Betriebssystem?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NotYourFan
        wrote on 2 Jul 2019, 07:38 last edited by NotYourFan 7 Feb 2019, 07:38
        #3

        hi @J-Hilk
        Windows 10

        Edit:
        Folgendes habe ich ebne auch nochmal im Internet gefunden und versucht, leider ohne Erfolg:

        QProcess process;
         
                process.setWorkingDirectory("C:/Users/…/Desktop/Project");
                process.setProgram("creatJSON.py");
                process.start("createJSON.py");
                process.waitForFinished(1000);
        
        J 1 Reply Last reply 2 Jul 2019, 08:00
        0
        • N NotYourFan
          2 Jul 2019, 07:38

          hi @J-Hilk
          Windows 10

          Edit:
          Folgendes habe ich ebne auch nochmal im Internet gefunden und versucht, leider ohne Erfolg:

          QProcess process;
           
                  process.setWorkingDirectory("C:/Users/…/Desktop/Project");
                  process.setProgram("creatJSON.py");
                  process.start("createJSON.py");
                  process.waitForFinished(1000);
          
          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 2 Jul 2019, 08:00 last edited by
          #4

          @NotYourFan
          Ich bin keine Experte für Win10&QProcess aber ich bin mir ziemlich sicher das du die Endung exe nicht unterschlagen darfst beim Programm Aufruf


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NotYourFan
            wrote on 2 Jul 2019, 08:08 last edited by
            #5

            wo bzw. welche Endung mit .exe?

            J 1 Reply Last reply 2 Jul 2019, 08:12
            0
            • N NotYourFan
              2 Jul 2019, 08:08

              wo bzw. welche Endung mit .exe?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 2 Jul 2019, 08:12 last edited by J.Hilk 7 Feb 2019, 08:13
              #6

              @NotYourFan

              p.start("python.exe", params);

              und das ist unter der vorraussetzung das Python in deiner Path variable hinterlegt ist!

              Wenn nicht, wird das auch nicht gefunden/ausgeführt

              du solltest auf das error signal achten und auswerten
              https://doc.qt.io/qt-5/qprocess.html#errorOccurred


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • N Offline
                N Offline
                NotYourFan
                wrote on 2 Jul 2019, 09:11 last edited by
                #7

                Hey,
                eben Python als Umgebnungsvariable angelegt (Wenn ich CMD-Öffne --> "python --Version" eingebe, dann erhalte ich 3.7.3) also funktioniert die Umgebungsvariable.
                Leider hat es auch mit deinem Vorschlag ( @J-Hilk ) nicht funktioniert.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NotYourFan
                  wrote on 2 Jul 2019, 10:12 last edited by NotYourFan 7 Feb 2019, 10:13
                  #8

                  Hey,

                  eben folgendes Probiert um die Fehlersuche einzugrenzen:

                  QProcess p;
                      QStringList params;
                  
                      params << "C:/Users/.../Desktop/test.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;
                  
                  

                  Ausgabe:

                  1. Process started!
                  2. Error occurred "Process crashed"
                  3. OUT ""
                  4. ERR "Fatal Python error: initfsencoding: unable to load the file system codec\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00005b50 (most recent call first):\n"

                  Hab eben auch nach "No module named 'encodings' "gegoogelt" aber leider nicht schlauer geworden. Vielleicht kennt sich ja jemand von euch damit aus :D

                  J 1 Reply Last reply 2 Jul 2019, 11:23
                  0
                  • N NotYourFan
                    2 Jul 2019, 10:12

                    Hey,

                    eben folgendes Probiert um die Fehlersuche einzugrenzen:

                    QProcess p;
                        QStringList params;
                    
                        params << "C:/Users/.../Desktop/test.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;
                    
                    

                    Ausgabe:

                    1. Process started!
                    2. Error occurred "Process crashed"
                    3. OUT ""
                    4. ERR "Fatal Python error: initfsencoding: unable to load the file system codec\nModuleNotFoundError: No module named 'encodings'\n\nCurrent thread 0x00005b50 (most recent call first):\n"

                    Hab eben auch nach "No module named 'encodings' "gegoogelt" aber leider nicht schlauer geworden. Vielleicht kennt sich ja jemand von euch damit aus :D

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 2 Jul 2019, 11:23 last edited by jsulm 7 Feb 2019, 11:33
                    #9

                    @NotYourFan Funktioniert dein Script wenn du ihn direkt ausführst?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      NotYourFan
                      wrote on 2 Jul 2019, 11:32 last edited by
                      #10

                      Ja das tut es komischerweise ohne Probleme.

                      1 Reply Last reply
                      0

                      3/10

                      2 Jul 2019, 07:38

                      7 unread
                      • Login

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