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 to use/execute a Python script inside QtCreator C++ project
Forum Updated to NodeBB v4.3 + New Features

How to use/execute a Python script inside QtCreator C++ project

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 25.5k Views 2 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 21 Oct 2017, 18:24 last edited by
    #5

    @Niagarer

    should take a text file, change some things and save it

    I was wondering
    why you want to use another language to do it to process a file?
    Is it something python is really, really good at why not just do it in c++?

    N 1 Reply Last reply 21 Oct 2017, 19:26
    0
    • M mrjj
      21 Oct 2017, 18:24

      @Niagarer

      should take a text file, change some things and save it

      I was wondering
      why you want to use another language to do it to process a file?
      Is it something python is really, really good at why not just do it in c++?

      N Offline
      N Offline
      Niagarer
      wrote on 21 Oct 2017, 19:26 last edited by Niagarer
      #6

      @mrjj
      Yes, working with text in Python is just wonderful and fun. In C++ not really.
      You just have to focus too much on the syntax in C++, in Python I just can focus on the algorithms and not have to worry about hundrets of casts of objects and stuff.

      M 1 Reply Last reply 21 Oct 2017, 19:30
      0
      • N Niagarer
        21 Oct 2017, 19:26

        @mrjj
        Yes, working with text in Python is just wonderful and fun. In C++ not really.
        You just have to focus too much on the syntax in C++, in Python I just can focus on the algorithms and not have to worry about hundrets of casts of objects and stuff.

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 21 Oct 2017, 19:30 last edited by
        #7

        @Niagarer
        Well if you worked more with python, its natural.
        I used c++ for 30 years so for me the syntax is nice and i find python
        less easy to read and very distracting with all that Self all over.

        But what kind of text processing is it ?
        Qt offers regular expression and other advanced features for matching etc?

        N 1 Reply Last reply 21 Oct 2017, 20:36
        0
        • M mrjj
          21 Oct 2017, 19:30

          @Niagarer
          Well if you worked more with python, its natural.
          I used c++ for 30 years so for me the syntax is nice and i find python
          less easy to read and very distracting with all that Self all over.

          But what kind of text processing is it ?
          Qt offers regular expression and other advanced features for matching etc?

          N Offline
          N Offline
          Niagarer
          wrote on 21 Oct 2017, 20:36 last edited by Niagarer
          #8

          @mrjj
          Yes, the self is ugly :D
          But in C++ I have to spend so much time to all the systax stuff and spending hours and hours for finding any tiny missing symbol anywhere in the program, what is hard to find because there are soo many >.<
          I find Python much more usable for all kinds of algorithmic things.
          I want to go through a text file, which contains some custom macro-language-like keywords like

          if(%CONTIDION1%){
          }
          

          into a real condition in this case, which I stored elsewhere (at the bottom or in another file) like

          if(a == b){
          }
          

          Yes, but I also just can use C++, if there are good functions, I just wanted to do something in the program in Python, because I like it (except the self, yes, but I would not use OOP in this case).

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 21 Oct 2017, 21:32 last edited by
            #9

            Well if a given tool makes you more productive, there is no reason not to use it.
            i also use scripting languages for tasks that is more handy without having to compile it for each change etc.

            The only minus is that if you use python for the text processing , you must also include that
            if you deploy to other machines. On linux its easy to get python but
            on windows, there might be path issue etc when u try to call it.
            And user have to install it.

            Not sure you can just have it your folder and call it there but it is of course solvable.

            N 1 Reply Last reply 22 Oct 2017, 08:19
            1
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 22 Oct 2017, 01:30 last edited by Chris Kawa
              #10

              And now imagine this hypothetical conversation of a dev(il) and a (l)user ;)

              DEV: Hi user! Try my new app, it does stuff!
              USER: Great! I need stuff.
              ...
              USER: Um, it crashes/hangs/does nothing when I hit the "do stuff" button.
              DEV: Oh, can you [long and boring instructions to get logs, debug etc.]?
              ...
              DEV. Right. You don't have python.
              USER: Pardon? I don't have what?
              DEV: Python. It's [long and boring explanations that the user won't understand anyway]
              USER: Um.. right, so how do I fix it?
              DEV: You need to install it. [Long and boring instructions]
              USER: It's a company computer. I can't install anything on it.
              DEV: Um, right, you'll need to ask your IT department to do that.
              ...
              USER: Ok, they istalled it... Oh, that pythong thing is almost 200MB, that's pretty big for something that just does stuff.
              DEV: ... Yeeeeah, um, so how's it working now?
              USER: Still crashes/hangs/does nothing.
              DEV: Right. [Long and boring debuggind session with the user]
              DEV: Oh, the app can't find python. You need to add it to PATH.
              USER: To what now?
              DEV: PATH. It's a [long and boring explanation and instructions how to set up environment variables]
              USER That's pretty convoluted. I thought that doing stuff would be easier.
              DEV: ... Yeeeeah, um, so how's it working now?
              USER: It does stuff but it's pretty slow.
              DEV: Yeeeah, it spins another process that [long and boring explanations of what processes are] and then it communicates the result via [ long and boring explanation of how inter-process communication works]
              USER: Um.. sure, I guess. So you're saying there are actually 2 apps running when I do stuff?
              DEV: Well.. yes.
              USER: That seems a bit much to just do stuff. What if one of them crashes?
              DEV: Oh.. um.. let me get back to you on that
              ...
              DEV: So here's a new version of my app. It fixes an issue where [long and boring explanation about handling inter-process communication failures]
              USER: Oh, great... I guess? Thanks.
              ...
              USER: So I've been meaning to ask you - there's this app called "The other app" that also does stuff, but it does it faster, is a lot smaller and doesn't need me to install or setup anything else. Why is that?
              DEV: Oh! I know that app! It's totally lame. The authors use C++ to parse text and they needed 10 lines of code to do that. 10 lines! Madness! I'm doing the same in python with just one line! \o/ :D
              USER: So you're saying I had to go through all of that so that you could write 1 line instead of 10?
              ...
              DEV: Um... yeeeeeah...

              Now, I could also come up with a Dev and Maintainer conversation, but lets leave that to the imagination ;)

              M 1 Reply Last reply 22 Oct 2017, 09:36
              3
              • M mrjj
                21 Oct 2017, 21:32

                Well if a given tool makes you more productive, there is no reason not to use it.
                i also use scripting languages for tasks that is more handy without having to compile it for each change etc.

                The only minus is that if you use python for the text processing , you must also include that
                if you deploy to other machines. On linux its easy to get python but
                on windows, there might be path issue etc when u try to call it.
                And user have to install it.

                Not sure you can just have it your folder and call it there but it is of course solvable.

                N Offline
                N Offline
                Niagarer
                wrote on 22 Oct 2017, 08:19 last edited by Niagarer
                #11

                @mrjj
                Well yes, there seem to be no easy way to do that. There is no portable Pyhton interpreter, that only treads, what I need, the program would enlarge dramatically :/
                I mean doing what I want will be completely feasible also in C++, but I just wanted to look, if there maybe is a easy way to include a Python script in QtCreator without requiring a manually installed Python interpreter by the user, because I saw, that it even supports Python files because of PyQt. It seems like I have to use C++ for that.

                M 1 Reply Last reply 22 Oct 2017, 09:45
                0
                • C Chris Kawa
                  22 Oct 2017, 01:30

                  And now imagine this hypothetical conversation of a dev(il) and a (l)user ;)

                  DEV: Hi user! Try my new app, it does stuff!
                  USER: Great! I need stuff.
                  ...
                  USER: Um, it crashes/hangs/does nothing when I hit the "do stuff" button.
                  DEV: Oh, can you [long and boring instructions to get logs, debug etc.]?
                  ...
                  DEV. Right. You don't have python.
                  USER: Pardon? I don't have what?
                  DEV: Python. It's [long and boring explanations that the user won't understand anyway]
                  USER: Um.. right, so how do I fix it?
                  DEV: You need to install it. [Long and boring instructions]
                  USER: It's a company computer. I can't install anything on it.
                  DEV: Um, right, you'll need to ask your IT department to do that.
                  ...
                  USER: Ok, they istalled it... Oh, that pythong thing is almost 200MB, that's pretty big for something that just does stuff.
                  DEV: ... Yeeeeah, um, so how's it working now?
                  USER: Still crashes/hangs/does nothing.
                  DEV: Right. [Long and boring debuggind session with the user]
                  DEV: Oh, the app can't find python. You need to add it to PATH.
                  USER: To what now?
                  DEV: PATH. It's a [long and boring explanation and instructions how to set up environment variables]
                  USER That's pretty convoluted. I thought that doing stuff would be easier.
                  DEV: ... Yeeeeah, um, so how's it working now?
                  USER: It does stuff but it's pretty slow.
                  DEV: Yeeeah, it spins another process that [long and boring explanations of what processes are] and then it communicates the result via [ long and boring explanation of how inter-process communication works]
                  USER: Um.. sure, I guess. So you're saying there are actually 2 apps running when I do stuff?
                  DEV: Well.. yes.
                  USER: That seems a bit much to just do stuff. What if one of them crashes?
                  DEV: Oh.. um.. let me get back to you on that
                  ...
                  DEV: So here's a new version of my app. It fixes an issue where [long and boring explanation about handling inter-process communication failures]
                  USER: Oh, great... I guess? Thanks.
                  ...
                  USER: So I've been meaning to ask you - there's this app called "The other app" that also does stuff, but it does it faster, is a lot smaller and doesn't need me to install or setup anything else. Why is that?
                  DEV: Oh! I know that app! It's totally lame. The authors use C++ to parse text and they needed 10 lines of code to do that. 10 lines! Madness! I'm doing the same in python with just one line! \o/ :D
                  USER: So you're saying I had to go through all of that so that you could write 1 line instead of 10?
                  ...
                  DEV: Um... yeeeeeah...

                  Now, I could also come up with a Dev and Maintainer conversation, but lets leave that to the imagination ;)

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 22 Oct 2017, 09:36 last edited by
                  #12

                  @Chris-Kawa
                  Haha, sadly its not far from common truth.

                  We use autocad and the drawing are also show to potential buyers.
                  I have shown the sales persons how to export to pdf to send via mail.

                  The marketing boss however, didn't feel like exporting - so he send the native format to a client. The client cannot watch it (surprice) and ask him how to open it.
                  The boss ask me and i tell client we thus this autodesk viewer. DWG TrueView (720 MB) The client tries to install it, but being on Win 7 - it wants all .NET runtimes
                  updated and he end up calling his IT person.
                  The TrueView fails to install several times for him and he ask the client if we can do something else. The Client ask the Boss and he ask me to call the clients IT dude.
                  The IT dude ask how we can solve it and I say just "a moment" and then export the
                  drawing to PDF and send it....

                  1 Reply Last reply
                  3
                  • N Niagarer
                    22 Oct 2017, 08:19

                    @mrjj
                    Well yes, there seem to be no easy way to do that. There is no portable Pyhton interpreter, that only treads, what I need, the program would enlarge dramatically :/
                    I mean doing what I want will be completely feasible also in C++, but I just wanted to look, if there maybe is a easy way to include a Python script in QtCreator without requiring a manually installed Python interpreter by the user, because I saw, that it even supports Python files because of PyQt. It seems like I have to use C++ for that.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 22 Oct 2017, 09:45 last edited by
                    #13

                    @Niagarer
                    Well it depends how much of python you need/use. If the trade off in deployment are worth it.

                    That said, there is
                    https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-win32.zip
                    Which can run from a local folder as far as i can see.
                    The docs says
                    "Windows Users: There are redistributable zip files containing the Windows builds, making it easy to redistribute Python as part of another software package. Please see the documentation regarding Embedded Distribution for more information."

                    But if your app is to run on say Mac,Linux AND windows, i would not myself go down the route of including/needing python as its bound to give issues.

                    However, if only for windows, then this new feature in 3.5
                    https://docs.python.org/3.6/using/windows.html#embedded-distribution
                    is ment for your scenario and makes it far less involved mixing c++/python
                    on windows.

                    N 1 Reply Last reply 22 Oct 2017, 12:14
                    3
                    • C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 22 Oct 2017, 11:11 last edited by
                      #14

                      @mrjj Haha, well I didn't just make it up. It's a very common scenario. Trading hours of users time/money/resources to workaround a minor development inconvenience.
                      As for the embedded solution. It is a solution but I don't like it personally. Just out of curiosity I searched for python on my system and I found I have 23 copies of the interpreter, installed along various software :/ That's just awful.
                      I know it's against the tide these days but I treat python as a development tool, not something I would bother my users with.

                      M 1 Reply Last reply 22 Oct 2017, 13:23
                      2
                      • M mrjj
                        22 Oct 2017, 09:45

                        @Niagarer
                        Well it depends how much of python you need/use. If the trade off in deployment are worth it.

                        That said, there is
                        https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-win32.zip
                        Which can run from a local folder as far as i can see.
                        The docs says
                        "Windows Users: There are redistributable zip files containing the Windows builds, making it easy to redistribute Python as part of another software package. Please see the documentation regarding Embedded Distribution for more information."

                        But if your app is to run on say Mac,Linux AND windows, i would not myself go down the route of including/needing python as its bound to give issues.

                        However, if only for windows, then this new feature in 3.5
                        https://docs.python.org/3.6/using/windows.html#embedded-distribution
                        is ment for your scenario and makes it far less involved mixing c++/python
                        on windows.

                        N Offline
                        N Offline
                        Niagarer
                        wrote on 22 Oct 2017, 12:14 last edited by Niagarer
                        #15

                        @mrjj
                        Yes, I also read about it, but I want to make it platform independent (all platforms, Qt can support with a desktop application), so unfortunately not the best option...
                        Well, that story sounds like a lot of fun and @Chris-Kawa yes 23 copies of one interpreter... that hurts.

                        1 Reply Last reply
                        0
                        • C Chris Kawa
                          22 Oct 2017, 11:11

                          @mrjj Haha, well I didn't just make it up. It's a very common scenario. Trading hours of users time/money/resources to workaround a minor development inconvenience.
                          As for the embedded solution. It is a solution but I don't like it personally. Just out of curiosity I searched for python on my system and I found I have 23 copies of the interpreter, installed along various software :/ That's just awful.
                          I know it's against the tide these days but I treat python as a development tool, not something I would bother my users with.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 22 Oct 2017, 13:23 last edited by
                          #16

                          wow @Chris-Kawa , i only had 2 extra. Spacewise its no concern but if an app adds it path to the global path and others do too, you have this lovely situation it might use
                          another version than it came with. I imagine on linux its being less of an issue but
                          i full agree that an external dependency often is an extra invitation for user support requests. So the fix for a system wide working python is to let all apps bundle one is
                          indeed awful.

                          @Niagarer
                          For multiple platform deployment, i would think very, very hard if you could avoid
                          using python with the c++ program.
                          If the python programs only scan and replace such %tags%, it might not be so bad in c++ but i suspect you are doing far more.

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            Niagarer
                            wrote on 1 Nov 2017, 08:06 last edited by
                            #17

                            Well, to sum it up:
                            It is technically very possible with embedded python like
                            https://www.python.org/ftp/python/3.6.3/python-3.6.3-embed-win32.zip
                            but
                            @mrjj said in How to use/execute a Python script inside QtCreator C++ project:

                            But if your app is to run on say Mac,Linux AND windows, i would not myself go down the route of including/needing python as its bound to give issues.

                            There are easy solutions for Windows and if you use the global path to a python interpreter, you should pay attention, that your python script always works with the newest python versions, but then this would be a legitim option.
                            I also want to refer to the post by Chris Kawa (this first answer post at the top).
                            If tere is another option I forgot, please answer here and update this post ^^

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              marwa ahmed
                              wrote on 2 Jun 2020, 19:50 last edited by
                              #18

                              hello, can you tell me how to call python code from qt c++ application? how the python code shall look like and the function that shall be called how shall it be looked like?

                              jsulmJ 2 Replies Last reply 3 Jun 2020, 06:37
                              0
                              • M marwa ahmed
                                2 Jun 2020, 19:50

                                hello, can you tell me how to call python code from qt c++ application? how the python code shall look like and the function that shall be called how shall it be looked like?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 3 Jun 2020, 06:37 last edited by
                                #19

                                @marwa-ahmed Do you want to execute an external Python script or do you want to execute Python script inside your application (in same process)?

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

                                JonBJ 1 Reply Last reply 3 Jun 2020, 06:41
                                0
                                • jsulmJ jsulm
                                  3 Jun 2020, 06:37

                                  @marwa-ahmed Do you want to execute an external Python script or do you want to execute Python script inside your application (in same process)?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on 3 Jun 2020, 06:41 last edited by
                                  #20

                                  @jsulm
                                  The OP already re-raised this question in https://forum.qt.io/topic/115475/calling-python-from-qt-c, where it has been answered.

                                  1 Reply Last reply
                                  2
                                  • M marwa ahmed
                                    2 Jun 2020, 19:50

                                    hello, can you tell me how to call python code from qt c++ application? how the python code shall look like and the function that shall be called how shall it be looked like?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 3 Jun 2020, 06:42 last edited by
                                    #21

                                    @marwa-ahmed Please do not double-post!

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

                                    1 Reply Last reply
                                    0

                                    • Login

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