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

Ubuntu20.04下VSCode编译Python C++扩展报错:Python.h不存在(已装python3.9-dev)

解决Ubuntu 20.04下VSCode编译Python C++扩展时Python.h找不到的问题

核心问题

你的c_cpp_properties.json仅配置了VSCode的IntelliSense代码提示路径,实际编译时clang并没有获取到这些路径,同时你还缺少numpy头文件的包含路径配置,这才导致编译报错。

步骤1:获取numpy的实际包含路径

在终端执行以下命令,得到numpy的头文件根目录:

python3.9 -m numpy.get_include

输出示例:/usr/lib/python3.9/site-packages/numpy/core/include(具体路径根据你的numpy安装位置调整)

步骤2:配置VSCode编译任务(tasks.json)

在VSCode中按Ctrl+Shift+B,选择「创建tasks.json文件」,选择「Others」,然后替换为以下配置(注意将numpy路径替换为你刚才获取的结果):

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile-python-extension",
            "type": "shell",
            "command": "clang++",
            "args": [
                "-std=c++14",
                "-I/usr/include/python3.9",
                "-I/usr/lib/python3.9/site-packages/numpy/core/include", // 替换成你的numpy实际路径
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lpython3.9"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}

步骤3:验证编译

回到你的测试代码,按Ctrl+Shift+B执行编译任务,此时clang会正确加载Python和numpy的头文件路径,并链接Python库,编译应该可以正常完成。

额外检查点

  • 确保numpy是通过python3.9的pip安装:执行python3.9 -m pip install numpy,避免系统numpy与pip安装的numpy路径冲突
  • 检查/usr/include/python3.9/Python.h是否存在,若不存在,重新安装python3.9-dev:
sudo apt reinstall python3.9-dev

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 08:37:25