Skip to content

Python and the Mac

I wanted to run a python pro­gram on my Mac that was ori­gin­ally writ­ten for Linux. Should work just fine, right? Well, not quite. Given it took quite a while to fig­ure out pre­cisely what I needed, I’m writ­ing it up for next time I need to do this.

The first prob­lem was an error mes­sage triggered by the fail­ure of import gtk. The first assump­tion was that I needed to install python-gtk2. For­tu­nately I’d already installed XCode. The easi­est way to pull in pack­ages for other code is to install some port soft­ware; I picked Mac­Ports. Installing that doesn’t take too long, and it sets up the right depend­en­cies and envir­on­ment vari­ables. Then comes the fun stuff, installing the other pack­ages. This takes some time, although being almost an auto­matic pro­cess makes it rel­at­ively easy.

The first thing to install was python_select, since I wanted to use a ver­sion of python other than the default 2.4, then the right ver­sion of python-gtk. For python 2.5, that meant

sudo port install python_select
python_select python25
sudo port install py25-gtk2

The pro­gram still didn’t run, with the same gtk error mes­sage, so after vainly edit­ing path vari­ables etc, I looked at the source, and added

try:
      import pygtk
      pygtk.require("2.0")
except:
      pass

before the

try:
      import gtk

lines. This worked, and the pro­gram moved onto the next miss­ing package.

Repeat­ing for libxml2, and libxslt:

sudo port install py25-libxml2
sudo port install py25-libxslt

and my pro­gram finally worked.

Check­ing what was installed where is easy: port contents package-name where package-name is the name of the package.

It looks like things work a lot bet­ter than they did just a few months ago, if all the art­icles I found on how to get libxml to work with python on the Mac are any­thing to go by.

{ 1 } Comments

  1. Paolo | Aug 21, 2009 at 4:11 am | Permalink

    If you need a Synaptic-like fron­tend for installing ports I sug­gest to take a look at Por­ti­cus (http://porticus.alittledrop.com/)

    HTH

{ 1 } Trackback

  1. Short-form Fragments | dv8-designs | Aug 24, 2009 at 12:41 am | Permalink

    […] hap­pens when a Rubyist’s spouse starts slip­ping into Python? http://www.laurenwood.org/anyway/2009/08/20/python-and-the-mac/ […]

Post a Comment

Your email is never published nor shared. Required fields are marked *