Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Embedding the Python Interpreter in a Qt Application (ubuverse.com)
47 points by istotex on March 13, 2017 | hide | past | favorite | 10 comments


I looked at doing this several years ago on Windows and I seem to recall that Python had a lot of global state. So you could only have one instance of the Python VM and therefore only Python script running at once. Is this still the case?

In the end, for me, it was easier to share file formats between the C++ and Python and run the Python scripts out of process.



Subinterpreters still share a lock and suffer from compatibility issues with C extension modules as the module interface only exports a very primitive notion of per-subinterpreter state, they're pretty much not all that safe to use unless you have a very constrained environment


A big advantage of embedding the Python interpreter is that it cannot be replaced by a malicious program pretending to be Python. It’s going to be internal to your binary, so it will be difficult for someone to intercept the scripts you are executing, or modify the functionality of your application.

There are only a handful of cases where it is worthwhile to embed the Python interpreter. For most cases, running Python in a separate process is probably the best approach.


If you have the luxury of using .NET, embedding IronPython is considerably easier, in my experience. A while back I was fooling around writing a game engine with python scripting support, and it was pretty slick to get going with.


In .NET you can also embed CPython​ using pythonnet:

https://github.com/pythonnet/pythonnet


Piggy backing off this a little, in a similar vain I recently embedded Python in a macOS app (approved by the store) which was a bit of a pain to do. I documented the process as well since I couldn't find a full fledged tutorial on it.

https://medium.com/python-pandemonium/embedding-a-python-app...


Something like this can be (could be?) done with a BlackBerry 10 application. I developed a hackernews client with a python backend and Qt QML frontend. Didn't involve embedding the interpreter as BB10 shipped with python3.2

https://bitbucket.org/microcode/blackberry-py/ http://blackberry-py.microcode.ca/


Has the embedding story improved for Python 3.* ? Whereas extending Python is easy enough, embedding has not been that convenient. Tcl, Guile, Lua do a far better job.

If anyone has drops of wisdom on how to run an embedded Python as a filter or an iterator I would be super interested to know.


What'r the choice points between doing this, and building a Qt app using PyQt (or PySide)? Again all the logic is in Python, no C++ whatever. And use PyInstaller or cxFreeze to make a standalone app for distribution.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: