Magics give timing, profiling, reloading and shell access in one keyword, and appear constantly in notebooks you will read.
The IPython kernel adds magic commands that are not Python syntax:
- Line magics start with
%and apply to the rest of the line. - Cell magics start with
%%, must be the first line of a cell, and apply to the whole cell.
The ones you will use most:
%timeit exprruns a statement many times and reports mean and spread;%%timeittimes a cell.%%time(or%time) measures a single run, wall and CPU time.%run script.pyruns a file in the notebook, leaving its variables available.%pip install pkginstalls into the kernel's environment.%load_ext autoreloadthen%autoreload 2reloads edited modules before each cell, essential when developing a package alongside a notebook.%matplotlib inlineor%matplotlib widgetpicks the plotting backend.%who,%whoslist variables;%envreads or sets environment variables.%%writefile file.pysaves the cell to a file;%%bashand%%htmlrun or render other languages.%debugopens a debugger at the last exception;%prunprofiles.
Lines starting with `!` run shell commands: !ls data/. The output can be captured into Python: files = !ls *.csv, and Python values interpolate with braces: !echo {name}.
%lsmagic lists all magics and %timeit? shows help. Magics only work in IPython-based kernels, so notebooks full of them do not run as plain .py scripts.
Example
%load_ext autoreload
%autoreload 2
nums = list(range(10_000))
%timeit sum(nums) # e.g. 290 µs ± 50 µs per loop (mean ± std. dev. of 7 runs, ...)
%time total = sum(x * x for x in nums)
print(total) # 333283335000
name = "jupyter"
!echo hello {name} # hello jupyter
files = !ls /
print(type(files).__name__, "usr" in files) # SList True
%env APP_MODE=dev
import os
print(os.environ["APP_MODE"]) # devRemember
%line magics apply to one line;%%cell magics to the whole cell- %timeit/%%time for timing, %run for scripts, %autoreload for live module edits
!cmdruns shell commands;x = !cmdcaptures output,{var}interpolates
Watch out for
- Putting a
%%cell magic anywhere but the first line of the cell, which raises an error.