To profile Python code line by line. We can use line_profiler1.



Just use pip:

pip install line_profiler


It is easier to use conda since it does not require building the package:

# if you use pip on Windows, you must have Visual Studio to build the package.
conda install line_profiler

How to profile?

Annotate the function we want to profile with @profile annotation. On the command line, run:

kernprof -v -l

The command line options to can be followed after it. Just like you call python


  1. This is a fork of original line_profiler repo. The original repo is not maintained. ↩︎