pythongtkpygobject

File msvc_recommended_pragmas.h missing while installing PyGObject via pip


I tried to install PyGObject via pip and it fails with given error:

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

I had another attempt with python -m pip install pygobject --no-use-517 and it still fails with error:

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.

It seems like all fails is due to a missing file msvc_recommended_pragmas.h. This leaves me with no clue of what I may have missed. All runtime dependencies involved (python3.9, pip, MSYS2, C++ Build Tools from Visual Studio) have been upgraded to the latest version FYI.


Solution

  • gvsbuild provides a method to compile GTK in Windows. The latest instructions can be found in the README at https://github.com/wingtk/gvsbuild. Once you install the dependencies, it will work something like this:

    mkdir C:\gtk-build\github
    cd C:\gtk-build\github
    git clone https://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
    

    Once that builds, you can switch to your project you are trying to use PyGObject for and activate the virtualenv. Then install the wheels you created above:

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