IPython Interactive Python shell

Version 9.0 · source

Start here

obj?
Help on an object
obj.<TAB>
Complete attributes
%run <script.py>
Run a script here
%timeit <expr>
Time an expression
!<command>
Run a shell command
%debug
Debug last error
%history
Show past input
%load_ext autoreload
Reload edited modules

Concepts

  1. In[n] your input
  2. Out[n] result
  3. _ __ ___ last 3 outputs

Help and inspection

<obj>?
Show docs for an object
<obj>??
Show source code too
<obj>.<TAB>
Complete attributes and methods
*<pattern>*?
Search names by wildcard
For example np.*load*? lists matching names in np.
%psource <obj>
Print an object's source
%pdoc <obj>
Print an object's docstring
%pfile <obj>
Show the file defining it
%quickref
Quick reference card
%magic
Full magic system docs
%lsmagic
List all magics
%<magic>?
Docs for one magic

Running code

%run <script.py> <args>
Run a script in this session
%run -i <script.py>
Run in the session namespace
%run -t <script.py>
Time the script
%run -p <script.py>
Profile the script
%run -d <script.py>
Step through under pdb
%run -m <module>
Run a module like python -m
%load <file>
Load code into the prompt
%edit
Compose input in your editor
%paste
Paste clipboard code as-is
%cpaste
Paste block, end with --
%%writefile <file>
Write the cell to a file
%save <file> <range>
Save history lines to a file
%reset
Clear all variables
%reset -f
Clear without asking

Timing

%time <stmt>
Time one run
%timeit <stmt>
Time many runs, average
%timeit -n <N> -r <R> <stmt>
Set loops and repeats
%%time
Time the whole cell
%%timeit
Time a cell, with setup line
%prun <stmt>
Profile a statement

History and output

%history
Show session input
%history -n
Include line numbers
%history -g <text>
Search all stored history
%history -l <N>
Show last N lines
%rerun
Re-run the previous line
%rerun -g <text>
Re-run latest match
In[<n>]
Input of cell n
Out[<n>]
Output of cell n
_ / __ / ___
Last three outputs
%macro <name> <n1-n2>
Bind lines to a name
%store <var>
Persist a variable

Shell and system

!<command>
Run a shell command
<var> = !<command>
Capture output as a list
!<command> {<pyvar>}
Insert a Python value
$var works too; escape a literal dollar with $$.
%%bash
Run the cell in bash
%cd <dir>
Change directory
%pwd
Print working directory
%ls
List the current directory
%env <VAR>=<value>
Set an env variable
%alias <name> <cmd>
Define a shell alias
%pushd <dir> / %popd
Directory stack
%pip install <pkg>
Install into this environment
%conda install <pkg>
Conda install, same env

Debugging

%debug
Open pdb after an exception
%pdb on
Debug every uncaught error
%pdb off
Stop auto-debugging
%xmode Verbose
Richer tracebacks
Modes: Plain, Context, Verbose, Minimal.
%tb
Show last traceback again
%debug --breakpoint <file:line>
Start debugging at a line

Modules and workspace

%load_ext autoreload
Enable the extension
%autoreload 2
Reload all before each run
%autoreload 1
Reload only %aimport modules
%autoreload 0
Disable reloading
%aimport <module>
Mark module for reload
%who
List variable names
%whos
List variables with types
%who_ls
Variable names as a list
%matplotlib inline
Plot inside the frontend
%matplotlib -l
List plotting backends

Config and shortcuts

ipython profile create
Create default config files
ipython --profile=<name>
Start with a profile
ipython -i <script.py>
Run, then stay interactive
ipython -c "<code>"
Run a one-liner
ipython locate
Print the IPython dir
%colors <scheme>
Change the colour scheme
%automagic
Toggle dropping the %
Keyboard
Key Does
Tab Complete or cycle
Ctrl+R Search history
Ctrl+O Open new line
Esc Enter Force run block
Ctrl+L Clear screen
Ctrl+D Exit

Recipes

Edit a module and keep going
%load_ext autoreload%autoreload 2import mymodmymod.run()# edit mymod.py, then call again
Debug a failing script
%run script.py%debug# or start under pdb from the top:%run -d script.py
Compare two implementations
%timeit sorted(data)%timeit -n 100 -r 5 list_sort(data)
Shell output into Python
files = !ls *.csvfor f in files: print(f)%%bashgrep -c error app.log