Just a convenience question. I\'ve been a bit spoiled with debuggers in IDEs like Visual Studio and XCode. I find it a bit clumsy to have to type import pdb; pdb.set_t
This is how you would use pdb in the command line without implementing anything in your source code (the documentation and other online resources don't do a good job explaining this to a programmer who has only used visual debuggers in the past):
Start pdb by typing the following in a shell prompt:
python -m pdb 'python_script'
This command initializes pdb and the pdb debugger will break at the first line of your python_script and wait for an input from you:
(Pdb)
This is the interface for communicating with the debugger. Now, you can specify your commands here. Opposed to using buttons or keyboard shortcuts in visual debuggers, here you will use commands to derive the same results.
You can go to the next line in your code by command "n" (next):
(Pdb) n
Performing a next would display the line number, and the specific code in the source:
> python_script(line number)method name
-> current line in the source code
You can set a breakpoint by specifying a line number in your source code.
(Pdb) b 50
Here, the debugger is set to break at line 50. If there aren't any other breakpoints, the breakpoint at line 50 will be the first and it could be referenced by the breakpoint id which is 1 in this case. If you add more break points they will get identifiers sequentially (i.e., 2, 3 etc.)
Once a breakpoint is set, you would continue executing your program until pdb gets to the breakpoint as follows:
(Pdb) c
Once you get to a breakpoint you could go to the next line, with the n command as described before. If you want to examine the values of variables, you would execute the parameter command as follows:
(Pdb) p variable_name
If you no longer need a breakpoint, you can clear it by passing in the id of the breakpoint with the clear command:
(Pdb) clear 1
Finally, when you are done with the debugger you can exit the execution as you would exit the python command line interpreter.
(Pdb) exit()
I hope this will help anybody get started with pdb. Here is a list of commands you can use with the debugger: pdb so question and answers
You could use Vim with the Python-Mode plugin or Emacs with the Elpy plugin.
These plugins give you breakpoints with easy keystrokes (\ b
in Vim and C-c C-u b
in Emacs) plus many other features from heavy-weight IDEs (code folding, refactoring, linting, etc.) - all within a light-weight terminal-based text editor.
You can use:
All of the above support python debugging from inside an IDE.
You could use an IDE which supports python debugging, or you can check out the excellent Winpdb tool. Which works on any platform and provides graphical debugging facilities to your python script.
http://winpdb.org/
You can use:
from pdb import set_trace as bp
code
code
bp()
code
code
Python 3.7
has a new builtin way of setting breakpoints. Calling
breakpoint()
More here https://stackoverflow.com/a/53263117/6488361