在 C++ 应用程序中嵌入 python 环境
embed python environment in c++ application
使用 c-python api 我尝试将 python 3.6 嵌入到 C++ 应用程序中。
但是,我不想使用系统安装,而是想使用虚拟环境。
我没有找到有关执行此操作的任何文档。
一些相关文档提到
py_SetProgramName
或
py_SetPythonHome
此外,在阅读 c-python 代码时,我可以看到 pvenv.cfg
或 ._pth
文件的使用,但这些解决方案的 none 似乎有效。
知道从 c api 使用虚拟环境的正确方法是什么吗?
编辑
举个具体的例子吧。我在
安装了 python
c:\python36
对于我的 C++ 应用程序,我使用命令 python -m venv c:\my_cpp_app\python_venv\
创建了一个虚拟环境:
c:\my_cpp_app\python_venv\
使用 c-python api 我想让我的 cpp 应用程序使用位于 python_venv
而不是 c:\python36\
[=20= 的虚拟环境]
如评论中所述,嵌入式 python 3.6 和使用 venv 创建的虚拟环境似乎不兼容 (bugs.python.org/issue22213)
我设法使用 virtualenv
并在 Py_Initialize
之前调用 Py_SetPythonHome
使其工作。
查看有关 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.
以后版本pep 587的实现应该会方便这一切!
使用 c-python api 我尝试将 python 3.6 嵌入到 C++ 应用程序中。 但是,我不想使用系统安装,而是想使用虚拟环境。 我没有找到有关执行此操作的任何文档。 一些相关文档提到
py_SetProgramName
或
py_SetPythonHome
此外,在阅读 c-python 代码时,我可以看到 pvenv.cfg
或 ._pth
文件的使用,但这些解决方案的 none 似乎有效。
知道从 c api 使用虚拟环境的正确方法是什么吗?
编辑
举个具体的例子吧。我在
安装了 pythonc:\python36
对于我的 C++ 应用程序,我使用命令 python -m venv c:\my_cpp_app\python_venv\
创建了一个虚拟环境:
c:\my_cpp_app\python_venv\
使用 c-python api 我想让我的 cpp 应用程序使用位于 python_venv
而不是 c:\python36\
[=20= 的虚拟环境]
如评论中所述,嵌入式 python 3.6 和使用 venv 创建的虚拟环境似乎不兼容 (bugs.python.org/issue22213)
我设法使用 virtualenv
并在 Py_Initialize
之前调用 Py_SetPythonHome
使其工作。
查看有关 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) 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.
以后版本pep 587的实现应该会方便这一切!