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.
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.
OK, why go to all this trouble? Here are some of IPython's (IMO) indespensible features:
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.
IPython will complete symbol names by introspecting the current namespace or object.
IPython auto-indents blocks. This makes it much more convenient to enter multi-line constructs such as functions and control structures.
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
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.
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.
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.
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.