Thursday, September 2, 2010

Installing Pygame on Mac OS X Leopard

In my ongoing campaign to broaden my programming abilities I decided to install
Pygame and play around with it. I want to try it out as something to present to my students this year. I've decided to add a small programming segment to the first semester of my high school computer class this year where we do a quick survey of several languages by doing tutorials in them before we go into Java programming in spring semester.

Pygame is an add-on package for python that makes it easy to develop games using Python. Python has no built-in support for native graphics, sound, etc.--Pygame adds that to Python.

When I went to install Pygame on my Mac, I ran into a couple of hitches. I managed to work around them without too much problem, but I thought what I did to get things going was worth sharing.

First, Pygame requires the python.org version of Python. Even though my Mac already came with Python installed (hurray for Apple for that), the version isn't one that Pygame is happy with. So I went out to python.org, clicked on downloads, and pulled the first thing I saw for my OS version. This turned out to be Python 2.7 for Mac OS X 10.5 or later. Yeah, I could have gone with 3.1, but to be honest I didn't look that far down the page at first.

Once I had it downloaded, I started the download of Pygame. I installed Python 2.7 from the Mac installer while that was going.

Once the Python install finished, I did python -V at the command line in Terminal. My answer:

#python -V
Python 2.5.1


Hmmm. That's the Mac preinstalled version, not the new one. OK, fine:

#whereis python
/usr/bin/python

#ls -l /usr/bin/python
lrwxr-xr-x 1 root wheel 72 Feb 21 2008 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python


Aha! I needed to set up the appropriate links to make the new Python the current one.

#cd /System/Library/Framework/Python.framework/Versions/
#ls
2.3 2.5 Current
#ls -l Current
lrwxr-xr-x 1 root wheel 3 Feb 21 2008 Current -> 2.5
#ls /Library/Frameworks/Python.framework/Versions/
2.7
#ln -s /Library/Frameworks/Python.framework/Versions/2.7 ./2.7
#ls
2.3 2.5 2.7 Current
#rm Current
#ln -s 2.7 Current
#ls -l Current
lrwxr-xr-x 1 root wheel 3 Sep 2 12:45 Current -> 2.7
#python -V
Python 2.7


Hurray! Next I went to install Pygame. I unzip the zip, then run the installer.

No Install

Pygame then complained that it wanted Python 2.6! Python 2.7 wasn't good enough for it. I took a look at the dependency settings by right clicking on the Pygame installer in Finder, selecting "Show Package Contents", then opening "Contents" and double-clicking on info.plist there to open it in the Property List Editor. I expanded the Root item (the only top level item), then expanding "IFRequirementDicts" and expanding item "0". There I saw the Python version requirements properties. I considered changing it to require Python 2.7 by editing the SpecArgument field to 2.7 instead of 2.6.

But, it's not like my hard disk is mine to control, so I wimped out and installed Python 2.6 by going back to the Python All Releases download page, finding the 2.6 version, downloading and installing it.

Once it was in place, I went back to Terminal:

#cd /System/Library/Framework/Python.framework/Versions/
#ls
2.3 2.5 2.7 Current
#ln -s /Library/Frameworks/Python.framework/Versions/2.6 ./2.6
#ls
2.3 2.5 2.6 2.7 Current


Now Pygame will install just fine.

2 comments:

  1. Thanks man! I was struggling with this and had been researching for hours. Nothing had the right combination of facts that would tell me what to do.

    Your article provided some context that helped me to troubleshoot and finally get the thing installed.

    In particular, your speculation about how one might alter the installation package to have pygame install for Python 2.7 seems valuable. (Wish I'd had the fortitude to give it a try.)

    Definitely post more about what you end up doing with your students using pygame. I'd love to hear about it.

    Meanwhile, I've added your blog to my blogroll at Tech Whine (http://techwhine.blogspot.com). See you there!

    ReplyDelete
  2. Thanks for your suggestion, I tried it and got it to work for 2.7.

    In the plist, you pretty much just change everything that says 2.6 to 2.7. Then (I may have missed something), you have to move the files from
    /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
    to
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

    Specifically in the plist, I changed:
    Root/IFRequirementDicts/0/SpecArgument
    Root/PythonInfoDict/PythonExecutable
    Root/PythonInfoDict/PythonLongVersion
    Root/PythonInfoDict/PythonShortVersion

    PythonLongVersion is the first part of what sys.version returns.

    To test to see if it works, try to import pygame:

    $ python
    Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 14:13:39)
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pygame
    >>>

    ReplyDelete