Skip to content

Instantly share code, notes, and snippets.

@ches
Created July 20, 2011 11:34
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ches/1094799 to your computer and use it in GitHub Desktop.
Save ches/1094799 to your computer and use it in GitHub Desktop.
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
@gregglind
Copy link

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

@ches
Copy link
Author

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 😄

@basukalabikash
Copy link

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

:(

@anandpdoshi
Copy link

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

@freak115
Copy link

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 ^_^)

@jedschneider
Copy link

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
Copy link

stuaxo commented Feb 21, 2014

@jedschneider - surely you want pygtk not pygtx

@abcbarryn
Copy link

abcbarryn commented Sep 8, 2020

This may help some people:
Copy these files to /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula

pygobject.rb
---Begin---

class Pygobject < Formula
  desc "GLib/GObject/GIO Python bindings for Python 2"
  homepage "https://wiki.gnome.org/Projects/PyGObject"
  url "https://download.gnome.org/sources/pygobject/2.28/pygobject-2.28.7.tar.xz"
  sha256 "bb9d25a3442ca7511385a7c01b057492095c263784ef31231ffe589d83a96a5a"
  revision 2

  bottle do
    cellar :any
    sha256 "2dbdab3cd94b10eae121c1e8460085b84c1908b487150e486005dd125f99890d" => :catalina
    sha256 "caf4b79e8454b58bb67e73a6f06853d7a410c1fa9b31478c6ac389424ca690bb" => :mojave
    sha256 "04550d558f335fd6431ee3c124ba19011ec4284e7584eff343ad6b78483472a5" => :high_sierra
    sha256 "fbe187ff2aa28f4e9f57e1e3f8f69df8e69da6406833b3dd9a976f5bae267ee7" => :sierra
  end

  depends_on "pkg-config" => :build
  depends_on "glib"
  depends_on "python@2"

  patch do
    url "https://raw.githubusercontent.com/Homebrew/formula-patches/85fa66a9/pygobject/2.28.7.diff"
    sha256 "ada3da43c84410cc165d8547ad3c7809435e09c9e8539882860d97cd1ce922b2"
  end

  def install
    system "./configure", "--disable-dependency-tracking",
                          "--prefix=#{prefix}",
                          "--disable-introspection"
    system "make", "install"
    (lib/"python2.7/site-packages/pygtk.pth").append_lines <<~EOS
      #{HOMEBREW_PREFIX}/lib/python2.7/site-packages/gtk-2.0
    EOS
  end

  test do
    system Formula["python@2"].opt_bin/"python2.7", "-c", "import dsextras"
  end
end

---End---

pygtk.rb
---Begin---

class Pygtk < Formula
  desc "GTK+ bindings for Python"
  homepage "http://www.pygtk.org/"
  url "https://download.gnome.org/sources/pygtk/2.24/pygtk-2.24.0.tar.bz2"
  sha256 "cd1c1ea265bd63ff669e92a2d3c2a88eb26bcd9e5363e0f82c896e649f206912"
  revision 3

  bottle do
    cellar :any
    rebuild 2
    sha256 "12bab3d76587659b38e56867c9b359941803275716896e2936cd3e8029cf5f3f" => :catalina
    sha256 "87f89d246e3a779381ec2efdee7ee2b69fda464f38a59dd8e14304435d759419" => :mojave
    sha256 "969cef803e110b2767c6d3ade304b92d7f23a02ff7eb4030772b69b52df7c3b2" => :high_sierra
  end

  depends_on "pkg-config" => :build
  depends_on "atk"
  depends_on "glib"
  depends_on "gtk+"
  depends_on "libglade"
  depends_on "py2cairo"
  depends_on "pygobject"

  # Allow building with recent Pango, where some symbols were removed
  patch do
    url "https://raw.githubusercontent.com/Homebrew/formula-patches/85fa66a9/pygtk/2.24.0.diff"
    sha256 "ec480cff20082c41d9015bb7f7fc523c27a2c12a60772b2c55222e4ba0263dde"
  end

  def install
    ENV.append "CFLAGS", "-ObjC"
    system "./configure", "--disable-dependency-tracking",
                          "--prefix=#{prefix}"
    system "make", "install"

    # Fixing the pkgconfig file to find codegen, because it was moved from
    # pygtk to pygobject. But our pkgfiles point into the cellar and in the
    # pygtk-cellar there is no pygobject.
    inreplace lib/"pkgconfig/pygtk-2.0.pc", "codegendir=${datadir}/pygobject/2.0/codegen", "codegendir=#{HOMEBREW_PREFIX}/share/pygobject/2.0/codegen"
    inreplace bin/"pygtk-codegen-2.0", "exec_prefix=${prefix}", "exec_prefix=#{Formula["pygobject"].opt_prefix}"
  end

  test do
    (testpath/"codegen.def").write("(define-enum asdf)")
    system "#{bin}/pygtk-codegen-2.0", "codegen.def"
  end
end

---End---

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