IPython3 (Notes)

last update: 2021-08-14

IPython3 provides a series of advantages over the standard Python shell - in particular with regard to scientific computing. A good start is too look into the IPython documentation. Start by making sure it is properly installed:

$ sudo apt install ipython3 python3-ipdb ipython3-qtconsole ipython3-notebook

IPython can be started via eg.

$ ipython3
$ ipython3 qtconsole --pylab=inline
$ ipython3 notebook --pylab=inline

However, I prefer adding an alias to start it with less typing. This can, for example, be achieved by

$ echo "alias i='ipython3'" >> ~/.bashrc


_, __
last, second last output
_i[X] / _[X]
input / output number X
var = !command
execute operating system shell command and store output in var
!command $var
use content of Python variable var as arguments to operating system command

Magic Functions

One of IPython's key features are so-called magic functions. They are helpful when Python would not be natural / straight-forward in solving a problem. Examples include %cpaste, %paste and . Refer to the documentation (%quickref) for more. It is possible to obtain help for the magic functions by appending a ? directly after their name.

Open a special prompt for manually pasting Python code to be executed
Invoke post mortem debugger
Print command input history for copying to an editor
Create log of entire console session
Execute pre-formatted Python code from clipboard
Automatically enter debugger after any exception
Display the IPython Quick Reference Card
%run [-d] script.py
Run passed script inside IPython (-d enables the debugger); -p -s cumulative runs the script with profiling enabled.
%timeit statement
Time execution of a Python statement or expression. It has a heuristic to run the statement multiple times to produce a fairly reliable average runtime.
%prun statement
Run a statement through the python code profiler


IPython's integrated debugger. Allows inspecting the status of variable by simply typing their name. Should their name overlap with a debugger command, the latter takes precedence. To be able to still access the variable, type


Inside the debugger, simply type h for help. If you have trouble getting out of the debugger, the following should help

import os

For debugging a function available inside ipython, the following snipped (kudos to Wes McKinney) will be helpful

def debug(f, *args, **kwargs):
    from IPython.core.debugger import Pdb
    pdb = Pdb(color_scheme='Linux')
    return pdb.runcall(f, *args, **kwargs)