Issues in installing pygobject in MacOS

Question:

I am trying to install pygobject using pip, even with brew in MacOS, but now successful. I followed guides.

If I use the official installation guide in https://pygobject.readthedocs.io/en/latest/getting_started.html, then I have gtk+3 installed, but when I try to import the library:

python3 -c 'import gi; gi.require_version("Gtk", "3.0"); print("ok")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>

This is the output of the installation after using pip3:

(base) khalil presentation (master) >> pip3 install gobject pygobject
Collecting gobject
  Downloading gobject-0.1.0-py3-none-any.whl (5.9 kB)
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 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from pygobject) (1.20.1)
Building wheels for collected packages: pygobject
  Building wheel for pygobject (pyproject.toml) ... error
  ERROR: Command errored out with exit status 1:
   command: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py build_wheel /var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/tmpsnfmi_oc
       cwd: /private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-install-2saq002f/pygobject_1efb85aba31e4700a39a2c6c52381a62
  Complete output (90 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.9-x86_64-3.6
  creating build/lib.macosx-10.9-x86_64-3.6/pygtkcompat
  copying pygtkcompat/pygtkcompat.py -> build/lib.macosx-10.9-x86_64-3.6/pygtkcompat
  copying pygtkcompat/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/pygtkcompat
  copying pygtkcompat/generictreemodel.py -> build/lib.macosx-10.9-x86_64-3.6/pygtkcompat
  creating build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_signalhelper.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_constants.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/pygtkcompat.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_ossighelper.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/importer.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_error.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_gtktemplate.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_propertyhelper.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/types.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/docstring.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/_option.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  copying gi/module.py -> build/lib.macosx-10.9-x86_64-3.6/gi
  creating build/lib.macosx-10.9-x86_64-3.6/gi/repository
  copying gi/repository/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/gi/repository
  creating build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/GIMarshallingTests.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/Gdk.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/Pango.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/GdkPixbuf.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/Gtk.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/__init__.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/GLib.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/GObject.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/Gio.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  copying gi/overrides/keysyms.py -> build/lib.macosx-10.9-x86_64-3.6/gi/overrides
  running build_ext
  pycairo: new API
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 363, in <module>
      main()
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 345, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 262, in build_wheel
      metadata_directory)
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 231, in build_wheel
      wheel_directory, config_settings)
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 215, in _build_with_temp_dir
      self.run_setup()
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 268, in run_setup
      self).run_setup(setup_script=setup_script)
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/setuptools/build_meta.py", line 158, in run_setup
      exec(compile(code, __file__, 'exec'), locals())
    File "setup.py", line 1259, in <module>
      main()
    File "setup.py", line 1254, in main
      zip_safe=False,
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
      return distutils.core.setup(**attrs)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/wheel/bdist_wheel.py", line 299, in run
      self.run_command('build')
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/command/build.py", line 135, in run
      self.run_command(cmd_name)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "setup.py", line 1088, in run
      self._setup_extensions()
    File "setup.py", line 1083, in _setup_extensions
      add_pycairo(gi_cairo_ext)
    File "setup.py", line 1066, in add_pycairo
      ext.include_dirs += [get_pycairo_include_dir()]
    File "setup.py", line 893, in get_pycairo_include_dir
      include_dir = find_path(find_new_api())
    File "setup.py", line 848, in find_new_api
      import cairo
    File "/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/cairo/__init__.py", line 1, in <module>
      from ._cairo import *  # noqa: F401,F403
  ImportError: dlopen(/private/var/folders/t0/_kkpxc35687gknp4d99b9lbr0000gn/T/pip-build-env-8drx7d57/overlay/lib/python3.6/site-packages/cairo/_cairo.cpython-36m-darwin.so, 0x0002): symbol not found in flat namespace '_cairo_append_path'
  ----------------------------------------
  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
(base) khalil presentation (master) >> ```
Asked By: Khalil Mebarkia

||

Answers:

Problem solved! Thanks to @rasjani

First, I upgraded to the latest version of python, then I create a new_env using conda:

conda create --name new_env python=3.9.12 
conda activate new_env 
conda install -c conda-forge pygobject

Solution from: Github

Answered By: Khalil Mebarkia
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.