Tools of the Python Trade: IPython

One Shell to Rule them All

I’ve been doing a lot of python work lately, as part of several projects, websites and writing pieces. As a result I’ve had to get my act together as far as working efficiently with python is concerned.

In this article I’ll be discussing one of my primary components of my toolbox, IPython. IPython is an interactive python shell, an enhancement for the generic python interpreter. IPython incorporates a wide variety of simple additions which make the interpreter much easier to work with, but do not warrant being added into the core distribution.

I’m not going to list out and explain IPython’s every capability, that’s a job for their documentation. My contribution is a small selection of features to whet any python user’s appetite, and to show them a whole new way of working with python.

IPython Remembers

The first things one notices on starting IPython are the In [1] and Out [1] prompts. These are the tips of IPython’s history system, and it sure is an iceberg.

Put simply, IPython remembers everything. It remembers what you type, what it displays, what it computes, and everything in between. And all of this is configurable.

IPython remembers everything you type at the prompt, including what was typed in previous sessions. This is noticeably absent in the standard python interpreter.

In addition, it can recognize block constructs. Statements in a block such as a conditional or loop seldom make sense outside of their block context. IPython remembers when you enter blocks, and stores the entire block as one entry in the history, so that it may be recalled as one. In my opinion, this one of IPython’s best features.

IPython also remembers the results of your past computations - all of them, if necessary. For example, the text of the code you typed in on history line number 3 can be accessed through the variable _i3, and the result of the expression on that line is stored in _3. Note that the results of expressions that do not evaluate to anything (that is, they evaluate to None) are not stored, so don’t be surprised if you find some gaps in your output history.

IPython Completes You

IPython does completion on the command line like any other respectable shell. It can complete variables, functions, members, methods, classes, modules, everything. It includes __ names in its completion which clogs the list up a bit, but nothing that can’t be handled.

Due to its completion of modules and packages, it lends itself as a very useful tool to find out exactly what is importable - great for debugging PYTHONPATH issues - as well as finding out what’s in an unfamiliar module.

IPython Knows

Documentation of python code is mostly contained in the code file, and often is the code. The standard python interpreter makes docstrings easy to access using the inbuilt help function. However, reading the python code corresponding to a particular object, function or module requires finding out the file in use, and opening it up externally. IPython makes the former easier and the latter possible.

With two simple syntactic variations, IPython allows you to access the entirety of inbuilt documentation on any python object. The first, namely object? is simply an alias for help(object), but more convenient when you’ve already typed object. The second, namely object?? pulls up the code for the object if it can. Even IPython can’t do anything about C-extensions.

The incorporation of documentation retrieval into the workflow is an invaluable tool in python development.

IPython Goes Both Ways

Any interpreter can interpret a file of source code, but not too many can turn an interactive session back into a source file. Using IPython’s extensive logging capabilities, you can record your interactive session to a python source file, which can later be executed by itself. IPython knows how to treat extraneous data, and ensures that the resulting log is proper, executable python code.

Logging is especially useful when design test-cases; Step through the statements you want to execute (while viewing their output), and log them to form a test-case.

In addition, using the %run, %debug and %edit commands, you can use IPython to engage in a work-reload-test workflow without dancing with different windows containing your interactive session and your text editor.

That’s a Wrap

I’ve just scratched the surface here, there’s a lot more IPython can do (try %magic, for example). These are, however, my most commonly used features.

If you have your own IPython tricks, fire away! I’d love to know more.

3 Responses

wow nice ! finally an update on your website … need to try Ipython :) “IPython Completes You” sounds really interesting ! :)

/
March 25, 2010

Yeah, it was about time. Thankfully I have a whole lot of other ideas for articles lined up, I just need to write them.

sykora
/
March 26, 2010

Now that you have a lot of time at your disposal hoping to see updates at regular intervals of time :)

/
May 5, 2010
Name: (Required)
Email: (Required, not published)
URL: (Optional)
Comment: (ReST enabled.)