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.8k 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.
  • mrjjM mrjj

    @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?

    NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on 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
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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.

      NiagarerN 1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 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 ;)

        mrjjM 1 Reply Last reply
        3
        • mrjjM mrjj

          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.

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on 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.

          mrjjM 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            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 ;)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            • NiagarerN Niagarer

              @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.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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.

              NiagarerN 1 Reply Last reply
              3
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 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.

                mrjjM 1 Reply Last reply
                2
                • mrjjM mrjj

                  @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.

                  NiagarerN Offline
                  NiagarerN Offline
                  Niagarer
                  wrote on 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
                  • Chris KawaC Chris Kawa

                    @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.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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
                    • NiagarerN Offline
                      NiagarerN Offline
                      Niagarer
                      wrote on 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 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
                        0
                        • M marwa ahmed

                          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 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
                          0
                          • jsulmJ jsulm

                            @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 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

                              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 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