OiO.lk Blog python How to configure PyConfig for embedded interpreter
python

How to configure PyConfig for embedded interpreter


I have added an embedded python interpreter to a C program using libpython, complete with defining a custom python module that allows access from python to various variables and built-in functions within the program, the idea being to provide a scripting capability. That appears to have been the easy bit: all that is working fine if the program is compiled and run on the same machine.

However the packages (appimage, flatpak etc.) built by my CI do not work. I suspect this is because there can be a clash between the version of python used in the CI to build the package and the version the user has installed, and that I need to isolate the python interpreter within my program and provide it with a venv (which the program should create, as it’s not possible to rely on all users to be competent to do this themselves).

I’ve read https://peps.pythondiscord.com/pep-0587/#isolated-configuration and tried numerous variations but what I’ve tried either ends up not actually being isolated from the system installation or failing to locate the core modules it requires in order to set up the venv (usually in this case it complains about the encodings module not being found). So either way I end up either over-isolating or under-isolating the installation.

I’m ideally looking for example C code showing how to do the 2 steps:

  1. Set up a venv that uses either the system python (if not running from an appimage / flatpak) or the bundled python (if we are in an appimage / flatpak),
  2. Initialize python using the venv environment, isolated from the system python install if we’re in an appimage / flatpak,
    more clearly than is explained in PEP-0587. Alternatively, pointers to projects that do the same thing that I can look at as a reference would be helpful.



You need to sign in to view this answers

Exit mobile version