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

  • Hey,
    I want to run a Python script in my QtCreator C++ project, but what I don't want is something like a python interpreter inside my application.
    I just would like to switch to Python for doing one specific task in my program. It should take a text file, change some things and save it as a new one, so it has nothing to do with the rest of the program.
    Is this possible and how?
    Thansk for answers!

  • Moderators

    @Niagarer said in How to use/execute a Python script inside QtCreator C++ project:

    It should take a text file, change some things and save it as a new one, so it has nothing to do with the rest of the program.

    Sounds like this could be easily done with c++. Is it worth to plug another language with its whole runtime environment just for that one task?

    Anyway, If you don't want to embed an interpreter into your app the only other option is to run the interpreter as a separate process. You can use the QProcess class for that. It would require the user to have python interpreter installed.
    In the simplest case something like this:

    QStringList arguments { "", "someotherparam", ... };
    QProcess p;
    p.start("python", arguments);

  • @Chris-Kawa
    Ok. would it be possible with python interpreter?
    I did not explain that right, sorry. I meant I dont want a python script editor with interpreter behind. I said, because when I googled the progblem, I only got results, where someone tries to make a little python script window where you can code and send it to a interpreter after it.
    I just want one Python file in the project, where I do some stuff after I clicked on a button in my window.

  • Moderators

    @Niagarer said in How to use/execute a Python script inside QtCreator C++ project:

    I just want one Python file in the project, where I do some stuff after I clicked on a button in my window.

    That's not how it works. C++ compiler won't magically compile python all of a sudden. Unlike compiled c++ programs, python scrips need an interpreter installed to run. That's what my example is doing - runs a python script in an external interpreter. It doesn't matter if the script comes from a user via textbox he entered it in or you provide a file with the script. It still needs to go through python interpreter.

    Alternatives are writing your entire app in python (e.g. with PyQt if you still want to use Qt) or compile the python script to native executable (with something like py2exe or Shed Skin and run that through QProcess (the difference is you don't need python interpreter installed on user's computer this way).

  • Lifetime Qt Champion


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

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

  • Lifetime Qt Champion

    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?

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


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

  • Lifetime Qt Champion

    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.

  • Moderators

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

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

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion

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

    That said, there is
    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
    is ment for your scenario and makes it far less involved mixing c++/python
    on windows.

  • Moderators

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

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

  • Lifetime Qt Champion

    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.

    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.

  • Well, to sum it up:
    It is technically very possible with embedded python like
    @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 ^^

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

  • Lifetime Qt Champion

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

  • @jsulm
    The OP already re-raised this question in, where it has been answered.

  • Lifetime Qt Champion

    @marwa-ahmed Please do not double-post!

Log in to reply