通过 pip 安装 PyGObject 时缺少文件 msvc_recommended_pragmas.h

问题描述 投票:0回答:2

我尝试通过 pip 安装 PyGObject,但失败并出现给定错误:

Building wheels for collected packages: pygobject
Building wheel for pygobject (PEP 517) ... error
ERROR: Command errored out with exit status 1:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DPY_SSIZE_T_CLEAN -IC:\Users\James\AppData\Local\Temp\pip-install-deoh1xpv\pygobject_2e13db219b124bfd8bf7a88e47dc19a5 -IC:\Users\James\AppData\Local\Temp\pip-install-deoh1xpv\pygobject_2e13db219b124bfd8bf7a88e47dc19a5\gi -IC:\Program Files\Python39\include -IC:\Program Files\Python39\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\ATLMFC\include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt /Tcgi\gimodule.c /Fobuild\temp.win-amd64-3.9\Release\gi\gimodule.obj -FImsvc_recommended_pragmas.h
gimodule.c
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\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
----------------------------------------
ERROR: Failed building wheel for pygobject

我又尝试了

python -m pip install pygobject --no-use-517
,但仍然失败并出现错误:

Building wheels for collected packages: pygobject
Building wheel for pygobject (setup.py) ... error
ERROR: Command errored out with exit status 1:

gimodule.c
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\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
----------------------------------------
ERROR: Failed building wheel for pygobject
Running setup.py clean for pygobject
Failed to build pygobject

Installing collected packages: pygobject
Running setup.py install for pygobject ... error
ERROR: Command errored out with exit status 1:

gimodule.c
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\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29333\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
----------------------------------------
ERROR: Command errored out with exit status 1: 

'C:\Program Files\Python39\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\James\\AppData\\Local\\Temp\\pip-install-x0ki7wgt\\pygobject_0af3e48dd4b543de8f83ff716124e468\\setup.py'"'"'; __file__='"'"'C:\\Users\\James\\AppData\\Local\\Temp\\pip-install-x0ki7wgt\\pygobject_0af3e48dd4b543de8f83ff716124e468\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\James\AppData\Local\Temp\pip-record-andppe11\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Program Files\Python39\Include\pygobject' Check the logs for full command output.

似乎所有失败都是由于缺少文件

msvc_recommended_pragmas.h
。这让我不知道我可能错过了什么。涉及的所有运行时依赖项(
python3.9
pip
MSYS2
C++ Build Tools from Visual Studio
)已升级到最新版本仅供参考。

python gtk pygobject
2个回答
5
投票

gvsbuild提供了在Windows下编译GTK的方法。最新说明可以在 https://github.com/wingtk/gvsbuild 的自述文件中找到。一旦安装了依赖项,它将像这样工作:

mkdir C:\gtk-build\github
cd C:\gtk-build\github
git clone C:\github.com\wingtk\gvsbuild
python -m venv .venv
.\.venv\Scripts\activate.ps1
pip install .
gvsbuild build --enable-gi --py-wheel gobject-introspection gtk3 pycairo pygobject

构建完成后,您可以切换到您尝试使用 PyGObject 的项目并激活 virtualenv。然后安装上面创建的轮子:

Get-ChildItem C:\gtk-build\build\x64\release\*\dist\*.whl | ForEach-Object -process { poetry run pip install $_ }

0
投票

今天,当我尝试在 Windows 下(来自 Raspberry 操作系统)设置 Visual Studio Code 项目时,我偶然发现了这一点。

这是让 PYGObject 在 Windows Python 环境中而不是在 msys2 环境中工作的关键提示。由于我花了几个小时,我也会在这里添加我的完整解决方案,也许它对将来的人有帮助。

  1. 安装带有 Python 扩展和 Git 的 Visual Studio Code
  2. 安装GTK/PyGObject,这个需要从源码编译:

2.1 访问 http://www.msys2.org/ 并下载 x86_64 安装程序。按照页面上的说明设置基本环境。 我建议保留标准安装文件夹(C:\msys64),安装路径中的空格可能会导致问题。

2.2 安装具有 C++ 桌面开发工作负载的 Visual Studio Community Edition。 将未来的 GTK 文件夹添加到您的环境变量中:在打开 Windows 开始菜单的情况下键入 environment,然后单击“编辑您帐户的环境变量”。 双击顶部变量列表中的“路径”行。单击“新建”将新项目添加到列表中。粘贴到 C:\gtk-build\gtk\x64 释放 .单击“确定”两次。

克隆gsvbuild(GTK编译脚本)。以管理员身份打开 powershell 提示符并输入以下命令。 您可以在提示符上使用 $env:path 检查路径变量是否包含 gtk-build 路径。 我还建议保留标准安装文件夹,其他路径可能会导致问题:

mkdir C:\gtk-build\github
cd C:\gtk-build\github
git clone https://github.com/wingtk/gvsbuild.git
cd C:\gtk-build\github\gvsbuild
python -m venv .venv
.\.venv\Scripts\activate.ps1
pip install .
pip install setuptools
$env:LIB = "C:\gtk-build\gtk\x64\release\lib;" + $env:LIB
$env:INCLUDE = "C:\gtk-build\gtk\x64\release\include;C:\gtk-build\gtk\x64\release\include\cairo;C:\gtk-build\gtk\x64\release\include\glib-2.0;C:\gtk-build\gtk\x64\release\include\gobject-introspection-1.0;C:\gtk-build\gtk\x64\release\lib\glib-2.0\include;" + $env:INCLUDE
gvsbuild build --enable-gi --py-wheel gtk4 pygobject

注意“pip install setuptools”仅在 Python 3.12 或更高版本上需要,因为 distutils pakacge 在 Python 3.12 中已被删除,并且脚本仍然依赖于它。 希望很快就能修复。

在 Visual Studio 代码中

  1. 命令面板 -> Git:克隆 -> 从 Github 克隆 -> 输入 URL -> Enter
  2. 克隆完成后,在对话框中确认打开文件夹
  3. 命令面板 -> Python:创建环境
  4. 创建venv环境,选择安装需求
  5. 命令面板 -> Python:创建终端
  6. 现在在具有活动 Python 环境的 Visual Studio Code 命令提示符下输入: pip install --force-reinstall (解析路径 C:\gtk-build uild\x64 释放\pygobject\dist\PyGObject*.whl)

当然,在活动的 Python 环境中,即使没有 Visual Studio Code,这也应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.