Difference between revisions of "Python pdb"
From John Freier
(One intermediate revision by the same user not shown) | |||
Line 37: | Line 37: | ||
!<expr> execute the expression <expr> NOTE: this acts just like a python interpreter | !<expr> execute the expression <expr> NOTE: this acts just like a python interpreter | ||
q(uit) exit the debugger | q(uit) exit the debugger | ||
+ | |||
+ | Example Command | ||
+ | poetry run pytest -o log_cli=true --log-cli-level=INFO -v tests/functions/test_function.py::TestOne::test_one --pdb --no-cov | ||
+ | |||
+ | Issues | ||
+ | If pdb is breaking in the wrong location try adding the following flag. | ||
+ | --no-cov |
Latest revision as of 14:20, 4 December 2024
To debug python applications use the library pdb.
Add the following to the top of your file.
import pdb
To add a breakpoint in the python use
pdb.set_trace() or breakpoint() # 3.7 for higher
And then just add the following to your command.
--pdb
Example
pytest --pdb test/test.py
Once it starts up you will be brought to a promote. From the there you can use any of the following.
Navigating Code (within the Pdb interpreter)
l(ist) list 11 lines surrounding the current line l . list the current location w(here) display the file and line number of the current line n(ext) execute the current line s(tep) step into functions called at the current line r(eturn) execute until the current function’s return is encountered
Controlling Execution
b[#] create a breakpoint at line [#] b list breakpoints and their indices c(ontinue) execute until a breakpoint is encountered clear[#] clear breakpoint of index [#]
Changing Variables / Interacting with Code
p<name> print value of the variable <name> !<expr> execute the expression <expr> NOTE: this acts just like a python interpreter q(uit) exit the debugger
Example Command
poetry run pytest -o log_cli=true --log-cli-level=INFO -v tests/functions/test_function.py::TestOne::test_one --pdb --no-cov
Issues If pdb is breaking in the wrong location try adding the following flag.
--no-cov