Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install pygtk 3 on Mac OS X?

What I tried:

brew install pygobject3 --with-python@2 gtk+3
brew install pygtk3
brew install pygobject3
pip install pygobject

python -c 'import gi; gi.require_version("Gtk", "3.0")'

Result:

ValueError: Namespace Gtk not available for version 3.0

Note that I'm running conda, with python 3.6; and Mac OS X High-Sierra

I read through the following questions/answers before posting:

  • Easiest way to install pygtk on Mac OS X
  • Python Gtk3 executable
  • https://github.com/neovim/python-gui/issues/33
  • https://github.com/neovim/python-gui#dependencies-debian
  • https://github.com/pybee/toga/issues/38
  • Error "Could not find any typelib for Gtk" with Python3 and GTK3
  • https://github.com/neovim/python-gui/issues/33
  • pyGtk: Gtk missing error when get version

(Note that if I change the requires to 2.0, then it loads ok, but I get a warning:

"RuntimeWarning: You have imported the Gtk 2.0 module. Because Gtk 2.0 was not designed for use with introspection some of the interfaces and API will fail. As such this is not supported by the pygobject development team and we encourage you to port your app to Gtk 3 or greater. PyGTK is the recomended python module to use with Gtk 2.0" )

like image 699
Hugh Perkins Avatar asked Apr 21 '18 14:04

Hugh Perkins


People also ask

How do I add Python 3 to my Mac?

The complete path of the Python (or Python3) UNIX executable can be added (for OS X 10.8 Mountain Lion and up) by: Opening the Terminal and entering the command: sudo nano /etc/paths . Enter your password when prompted to do so.


2 Answers

The problem is that the developers of pygobject3 have written their install instructions that does NOT consider pip/conda. If you follow the official documentation:

  • Have to use the homebrew python installation (the one installed when you go brew install python)
  • brew install gtk+3 is NOT compatible with non homebrew Pip/Conda python installs. You have to build the GTK3 package manually to use the pip version of pygobject3.

However if you want to use conda, thanks to this conda contributor, there is a pre-built conda gtk3 package you can use, you just have to get it from his channel:

Make a new environment using conda:

conda create --name my_env

install pygobject3 from conda

conda install pygobject

install gtk3 from the pkgw-forge channel

conda install -c pkgw-forge gtk3

To verify, open python from within the conda env and run

import gi
gi.require_version("Gtk", "3.0")

Should get no errors

like image 156
Spcogg the second Avatar answered Sep 22 '22 18:09

Spcogg the second


I had the same problem. I tried

$ brew install pygobject3 gtk+3

and it was smoothly installing. Then checked as Jeff's answer:

$ python3 -c 'import gi; gi.require_version("Gtk", "3.0"); print("ok")'

got ok.

Hope this helps,

Cheers

like image 32
Cem Tunaboylu Avatar answered Sep 24 '22 18:09

Cem Tunaboylu