IPython Tip Sheet

To make better use of ipython, do the following one-time operations:
  1. Edit your ~/.bashrc file, and add the following lines in some appropriate place:
    export LESS="-R"
    export EDITOR=emacs
    
    This will tell the pager program less to interpret "raw" control sequences appropriately, and to use emacs as your default editor in certain situations. IPython uses raw control sequences to make colored text in its displays.
  2. Start up a new terminal window to get the updated environment variables, or, alternatively, type . ~/.bashrc to update the current shell.
  3. Edit your ~/ipython/ipythonrc file, and search for the set of commands describing xmode. Comment out (using #) the lines for "xmode Plain" and "xmode Context", and uncomment the line for "xmode Verbose", to get more useful information about python errors.
IPython "magic" commands are conventionally prefaced by %, but if the flag %automagic is set to on, then one can call magic commands without the preceding %. (%automagic appears to be on by default.)

The following ipython commands may be of use to you.

An aside on debugging and profiling

The python pdb module implements a debugger, typically used by calling pdb.run on a quoted python expression, e.g.:
import pdb
pdb.run('Networks.FindPathLengthsFromNode(g, 0)')
This puts you into the debugger, from which you can do the usual sort of things (list source code, set breakpoints, step one line at a time, continue until a breakpoint or exception is reached, etc.). Type ? at the pdb prompt for a list of available commands. As noted above, setting %pdb on within ipython will make it such that the pdb debugger will automatically be started at the point of an exception, once it is encountered.

Similarly, the python profile module is useful for identifying how much time is spent in various functions. The syntax is similar to that for debugging:

import profile
profile.run('Networks.FindPathLengthsFromNode(g, 0)')