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

CMake检测到Python3.10却编译出适配3.8的扩展,导入版本不匹配报错求解决

问题:PyBind11编译的C++ Python扩展出现版本不匹配错误

我正在使用PyBind11为Python 3.10构建C++ Python扩展,但导入时触发以下错误:

ImportError: Python version mismatch: module was compiled for Python 3.8, but the interpreter version is incompatible: 3.10.5

我的配置细节:

  • CMakeLists.txt中已指定:find_package(Python3 3.10 REQUIRED)
  • 运行cmake时额外传入参数:-DPYTHON_EXECUTABLE:FILEPATH=$(which python),且which python指向Python 3.10的可执行文件
  • cmake执行输出显示:
    Found Python3: /PATH/TO/venv/bin/python3 (found suitable version "3.10.5", minimum required is "3.10") found components: Interpreter 
    
    输出中未提及其他Python版本。

编译生成的扩展文件名为module.cpython-310-x86_64-linux-gnu.so,按Python扩展命名规则,该文件应仅适配Python 3.10解释器,但实际导入时却出现上述版本不匹配错误。手动将文件名改为module.cpython-38-x86_64-linux-gnu.so后,该扩展可在Python 3.8解释器中正常导入。

我已尝试过相关解决方案,现寻求该问题的原因及修复方法。


内容的提问来源于stack exchange,提问作者Alexander Soare

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 09:54:24