【问题标题】:Shipping Python interpreter with C++ project使用 C++ 项目运送 Python 解释器
【发布时间】:2022-11-27 23:29:51
【问题描述】:

问题描述: 我有一个涉及实时 python 脚本解释的 Visual Studio 2022 C++ 项目。当然,我需要一个有效的 Python 安装来执行此操作。但是,我打算将它作为一个应用程序发布,所以我想要一个本地化的 Python 安装,以避免消费者端安装,但这不会干扰 Windows 的环境变量。

我做了什么: 我从 Python 安装的“include”文件夹中包含“Python.h”,我已将其“libs”文件夹添加到“其他库目录”,我已将“python311.lib”添加到“其他依赖项”,我记得将 Python311.dll 复制到我项目的解决方案目录中。一切都正确链接。但是,当我运行编译并执行我的程序时,我收到一长串错误,如下所示:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = 'C:\Coding Projects\MaSGE\Lib'
  sys._base_executable = 'C:\\Coding Projects\\MaSGE\\x64\\Release\\MaSGE.exe'
  sys.base_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.base_exec_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.platlibdir = 'DLLs'
  sys.executable = 'C:\\Coding Projects\\MaSGE\\x64\\Release\\MaSGE.exe'
  sys.prefix = 'C:\\Coding Projects\\MaSGE'
  sys.exec_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.path = [
    'C:\\Coding Projects\\MaSGE\\python311.zip',
    'C:\\Coding Projects\\MaSGE\\Lib',
    'C:\\Coding Projects\\MaSGE\\DLLs',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000399c (most recent call first):
  <no Python frame>

我特别感兴趣的是前两行,加上第 4 行和第 5 行的“PYTHONHOME =(未设置)”和“PYTHONPATH =(未设置)”,据我所知,它们是环境变量。

这让我想到了问题的症结所在: 有什么方法可以将便携式 Python 解释器安装到特定文件夹以规避环境变量问题?

【问题讨论】:

  • 这就是安装程序存在的原因。
  • 这不是远程帮助。

标签: python c++ visual-studio-2022 python-install


【解决方案1】:

要将 Python 嵌入到您的应用程序中,您需要做两件事:

初始化孤立的python

这不会让用户的系统干扰您的应用程序。

https://docs.python.org/3/c-api/init_config.html#init-isolated-conf

使用您的应用程序部署 python 内容

在 Windows 上,您需要:

  1. Python DLL (python311.dll)。
  2. Python 标准库。 从 python 安装复制 Lib 文件夹,或压缩其内容并以 python dll 命名它(例如 python311.zip),或使用 Windows 可嵌入包中的相同 zip)
  3. Python 模块(DLLs 目录)

    在 Linux 上,情况略有不同:

    1. Python 共享库 (libpython311.so)
    2. Python 标准库(从 Python 安装复制 lib/python311/ 文件夹,或压缩其内容lib-dynload 文件夹除外并将其命名为lib/python311.zip)
    3. (如果使用压缩标准库)将lib-dynload复制到lib/python3.11/lib-dynload

      当然,你必须用你的python版本替换3113.11

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2021-09-14
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    相关资源
    最近更新 更多