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

使用Poetry安装PyMC时遇CompileError问题求助

解决PyTensor编译时找不到Python.h头文件的问题(Poetry+PyMC环境)

问题背景

使用Poetry管理依赖,执行poetry add pymc完成安装后,运行PyTensor官方示例代码时触发CompileError,核心报错为找不到Python.h头文件。最简复现代码如下:

import numpy
import pytensor.tensor as at
from pytensor import function
x = at.dscalar('x')
y = at.dscalar('y')
z = x + y
f = function([x, y], z)

报错关键片段:

CompileError: Compilation failed (return status=1):
...
/home/.pytensor/compiledir_.../mod.cpp:1:10: fatal error: Python.h: No such file or directory
    1 | #include <Python.h>
      |          ^~~~~~~~~~
compilation terminated.

原因分析

该错误源于系统缺少对应版本的Python开发包:PyTensor在动态编译C扩展时需要Python.h等头文件,这类文件属于Python开发依赖包,默认不会随Python解释器一起安装;而Docker容器通常预装了这类开发依赖,因此容器内可正常运行。

解决方案

1. 安装对应版本的Python开发包

根据你的Linux发行版,安装与当前Python版本匹配的开发包(示例为Python 3.10,需替换为你实际使用的版本):

  • Debian/Ubuntu系:
    sudo apt-get update && sudo apt-get install python3.10-dev
    
  • RHEL/CentOS系:
    sudo dnf install python3.10-devel
    
  • Arch Linux系:
    sudo pacman -S python310
    

2. 验证头文件是否存在

安装完成后,检查路径/usr/include/python3.10/Python.h是否存在,确保与编译命令中的-I/usr/include/python3.10路径一致。

3. 重新运行代码

直接再次执行示例代码,PyTensor应该能正常编译C扩展。若问题仍存在,可尝试重建Poetry虚拟环境:

poetry env remove python3.10
poetry install

注意事项

  • 确保Poetry环境使用的Python版本与安装的开发包版本一致,可通过poetry run python --version确认。
  • 若系统中存在多个Python版本,需明确指定安装对应版本的开发包,避免版本不匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:00:35