embed python environment in c++ application
Question:
Using the c-python api I try to embed python 3.6 into a c++ application.
However instead of using the system install i’d like to use a virtual environment.
I didn’t find any documentation on the way to do that.
Some related documentation mention
py_SetProgramName
or
py_SetPythonHome
Also when reading c-python code i can see the use of pvenv.cfg
or ._pth
files, but none of these solution seem to work.
Any idea what’s the right way to use virtual environment from c api?
EDIT
Let’s take a concrete example. I have python installed in
c:python36
For my c++ application I created a virtual env using the command python -m venv c:my_cpp_apppython_venv
in:
c:my_cpp_apppython_venv
Using the c-python api I’d like to make my cpp application use the virtual environment located in python_venv
instead of c:python36
Answers:
As stated in comments, embedded python 3.6 and virtual environment created with venv seem incompatible (bugs.python.org/issue22213)
I managed to make it work using virtualenv
instead and by calling Py_SetPythonHome
prior Py_Initialize
.
See more details on the python startup sequence
Locating Python and the standard library
The location of the Python
binary and the standard library is influenced by several elements. The
algorithm used to perform the calculation is not documented anywhere
other than in the source code. Even that description is
incomplete, as it failed to be updated for the virtual environment
support added in Python 3.3 (detailed in PEP 405).
These calculations
are affected by the following function calls (made prior to calling
Py_Initialize()) and environment variables:
Py_SetPythonHome()
Py_SetProgramName()
PYTHONHOME
The filesystem is also inspected for
pyvenv.cfg files (see PEP 405) or, failing that, a lib/os.py
(Windows)
or lib/python$VERSION/os.py
file.
The build time settings for PREFIX
and EXEC_PREFIX
are also relevant, as are some registry settings on
Windows. The hardcoded fallbacks are based on the layout of the
CPython source tree and build output when working in a source
checkout.
The implementation of pep 587 in later versions should facilitate all this!
The documentation is not very clear on how to do this exactly in the latest version of python 3.11. I cant find any good examples of someone doing this. What would be the equivalent of calling the activate and deactivate scripts? I can’t seem to get this to work and I am tired of just trial and erroring this.
Using the c-python api I try to embed python 3.6 into a c++ application.
However instead of using the system install i’d like to use a virtual environment.
I didn’t find any documentation on the way to do that.
Some related documentation mention
py_SetProgramName
or
py_SetPythonHome
Also when reading c-python code i can see the use of pvenv.cfg
or ._pth
files, but none of these solution seem to work.
Any idea what’s the right way to use virtual environment from c api?
EDIT
Let’s take a concrete example. I have python installed in
c:python36
For my c++ application I created a virtual env using the command python -m venv c:my_cpp_apppython_venv
in:
c:my_cpp_apppython_venv
Using the c-python api I’d like to make my cpp application use the virtual environment located in python_venv
instead of c:python36
As stated in comments, embedded python 3.6 and virtual environment created with venv seem incompatible (bugs.python.org/issue22213)
I managed to make it work using virtualenv
instead and by calling Py_SetPythonHome
prior Py_Initialize
.
See more details on the python startup sequence
Locating Python and the standard library
The location of the Python
binary and the standard library is influenced by several elements. The
algorithm used to perform the calculation is not documented anywhere
other than in the source code. Even that description is
incomplete, as it failed to be updated for the virtual environment
support added in Python 3.3 (detailed in PEP 405).These calculations
are affected by the following function calls (made prior to calling
Py_Initialize()) and environment variables:
Py_SetPythonHome()
Py_SetProgramName()
PYTHONHOME
The filesystem is also inspected for
pyvenv.cfg files (see PEP 405) or, failing that, alib/os.py
(Windows)
orlib/python$VERSION/os.py
file.The build time settings for
PREFIX
andEXEC_PREFIX
are also relevant, as are some registry settings on
Windows. The hardcoded fallbacks are based on the layout of the
CPython source tree and build output when working in a source
checkout.
The implementation of pep 587 in later versions should facilitate all this!
The documentation is not very clear on how to do this exactly in the latest version of python 3.11. I cant find any good examples of someone doing this. What would be the equivalent of calling the activate and deactivate scripts? I can’t seem to get this to work and I am tired of just trial and erroring this.