Skip to content

Debug in Python3

This document introduces 2 simple methods to debug Python code.

1. pdb

add breakpoint()

# some python code
breakpoint()
# some python code

support option

where
up
down
step
nexe
continue
quit

run with pdb

python3 -m pdb -c continue <program path>

2. cProfile

python3 -m cProfile simul.py
python3 -m cProfile -s tottime simul.py
python3 -m cProfile -o prof.out simul.py
from simul import benchmark
import cProfile

cProfile.run("benchmark()")

pr = cProfile.Profile()
pr.enable()
benchmark()
pr.disable()
pr.print_stats()

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App