IPython
Usage
- Append
? to see docs of any functions/magic commands/objects.
- Magic commands
%timeit, $time %debug, %page OBJECT, $paste, $who, etc…
- Use
%bookmark to mark directories.
- Magic commands can be executed without prefix, if it doesn’t conflict with
variables.
- Previous two outputs are stored in
_ and __. The input/output of X-th
command are stored in _iX and _i.
- The variables can be used with regular Python functions, e.g.
eval(_i27)
$xdel and %reset are needed, since the input/output history tracking may
prevent garbage collection.
- Shell commands
- Prefix
! to execute a shell command — this can be used in the middle of a
Python expression.
- Prefix
$ to expand a Python variable in a shell command.
%alias helps creating shell command aliases.
- Debugging
%debug goes into pdb right after error for postmortem debugging.
%pdb takes user into pdb after each execution.
%run -d opens pdf before execution for setting breakpoints. Adding -bn
would set a breakpoint at line n.
%prun profiles a code block with cProfile, with same API as cProfile
CLI. This can be used in Jupyter Notebook with %%prun.
- The
line-profiler tool has an IPython extension to provide
%lprun -f func1 -f func2 statement for detailed profiling results in
specific function(s).
- A
dreload function performs deep recursive reload for imports.