'When I Try To Install 'PyGObject' With pip I Get an Error

When I am trying to install PyGObject I get an error. I am using Python 3.10 and Windows Command Prompt.

C:\windows\system32>pip install PyGObject

    Collecting PyGObject
  Using cached PyGObject-3.42.1.tar.gz (718 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: pycairo>=1.16.0 in c:\users\mona.hynes.lisdoon\appdata\local\programs\python\python310\lib\site-packages (from PyGObject) (1.21.0)
Building wheels for collected packages: PyGObject
  Building wheel for PyGObject (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for PyGObject (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [48 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-310
      creating build\lib.win-amd64-cpython-310\pygtkcompat
      copying pygtkcompat\generictreemodel.py -> build\lib.win-amd64-cpython-310\pygtkcompat
      copying pygtkcompat\pygtkcompat.py -> build\lib.win-amd64-cpython-310\pygtkcompat
      copying pygtkcompat\__init__.py -> build\lib.win-amd64-cpython-310\pygtkcompat
      creating build\lib.win-amd64-cpython-310\gi
      copying gi\docstring.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\importer.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\module.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\pygtkcompat.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\types.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_constants.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_error.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_gtktemplate.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_option.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_ossighelper.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_propertyhelper.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\_signalhelper.py -> build\lib.win-amd64-cpython-310\gi
      copying gi\__init__.py -> build\lib.win-amd64-cpython-310\gi
      creating build\lib.win-amd64-cpython-310\gi\repository
      copying gi\repository\__init__.py -> build\lib.win-amd64-cpython-310\gi\repository
      creating build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\Gdk.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\GdkPixbuf.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\GIMarshallingTests.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\Gio.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\GLib.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\GObject.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\Gtk.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\keysyms.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\Pango.py -> build\lib.win-amd64-cpython-310\gi\overrides
      copying gi\overrides\__init__.py -> build\lib.win-amd64-cpython-310\gi\overrides
      running build_ext
      pycairo: new API
      pycairo: trying include directory: 'C:\\Users\\mona.hynes.LISDOON\\AppData\\Local\\Temp\\pip-build-env-6iivyiiw\\overlay\\Lib\\site-packages\\cairo\\include'
      pycairo: found 'C:\\Users\\mona.hynes.LISDOON\\AppData\\Local\\Temp\\pip-build-env-6iivyiiw\\overlay\\Lib\\site-packages\\cairo\\include\\py3cairo.h'
      building 'gi._gi' extension
      creating build\temp.win-amd64-cpython-310
      creating build\temp.win-amd64-cpython-310\Release
      creating build\temp.win-amd64-cpython-310\Release\gi
      "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DPY_SSIZE_T_CLEAN -IC:\Users\mona.hynes.LISDOON\AppData\Local\Temp\pip-install-zi8f2b72\pygobject_c02dc4480e0747d4bcd87d8b59ba11df -IC:\Users\mona.hynes.LISDOON\AppData\Local\Temp\pip-install-zi8f2b72\pygobject_c02dc4480e0747d4bcd87d8b59ba11df\gi -IC:\Users\mona.hynes.LISDOON\AppData\Local\Programs\Python\Python310\include -IC:\Users\mona.hynes.LISDOON\AppData\Local\Programs\Python\Python310\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\cppwinrt" /Tcgi\gimodule.c /Fobuild\temp.win-amd64-cpython-310\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\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.31.31103\\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


Solution 1:[1]

I had the same issue and the problem is that PyGObject needs to be compiled which will be done by default using Microsoft Visual Studio. This seems not to work. I also read somewhere that this won't work. I managed to compile it using the instructions on https://pygobject.readthedocs.io/en/latest/getting_started.html but then got errors importing it into my project ('circular import'). Msys2 relies on python 3.9 by default, while I'm using 3.10. Could be related. Did not have time to look further, yet. So I'm happy for solutions to that :) Maybe will try changing to 3.9. later.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Miklody