Search⌘ K
AI Features

Stepping Through the Code

Explore how to use Python’s pdb debugger to step through code line by line, inspect arguments, and jump to lines for efficient debugging. Learn commands like step, next, args, and jump to control program flow and troubleshoot your Python scripts.

We'll cover the following...

If you want to step through your code one line at a time, then you can use the step (or simply “s”) command. Here’s a session for your viewing pleasure:

Javascript (babel-node)
C:\Users\mike>cd c:\py101
c:\py101>python -m pdb debug_test.py
> c:\py101\debug_test.py(4)<module>()
-> def doubler(a):
(Pdb) step
> c:\py101\debug_test.py(11)<module>()
-> def main():
(Pdb) s
> c:\py101\debug_test.py(16)<module>()
-> if __name__ == "__main__":
(Pdb) s
> c:\py101\debug_test.py(17)<module>()
-> main()
(Pdb) s
--Call--
> c:\py101\debug_test.py(11)main()
-> def main():
(Pdb) next
> c:\py101\debug_test.py(13)main()
-> for i in range(1,10):
(Pdb) s
> c:\py101\debug_test.py(14)main()
-> doubler(i)
(Pdb)

Here we start up the debugger and tell it to step into the code. It starts at the top and ...