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. How I can run python script from QT
Forum Updated to NodeBB v4.3 + New Features

How I can run python script from QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 2.8k 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.
  • WaseeW Wasee

    Re: Run bash script from qt Hi everyone;
    How I can run python script from qt Code?

    Thanks

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

    @Wasee
    As @Christian-Ehrlicher has written, but depending on your version of Linux you may need to specify python3 to run Python 3 rather than 2....

    1 Reply Last reply
    0
    • WaseeW Wasee

      Re: Run bash script from qt Hi everyone;
      How I can run python script from qt Code?

      Thanks

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #4

      @Wasee To add a bit: if your script is configured (chmod +x and shebang) to run independently you can also use the same methodology as for running the bash script (bash will call interpreter defined in the shebang) - sometimes you might need shell environment for your script. It all depends on what the script does.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • WaseeW Offline
        WaseeW Offline
        Wasee
        wrote on last edited by
        #5

        Hi everyone;
        I coded following but did not run script;

           
            QProcess process;
            process.startDetached("/bin/python", QStringList() << "-c" << "/home/user/bashScripts/test.py");
            qDebug()<<"Welcome";
        

        thanks

        JonBJ 1 Reply Last reply
        0
        • WaseeW Offline
          WaseeW Offline
          Wasee
          wrote on last edited by
          #6

          Hi everyone;
          I need to pass two argument with script to with qt code. These argument entered from the linedit etc.
          thanks

          artwawA 1 Reply Last reply
          0
          • WaseeW Wasee

            Hi everyone;
            I need to pass two argument with script to with qt code. These argument entered from the linedit etc.
            thanks

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #7

            @Wasee
            Do you really need it detached?
            Anyway, arguments can be bundled further down the line:

            process.startDetached("/bin/python", QStringList({"-c","/home/user/bashScripts/test.py",ui->lineEdit->text().simplified()}));
            

            and so on.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            0
            • WaseeW Wasee

              Hi everyone;
              I coded following but did not run script;

                 
                  QProcess process;
                  process.startDetached("/bin/python", QStringList() << "-c" << "/home/user/bashScripts/test.py");
                  qDebug()<<"Welcome";
              

              thanks

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

              @Wasee said in How I can run python script from QT:

              process.startDetached("/bin/python", QStringList() << "-c" << "/home/user/bashScripts/test.py");

              As I stated earlier, is /bin/python correct for your desired Python on your machine?

              Since when has Python used a -c argument to run a script? Did you try /bin/python -c /home/user/bashScripts/test.py from the command-line first? Remove that -c argument?

              If it still does not work, check for errors and any output to stdout/stderr from QProcess.

              artwawA 1 Reply Last reply
              0
              • JonBJ JonB

                @Wasee said in How I can run python script from QT:

                process.startDetached("/bin/python", QStringList() << "-c" << "/home/user/bashScripts/test.py");

                As I stated earlier, is /bin/python correct for your desired Python on your machine?

                Since when has Python used a -c argument to run a script? Did you try /bin/python -c /home/user/bashScripts/test.py from the command-line first? Remove that -c argument?

                If it still does not work, check for errors and any output to stdout/stderr from QProcess.

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #9

                @JonB said in How I can run python script from QT:

                Since when has Python used a -c argument to run a script?

                https://docs.python.org/3/using/cmdline.html

                seems legit? I assumed the OP knows how to call an interpreter and used the parameter deliberately.

                For more information please re-read.

                Kind Regards,
                Artur

                JonBJ 1 Reply Last reply
                0
                • WaseeW Offline
                  WaseeW Offline
                  Wasee
                  wrote on last edited by
                  #10

                  @artwaw said in How I can run python script from QT:

                  process.startDetached("/bin/python", QStringList({"-c","/home/user/bashScripts/test.py",ui->lineEdit->text().simplified()}));

                  Didn't run script by changing the code.

                  JonBJ 1 Reply Last reply
                  0
                  • artwawA artwaw

                    @JonB said in How I can run python script from QT:

                    Since when has Python used a -c argument to run a script?

                    https://docs.python.org/3/using/cmdline.html

                    seems legit? I assumed the OP knows how to call an interpreter and used the parameter deliberately.

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

                    @artwaw
                    Nope, you mis-read what is there!

                    python  [-c command]
                    

                    runs command as as python, not as a python file of script.

                    As it says there, to run a script file it's

                    python myscript.py
                    

                    I imagine the OP copied the -c directly from the example of the Bash script, which does use -c for a file:

                    /bin/bash -c myscript.sh
                    

                    :)

                    1 Reply Last reply
                    2
                    • WaseeW Wasee

                      @artwaw said in How I can run python script from QT:

                      process.startDetached("/bin/python", QStringList({"-c","/home/user/bashScripts/test.py",ui->lineEdit->text().simplified()}));

                      Didn't run script by changing the code.

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

                      @Wasee said in How I can run python script from QT:

                      process.startDetached("/bin/python", QStringList({"-c","/home/user/bashScripts/test.py",ui->lineEdit->text().simplified()}));
                      Didn't run script by changing the code.

                      Already told you why earlier, what you should test, and what you need to have. Any particular reason to just ignore me?

                      1 Reply Last reply
                      0
                      • WaseeW Offline
                        WaseeW Offline
                        Wasee
                        wrote on last edited by
                        #13

                        @JonB Hi;
                        I run in command-line following commands: command not found
                        user@user-HP:~$ sudo /bin/python -c /home/user/bashScripts/test.py
                        [sudo] password for user:
                        sudo: /bin/python: command not found
                        user@user-HP:~$ sudo /bin/python /home/user/bashScripts/test.py
                        [sudo] password for user:
                        sudo: /bin/python: command not found
                        user@user-HP:~$

                        thanks

                        1 Reply Last reply
                        0
                        • WaseeW Offline
                          WaseeW Offline
                          Wasee
                          wrote on last edited by
                          #14

                          @JonB said in How I can run python script from QT:

                          python myscript.py

                          Its work perfect now I want to pass two arguments with my script from UI basically two different integers.

                          thanks

                          1 Reply Last reply
                          0
                          • WaseeW Offline
                            WaseeW Offline
                            Wasee
                            wrote on last edited by
                            #15

                            Hi everyone;
                            I coded following my script run successfully:

                            process.startDetached("python /home/user/bashScripts/test.py");
                            

                            But when I pass arguments then my script fails to run ; I coded following script as argument:

                            process.startDetached(({"python /home/user/bashScripts/test.py",(ui->pan->text().simplified()),(ui->tilt->text().simplified());}));
                            

                            thanks

                            jsulmJ 1 Reply Last reply
                            0
                            • WaseeW Wasee

                              Hi everyone;
                              I coded following my script run successfully:

                              process.startDetached("python /home/user/bashScripts/test.py");
                              

                              But when I pass arguments then my script fails to run ; I coded following script as argument:

                              process.startDetached(({"python /home/user/bashScripts/test.py",(ui->pan->text().simplified()),(ui->tilt->text().simplified());}));
                              

                              thanks

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @Wasee Please read QProcess documentation!
                              And also please read what @JonB gave you!

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

                              1 Reply Last reply
                              2

                              • Login

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