Python has a debugger called pdb. Let's find out how can we use it instead of Pycharm.
Calling PDB Stack Trace
There is a PDB class that enables python debugging without additional python packages.
To use it, call stack-trace like that:
import pdb pdb.set_trace()
The line in which you will put
pdb.set_trace() is the one from which pdb will invoke stack-trace.
There is also a way in which you will start function without setting particular line at which stack-trace should start:
import pdb import yourmodulename pdb.run('yourmodulename.yourfunction()')
Using PDB - commands to move in pdb stack-trace
PDB stack trace is interactive - in a similar way as in Pycharm.
You can use commands in pdb to analyze stack-trace.
You can either write one letter of the command or full name of command - but some of the commands needs to be written in a full-length.
The most used commands:
- u(p) - moves current stack-trace frame up
- d(own) - moves current stack-trace frame down
- b(reak) [([filename:]lineno | function) [, condition]]
- without argument lists all set-up breaks.
- first argument sets up breakpoint at specific filename/function and line number.
- second argument gives option to break only on specific python condition
- cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
- without argument clears list of all set-up breaks.
- with first or second argument, gives option to distinct which breakpoints needs to be cleared.
- s(tep) - executes current line
- n(ext) - moves to next line of funtion or returns if not found.
- r(eturn) - continue execution until current function returns.
- q(uit) - quit from debugger.
The full documentation about commands can be found at the pdb documentation page on python docs
import pdb; pdb.set_trace()
- 27.3. pdb â The Python Debugger — Python 3.6.5 documentation
- 26.1. bdb â Debugger framework — Python 2.7.14 documentation
That's it :) Comment, share or don't - up to you.
Any suggestions what I should blog about? Post me a comment in the box below or poke me at Twitter: @anselmos88.
See you in the next episode! Cheers!