You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除嵌入式Python的"Could not find platform dependent libraries <exec_prefix>"错误?

解决嵌入Python的C++程序中"Could not find platform dependent libraries <exec_prefix>"错误

问题场景

我尝试运行一段嵌入Python的C++代码:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <iostream>

int main()
{
    Py_Initialize();

    std::wcout << std::endl;
    std::wstring pyPath = Py_GetPath();
    std::wcout << L"Paths: " << pyPath << std::endl;

    std::wcout << std::endl;

    std::wstring pyPref = Py_GetExecPrefix();
    std::wcout << L"Exec Prefix: " << pyPref << std::endl;

    PyRun_SimpleString( "print('Hello World from Embedded Python!!!')" );

    Py_Finalize();

    return 0;
}

控制台输出如下:

Could not find platform dependent libraries <exec_prefix>

Paths: C:\Program Files\Python311;C:\Program Files\Python311\python311_d.zip;C:\Program Files\Python311\Lib;C:\Program Files\Python311\DLLs

Exec Prefix: C:\Program Files\Python311
Hello World from Embedded Python!!!

我的问题是:如何消除"Could not find platform dependent libraries <exec_prefix>"错误,让Python正确加载其依赖库?

我的CMakeLists.txt文件内容:

cmake_minimum_required(VERSION 3.22)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(PROJ_NAME "Test")

project(${PROJ_NAME})

include_directories( $ENV{PYTHONPATH}/include )
link_directories( $ENV{PYTHONPATH}/libs )

list(APPEND SRC_LIST src/main.cpp)

add_executable(${PROJ_NAME} ${SRC_LIST})

系统变量配置:

  • PYTHONPATH: C:\Programm Files\Python311(存在拼写错误:多了一个m)
  • PYTHONHOME: C:\Programm Files\Python311(同上)
  • 该路径已加入PATH系统变量,实际对应Python 3.11的安装目录C:\Program Files\Python311

直接从CMD运行Python无任何错误:

>python --version
Python 3.11.0

>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit();

解决方案

1. 修正系统变量的拼写错误

首先修正PYTHONPATH和PYTHONHOME中的路径拼写,将C:\Programm Files\Python311改为C:\Program Files\Python311,确保系统变量指向正确的Python安装目录。修改后重启CMD或IDE,让新的环境变量生效。

2. 在代码中显式指定Python Home路径

如果修正系统变量后仍有问题,可以在调用Py_Initialize()之前,显式设置Python的Home路径,避免环境变量读取异常:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <iostream>

int main()
{
    // 显式设置Python Home路径
    wchar_t pythonHome[] = L"C:\\Program Files\\Python311";
    Py_SetPythonHome(pythonHome);

    Py_Initialize();

    std::wcout << std::endl;
    std::wstring pyPath = Py_GetPath();
    std::wcout << L"Paths: " << pyPath << std::endl;

    std::wcout << std::endl;

    std::wstring pyPref = Py_GetExecPrefix();
    std::wcout << L"Exec Prefix: " << pyPref << std::endl;

    PyRun_SimpleString( "print('Hello World from Embedded Python!!!')" );

    Py_Finalize();

    return 0;
}

3. 完善CMake配置,明确链接Python库

在CMakeLists.txt中,建议使用find_package自动查找Python,替代手动指定路径的方式,更适配不同版本和架构的Python:

cmake_minimum_required(VERSION 3.22)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(PROJ_NAME "Test")

project(${PROJ_NAME})

# 自动查找Python 3.11的开发组件
find_package(Python3 3.11 REQUIRED COMPONENTS Development)

include_directories(${Python3_INCLUDE_DIRS})
link_directories(${Python3_LIBRARY_DIRS})

list(APPEND SRC_LIST src/main.cpp)

add_executable(${PROJ_NAME} ${SRC_LIST})

# 链接Python库
target_link_libraries(${PROJ_NAME} ${Python3_LIBRARIES})

4. 确保Python DLLs目录加入PATH

将C:\Program Files\Python311\DLLs添加到系统PATH变量中,Python的平台依赖库(如python311.dll、各类扩展库DLL)都存放在这个目录下,确保系统在运行程序时能找到这些文件。

内容的提问来源于stack exchange,提问作者Dmitriy Goncharenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:41:27