Embedding Python in a C++ app

47 views
Skip to first unread message

Vadim Chugunov

unread,
Sep 27, 2020, 8:11:08 PM9/27/20
to pyoxidiz...@googlegroups.com
Hi,
I want to embed Python interpreter into a C++ application (which needs to run on a wide range of Linux distros, thus the need for portable Python).  
I'd like to customize the standard Python distro by omitting some modules (like tkinter), then package the rest into a zip archive alongside my application.  
Can PyOxidizer help me with any of that?

Gregory Szorc

unread,
Sep 27, 2020, 8:37:59 PM9/27/20
to Vadim Chugunov, pyoxidizer-users
PyOxidizer does not support emitting a zip archive. It could. But I don't believe anybody has officially asked for this feature. Please consider filing a GitHub issue for it. (It's probably low on my priority list, however).

Your best bet is to follow the PyOxidizer documentation for creating a Rust project and then modifying that project's Cargo.toml to produce a shared or static library instead of an executable, which you can then link into your C++ application. See https://pyoxidizer.readthedocs.io/en/latest/rust.html (note this links to the latest version of the docs, which have changed substantially since the 0.7 release).

I hope to one day teach PyOxidizer's config files to be able to produce static or shared libraries in addition to executables. It's on the long feature backlog. It is certainly possible, it just isn't very turnkey at the moment.

If you don't want to involve Rust or PyOxidizer, https://github.com/indygreg/python-build-standalone distributes Python builds (which PyOxidizer consumes) which have object files that can be linked into a new binary. PyOxidizer is effectively a glorified frontend for feeding these distribution archives into a linker (and a lot of other functionality, of course). You should be able to parse the PYTHON.json file from those distributions into some linker invocations to build a new binary. PyOxidizer's code for doing this lives at https://github.com/indygreg/PyOxidizer/blob/a49c3db24a9388e2124ea08eab45f66fdf3cdc8b/pyoxidizer/src/py_packaging/libpython.rs. Most of the complexity lives with populating the arguments to link_libpytho().

The `pyoxidizer` Rust crate is usable as a Rust crate. So if you wanted to write a few dozen lines of Rust, you could probably produce a quick and dirty program for emitting a statically linked libpython without involving a custom CLI or Starlark config files.

--
You received this message because you are subscribed to the Google Groups "pyoxidizer-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyoxidizer-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyoxidizer-users/CADecdi%2B2yrtEiDa1qOXwA%3DZZV%2Bdu7jHtCWvRG0oe_0T3LcENzg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages