For an introduction to IPython, be sure to checkout the article Introduction to IPython: An Enhanced Python Interpreter.

One of the nice things about working in an interpreted language like Python is exploratory programming using the interactive Python shell. It lets you try things out quickly and easily without writing a script and then executing it, and you can easily inspect values as you go along. The shell has some conveniences, as well, including line-by-line history and a built-in pager for reading help.

There are some limitations to the standard Python shell, though: you’ll end up using dir(x) frequently to inspect objects and find the method name you’re looking for. It’s also challenging to get the code you wrote from the shell into a convenient file with good formatting. Finally, if you’re used to the conveniences of an IDE, you’ll miss things like syntax highlighting, code completion, function signature assistance, and the like. Also, when testing GUI development using a toolkit other than TkInter, the GUI toolkit’s main loop blocks the interactive interpreter.

There are some alternative shells for Python, however, that provide some of these extensions to the behavior of the basic interface. IPython, probably the oldest of these, offers some very deep features but omits some superficial ones, while bpython and DreamPie are newer and do an excellent job on some of what IPython omits. We’ll look at each, starting — age before beauty! — with IPython.

### IPython Overview

IPython aims to fulfill a few purposes: it wants to work as a more powerful alternative to the regular Python shell; as a debugging interface or otherwise interactive console for your own Python programs; as a base for programming systems using Python, such as scientific-programming or similar uses; and as an interactive interface to GUI toolkits. IPython has also developed facilities for collaborative, interactive parallel programming.

IPython runs on Linux, Windows, and OS X, and can be installed easily using easy_install or pip. (If you want to use the optional Qt interface, you’ll need to install PyQt as well). Once it’s installed, you can start it by executing ipython (or, for the Qt version, ipython qtconsole from the command line. Here is an overview of its features:

### IPython Code Completion and Syntax Highlighting

IPython provides case-sensitive tab-completion of names not only of objects and keywords, but of available modules (including those in the current working directory) and file and directory names. There is no way to select one of the choices except to type until it is the only option. The choices are presented either in a single row across beneath the cursor or, if there are too many, in several columns.

In the Qt console, this presentation becomes a problem when there are too many choices to fit on the screen. For example, if you do: import gtk gtk.it will give you an un-manageable long list of possible choices and push your cursor off the screen. Still, you can recover by pressing q or Esc, which clears the choice view and returns to your cursor. In the regular terminal version, the list of choices is printed to the screen and then your cursor line is presented again, which is easier to use, though it is difficult to navigate the list of choices.

All in all, code completion using IPython is accurate and workable but not as convenient as it might be.

Syntax highlighting for code being entered is not available in the regular console view, but it is offered in the Qt console. It is not particularly customizable, however. There are three options, NoColor, Linux, and LightBG, and you can switch between them using IPython’s %colors magic, e.g.: %colors linux switches to the Linux coloring scheme. The %pycat% magic, which shows a Python file’s contents in a pager, does syntax-highlighting in both interfaces.

### IPython Magic

You may have noticed the reference to “magic” above; IPython provides a variety of magic functions that control or change the behavior of the shell. We saw %colors and %pycat above, but there are more. Here are some highlights:

• %autocall: Insert parentheses in calls automatically, e.g. range 3 5
• %debug: Debug the current environment
• %edit: Run a text editor and execute its output
• %gui: Specify a GUI toolkit to allow interaction while its event loop is running
• %history: Print all or part of the input history
• %loadpy: Load a Python file from a filename or URL (!)
• %logon and %logoff: Turn logging on and off
• %macro: Names a series of lines from history for easy repetition
• %pylab: Loads numpy and matplotlib for interactive use
• %quickref: Load a quick-reference guide
• %recall: Bring a line back for editing
• %rerun: Re-run a line or lines
• %run: Run a file, with fine control of its parameters, arguments, and more
• %save: Save a line, lines, or macro to a file
• %timeit: Use Python’s timeit to time execution of a statement, expression, or block

And those, as I said, are just the highlights. IPython has a lot of functionality, and much of it is not immediately visible. The docs are excellent, though, both online and built-in.

### IPython’s Help System

IPython’s help system is especially convenient — you simply type the name of the object or function you want to see described, and then add ?; IPython will display docstrings, definitions, source, source file, and more for the object in question. Not enough? Try again with two question marks, and you’ll get more.

For example, importing urllib2 and then entering urllib2? produces this:

Type: module
Base Class: <type 'module'>
String Form:<module 'urllib2' from '/usr/lib/python2.7/urllib2.pyc'>
Namespace: Interactive
File: /usr/lib/python2.7/urllib2.py
Docstring:  An extensible library for opening URLs using a variety of protocols

# ... 350 words snipped ...

Here's an example of it's usage:

import urllib2

# Set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
proxy_support = urllib2.ProxyHandler({'http' : 'http://proxyhostname:3128'})

# Build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler)

# Install it
urllib2.install_opener(opener)

f = urllib2.urlopen('http://www.python.org/')

Note that I snipped 350 words of description out of that result! Entering urllib2?? provides all of the above plus complete source code for the module.

IPython’s help also works for built-in magic commands, and it has a built-in overview of IPython (accessed with simply ?), and a quick reference guide (%quickref). Put simply, IPython’s help is great.

### Other Tools

IPython offers a number of other tools that are not available from other alternative Python shells, including tools for parallel programming and interactive work with multiple GUI toolkits.

### Parallel Programming in IPython

A major part of IPython is its structure for parallel programming, which is built in, and looks really powerful — but it’s outside the scope of this article, for the most part. (A side note: some of the dependencies of the IPython.parallel package are not installed with IPython by default.)

### Interactive GUI Programming

Interactive GUI programming is one of the activities IPython sets out to allow, and it works very well for it. To enable it, use the %gui magic function with an argument specifying which GUI toolkit you’re using:

• %gui wx: enable wxPython event loop integration
• %gui qt4|qt: enable PyQt4 event loop integration
• %gui gtk: enable PyGTK event loop integration
• %gui tk: enable Tk event loop integration
• %gui OSX: enable Cocoa event loop integration (requires %matplotlib 1.1)
• %gui: disable all event loop integration

Then, you can build your UI as usual, except that when you call the toolkit’s main loop (e.g. root.mainloop() in TkInter or gtk.main() in PyGTK), your user interface will run while you continue to issue commands interactively.

### IPython Review Summary

IPython can do much more than I’ve described here — it comprises a lot of power, though it has a considerable learning curve. It’s probably not for everyone; for many people one of the other options I’ll discuss probably offers all the features they’ll actually use with much less complexity — but it is certainly worth a look. Let’s go discuss bpython and DreamPie.