Archive for January 20, 2009

Python REPL Enhancement

This tip comes from here.

You can get tab completion and command history similar to that available in bash itself by creating registering a start up script for python. Create the following as ~/.pythonstartup

# python startup file
import readline
import rlcompleter
import atexit
import os

# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
	readline.read_history_file(histfile)
except IOError:
	pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

Then add the following to you ~/.bash_profile

export PYTHONSTARTUP=~/.pythonstartup

Now you can use tab to autocomplete and use tab-tab to show suggestions:

geoffs-mac:~ geoff$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.display. <-- I pressed tab here after pyg and the after .dis, but you can't see it :) 
pygame.display.Info               pygame.display.get_caption
pygame.display._PYGAME_C_API      pygame.display.get_driver
pygame.display.__PYGAMEinit__     pygame.display.get_init
... and more ...

You can also use the up and down arrows to cycle through previous commands, including those from previous sessions.

January 20, 2009 at 1:07 pm

Pygame on OS X Leopard

Skip to successful instructions

Tonight I wanted to start a new Pygame project to learn basic game development. I thought this would be rather easy, but apparently I’m not the only one who has had trouble.

Attempt 1 – Macports

Reading the Pygame download page I notice that it mentioned Macports. So I fired up Terminal, typed in sudo port install py-game and then waited about 5 mins for it to download and build everything.

Time to try it out.

geoffs-mac:~ geoff$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named pygame

Not cool. A little bit of searching and I found that for some reason Macports installed pygame under /opt/local/lib/python2.4/site-packages/pygame. This might be good if you use the Macports python but clearly not suitable for the default pre-installed Python 2.5.

As a last ditch effort to use the Macport Pygame I tried copying the /opt/local/lib/python2.4/site-packages/pygame into /Library/Python/2.5/site-packages which resulted in a whole heap of Python C API version mismatch warnings.

Oh well, rm -rf /Library/Python/2.5/site-packages/pygame

Attempt 2

Ok, time to try the pre-compiled installers.

I downloaded the OS X 10.5 installer from the Pygame downloads. However when I ran it, on the select destination screen it kept telling me that my startup volume was not acceptable as “System Python 2.5 is required”. Yet python was clearly running Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)

I installed the Python package linked to on this page. I’m not sure what the package does that the default Python didn’t, but others were recommending it worked for me as well.

Then I ran the Pygame installer again and all was good. That is, until I tried importing pygame again.

geoffs-mac:~ geoff$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
  File "", line 1, in 
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pygame/__init__.py", line 37, in 
    _check_darwin()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pygame/__init__.py", line 34, in _check_darwin
    raise ImportError("PyObjC 1.2 or later is required to use pygame on Mac OS X. http://pygame.org/wiki/PyObjC")
ImportError: PyObjC 1.2 or later is required to use pygame on Mac OS X. http://pygame.org/wiki/PyObjC
>>>

So back to the Pygame downloads page to grab pyobjc-1.4-py2.5-macosx10.4.mpkg.zip. Don’t be put of by the 10.4 in the filename – this is the one you want. The installer completed without troubles, and I could finally import pygame properly.

The Instructions

Without the waffle.

  1. Download and install python-2.5-macosx.dmg
  2. Download and install pyobjc-1.4-py2.5-macosx10.4.mpkg.zip
  3. Download and install pygame-1.8.1release-py2.5-macosx10.5.zip
  4. Test it in Terminal:
    geoffs-mac:~ geoff$ python
    Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pygame
    >>> 
    

What should have been five minutes, was more than 30 minutes of hassle. Now, time to start writing my game.

January 20, 2009 at 11:53 am 4 comments


Calendar

January 2009
M T W T F S S
     
 1234
567891011
12131415161718
19202122232425
262728293031  

Posts by Month

Posts by Category


Follow

Get every new post delivered to your Inbox.