The lineprofiler package profiles decorated Python functions line by line; kernprof is its CLI helper, and setting LINEPROFILE=1 prints a timing summary when the script exits.
Overview
line_profiler measures how much time each source line spends inside functions you mark for profiling. It targets cases where whole-function timing shows a hotspot but you need finer detail inside the function body.
The current workflow imports profile from lineprofiler, decorates selected functions, sets LINEPROFILE=1, and runs your script normally. When execution finishes, stdout shows a results summary, output file locations, and how to inspect details further.
kernprof remains supported as an older entry point, for example kernprof -lv scripttoprofile.py, alongside IPython integration installable via an optional extra. Basic usage, examples, lower-level APIs, and FAQ material are covered in the documentation.
Install from PyPI with pip; current releases require Python 3.10 or newer, with older releases for earlier Python versions. Wheels exist for common platforms, otherwise a C compiler or Cython may be needed when building from source.
Key features
- Per-line timing inside functions
- @profile decorator workflow
- LINEPROFILE environment toggle
- kernprof command-line helper
- Optional IPython extra
- Stdout summary on script exit
- PyPI wheels for common platforms
Use cases
- Finding slow lines inside a hot function
- Profiling Python scripts during development
- Comparing line costs after optimizations
- Using kernprof on standalone scripts
- Interactive profiling with IPython extras