简体   繁体   English

在 C++ 应用程序中嵌入 python 环境

[英]embed python environment in c++ application

Using the c-python api I try to embed python 3.6 into a c++ application.使用 c-python api 我尝试将 python 3.6 嵌入到 c++ 应用程序中。 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.此外,在阅读 c-python 代码时,我可以看到pvenv.cfg._pth文件的使用,但这些解决方案似乎都不起作用。

Any idea what's the right way to use virtual environment from c api?知道从 c api 使用虚拟环境的正确方法是什么吗?

EDIT编辑

Let's take a concrete example.让我们举一个具体的例子。 I have python installed in我已经安装了 python

c:\python36

For my c++ application I created a virtual env using the command python -m venv c:\\my_cpp_app\\python_venv\\ in:对于我的 C++ 应用程序,我使用命令python -m venv c:\\my_cpp_app\\python_venv\\创建了一个虚拟环境:

c:\my_cpp_app\python_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\\使用 c-python api 我想让我的 cpp 应用程序使用位于python_venv的虚拟环境而不是c:\\python36\\

As stated in comments, embedded python 3.6 and virtual environment created with venv seem incompatible ( bugs.python.org/issue22213 )如评论中所述,嵌入式 python 3.6 和使用 venv 创建的虚拟环境似乎不兼容( bugs.python.org/issue22213

I managed to make it work using virtualenv instead and by calling Py_SetPythonHome prior Py_Initialize .我设法使它工作使用virtualenv代替,并通过调用Py_SetPythonHome之前Py_Initialize See more details on the python startup sequence查看有关python启动顺序的更多详细信息

Locating Python and the standard library定位 Python 和标准库

The location of the Python binary and the standard library is influenced by several elements. Python 二进制文件和标准库的位置受几个元素的影响。 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).即使该描述也不完整,因为它未能针对 Python 3.3(在 PEP 405 中详细说明)中添加的虚拟环境支持进行更新。

These calculations are affected by the following function calls (made prior to calling Py_Initialize()) and environment variables:这些计算受以下函数调用(在调用 Py_Initialize() 之前进行)和环境变量的影响:

  • 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.还会检查文件系统是否有 pyvenv.cfg 文件(请参阅 PEP 405),或者检查失败的lib/os.py (Windows) 或lib/python$VERSION/os.py文件。

The build time settings for PREFIX and EXEC_PREFIX are also relevant, as are some registry settings on Windows. PREFIXEXEC_PREFIX的构建时间设置也是相关的,Windows 上的一些注册表设置也是相关的。 The hardcoded fallbacks are based on the layout of the CPython source tree and build output when working in a source checkout.硬编码回退基于 CPython 源代码树的布局,并在源代码检出时构建输出。

The implementation of pep 587 in later versions should facilitate all this!在更高版本中实现pep 587应该会促进这一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM