Kent's Korner
by Kent S Johnson

2008-05-18 21:05:38

IPython

IPython is an enhancement to the standard Python interactive interpreter that adds many convenience features. In this article I will cover the features that make IPython a must-have add-on for me. This is far from a comprehensive list of IPython features; I only scratch the surface of what it can do. I hope this whets your appetite enough that you try IPython and find your own favorite features.

Getting IPython

IPython is hosted by the SciPy project at http://ipython.scipy.org/moin/. To install IPython, follow the instructions in the fine manual. Many of the best features of IPython require the readline library. This complicates installation on Mac OSX and Windows, which do not include readline support by default. Follow the directions to get all the pieces.

Configuration

IPython has many customization options. You will probably want to visit the ipythonrc file fairly early in your use. The options are documented in this file.

Running IPython

IPython installs an executable, also called ipython. Running ipython will start Python with IPython as the interpreter. You can also make IPython start whenever you run Python interactively. To do this, make a file called pythonstartup.py in a convenient location. (Actually you can name it whatever you want.) The file should contain these two lines:

import IPython
IPython.Shell.IPShell().mainloop(sys_exit=1)

Also create an environment variable PYTHONSTARTUP whose value is the full path to the pythonstartup.py file. See http://ipython.scipy.org/doc/manual/node8.html for a few more details.

Features

OK, why go to all this trouble? Here are some of IPython's (IMO) indespensible features:

Automatic pretty-printing of results

Values printed by the interpreter are automatically pretty-printed. Lists that are too long for one line are printed with one line per item. Dicts print with one line per key/value pair, sorted by key. Nested structures are very readable. This alone is worth installing IPython for.

Tab completion

IPython will complete symbol names by introspecting the current namespace or object.

Auto-indent

IPython auto-indents blocks. This makes it much more convenient to enter multi-line constructs such as functions and control structures.

Robust command history

IPython has a very helpful command history mechanism. It is

  • persistent - command history is retained between invocations
  • smart - if you type a command prefix, then scroll through the history, it will only show commands starting with the given prefix

These two abilities make it very easy to recall commonly used imports or other commands that set up an interactive session.

  • editable - saved commands can be edited in an external editor

Result history

The result of every command is saved and available for later use. This is similar to the _ variable in the standard shell, extended to all results.

Formatted help

Help is available on every object. Just type ?name to get information about name. This works with method names, too, for example ?str.find will give information about the find() method of strings.

Rich traceback

IPython has three styles of traceback display:

  • plain - similar to standard tracebacks
  • context - shows five lines of context around each line in the stack trace
  • verbose - shows all variables visible at each level of the stack trace. This is extremely verbose and sometimes extremely handy.

But wait, there's more!

These are the features I use frequently but they only scratch the surface of what IPython can do. I haven't even begun to cover the features listed in the overview section in the docs, nor have I mentioned integration with GUI and plotting toolkits. I encourage you to explore the manual, you will find your own favorite features.

 
© Kent S Johnson Creative Commons License

Short, introductory essays about Python.

kentsjohnson.com

Kent's Korner home

Weblog

Other Essays