Python Startup on Mac OS X Leopard

The promise of the open source programming language Python for numerical computing has been frustrating for lack of good, consolidated advice out there to troubleshoot issues that should be really simple. Here’s a brief tutorial on how to get readline support on Mac OS X 10.5.x.

What is readline? Readline is a program that allows you to use tab completion, history, and other features that you’re used to in your favorite shell, MATLAB from the command line, and GNU Octave.

There are two steps to this. The first is to tell python to always use a startup file. The second is the create the startup file with the right info inside. I’m assuming you use Bash and are running python from a terminal — in xterm, Terminal.app, or iTerm.app, etc.

Step 1: In your .bashrc file (or .profile, .bash_profile, etc.), using your favorite text editor, put the following line:


export PYTHONSTARTUP=~/.pythonrc

To initialize this, at your shell, type:


source ~/.bashrc

(substitute your file appropriately)

Step 2: Create the file .pythonrc in your home directory (~/):


import rlcompleter
import readline
readline.parse_and_bind ("tab: complete")

Save this file, and now run any python build you may have on your machine. Python will read this file automatically and now you can test out tab completion by typing (no quotes): ‘imp’. The word “import” should be completed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s