Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Install PyGTK via Homebrew and virtualenv
# This LOOKS pretty straightforward, but it took awhile to sort out issues with
# py2cairo and pygobject, so I hope I've saved you some time :-)
#
# This assumes you already subscribe to a nice clean virtualenvwrapper workflow
# -- see https://gist.github.com/771394 if you need advice on getting there.
# There are some optional dependencies omitted, so if you're going to be doing
# heavy development with these libs, you may want to look into them.
#
# We go to some configure option pains to avoid polluting the system-level
# Python, and `brew link`ing Cairo which is keg-only by default.
brew install cairo && brew install gtk+
# Have a coffee. Better yet, a meal.
mkvirtualenv --no-site-packages pygtk
workon pygtk
# NOTE: pycairo uses a nonstandard build system, the autotools approach is
# officially unsupported. I couldn't get the latest version (1.10.0 at time of
# writing) to work with either approach, so I punted and used autotools here
# with the next-most-recent version.
curl -O http://cairographics.org/releases/py2cairo-1.8.10.tar.gz
tar xzf py2cairo-1.8.10.tar.gz && cd py2cairo-1.8.10
./configure --prefix=$VIRTUAL_ENV PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/Cellar/cairo/1.10.2/lib/pkgconfig
make
make install
# pygobject
# introspection no worky: https://github.com/mxcl/homebrew/pull/4353
curl -O http://ftp.gnome.org/pub/GNOME/sources/pygobject/2.28/pygobject-2.28.6.tar.bz2
tar xf pygobject-2.28.6.tar.bz2 && cd pygobject-2.28.6
./configure --prefix=$VIRTUAL_ENV --disable-introspection
make
make install
# pygtk
curl -O http://ftp.gnome.org/pub/GNOME/sources/pygtk/2.24/pygtk-2.24.0.tar.bz2
tar xf pygtk-2.24.0.tar.bz2 && cd pygtk-2.24.0
./configure --prefix=$VIRTUAL_ENV PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$VIRTUAL_ENV/lib/pkgconfig
make # moar coffee nao
make install
# There's a short test script you can run from the REPL here, if you want to
# verify that things are working:
#
# http://faq.pygtk.org/index.py?file=faq21.001.htp&req=show
#
#!/bin/sh
# ~/bin/openxenmanager
source ~/.virtualenvs/pygtk/bin/activate
cd $HOME/src/python/openxenmanager
python window.py
deactivate

Is this still the approach you are recommending? Any changes in the last year?

Owner

ches commented Jul 30, 2012

I haven't tried, but it looks like nearly everything is in Homebrew now, so it should be a lot easier:

$ brew info pygtk
pygtk: stable 2.24.0
http://www.pygtk.org/
Depends on: glib, gtk+, pygobject, py2cairo
Not installed
https://github.com/mxcl/homebrew/commits/master/Library/Formula/pygtk.rb

==> Caveats
For non-Homebrew Python, you need to amend your PYTHONPATH like so:
  export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

So as long as you don't need strong virtualenv isolation (like testing different versions of these libs, installed into different virtualenvs), I'd have a go at brew install pygtk and see how it goes 😄

After doing
brew install pygtk and export PYTHONPATH=/usr/local/lib/python2.7/site-packages

I get

Traceback (most recent call last):
  File "window.py", line 45, in <module>
    import gtk
ImportError: No module named gtk

:(

I have the same issue :(. How can we take care of this?

File "window.py" asks for python 2.5 and gtk 2.0

open window.py and change the request line of python 2.5 to the version you have...
i have python 2.7 and it worked after that (i hade to change the permission of file and folder to do so ^_^)

brew no longer seems to have a formula for pygtx

[joule] ~ [1.9.3-p194] brew update
Updated Homebrew from 93fe0378 to ce39c3f3.
==> Updated Formulae
adobe-air-sdk  doxygen        zeromq
[joule] ~ [1.9.3-p194] brew info pygtx
Error: No available formula for pygtx 
[joule] ~ [1.9.3-p194] 

stuaxo commented Feb 21, 2014

@jedschneider - surely you want pygtk not pygtx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment