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

PyGObject安装失败:无法找到msvc_recommended_pragmas.h文件求助

问题重现

安装PyGObject时,构建wheel阶段出现以下错误:

gi\gimodule.c: fatal error C1083: Cannot open include file: 'msvc_recommended_pragmas.h': No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.35.32215\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for PyGObject
Failed to build PyGObject
ERROR: Could not build wheels for PyGObject, which is required to install pyproject.toml-based projects

已尝试操作:安装gtk3/gtk4、确认msvc_recommended_pragmas.h文件存在、将文件所在目录添加至PATH环境变量、管理员权限运行命令行,但问题未解决。

解决方案

1. 正确配置INCLUDE环境变量

PATH环境变量用于定位可执行文件,而C/C++编译器查找头文件依赖INCLUDE环境变量,需将msvc_recommended_pragmas.h所在目录添加至INCLUDE:

  • 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」
  • 在「系统变量」区域点击「新建」,变量名设为INCLUDE,变量值填入文件所在的完整目录(例如C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.35.32215\include,根据实际路径调整)
  • 若INCLUDE已存在,点击「编辑」,将目录追加到现有值后,用分号;分隔
  • 重启命令行工具(CMD/PowerShell),重新执行pip install PyGObject

2. 修正编译时的包含目录参数

从报错的编译命令可见,存在-Imsvc_recommended_pragmas.h的错误参数(-I后应跟目录而非文件名),可通过pip手动指定包含目录:

pip install PyGObject --global-option=build_ext --global-option="-IC:\path\to\header_directory"

将C:\path\to\header_directory替换为msvc_recommended_pragmas.h所在的实际目录。

3. 使用预编译wheel跳过源码构建

Windows下PyGObject源码构建易遇依赖问题,直接使用预编译wheel可避免编译步骤:

  • 下载对应Python版本、系统位数的PyGObject wheel文件(需匹配已安装的gtk版本)
  • 执行以下命令安装:
pip install "path\to\PyGObject-xxx-cp310-cp310-win_amd64.whl"

4. 验证Visual Studio Build Tools组件完整性

确保已安装完整的C++开发组件:

  • 打开Visual Studio Installer,选择「修改」Visual Studio Build Tools 2022
  • 勾选「桌面开发C++」 workload,确保包含「MSVC v143 - VS 2022 C++ x64/x86生成工具」、「Windows 10 SDK (10.0.22000.0)」等组件
  • 点击「修改」完成安装后,重启命令行重试安装

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 02:15:38