(this is kind of theoretical, I did not check it if it matches the actions in the IDEs)
Assume we are using a good IDE (integrated development environment) (not python IDLE), like Microsoft Vscode with python package ( there is a link for python package on the start page in the program), or Pycharm community edition (has good autocomplete), but it takes some knowledge – how to configure the python there, need to select the python interpreter when creating the project (search in youtube.com “install python and pycharm”).
So how to debug?
Usually, first I notice the output is not as expected when I test with known arguments and expected outputs.
Usually, I first check the inputs to see if they are as expected: inputted correctly, with expected values, and with the expected type: – not an object, not an array, not a string when numeric is expected, etc.
(mouse over on the parameters. shows them while in debug),
Then I follow once the execution inside the function and check if it is done as expected, While I look and validate inputs and outputs of each expression.
There are two options to run a program, run mode, or debug mode.
Right-clicking on the code and from menu selecting run or debug creates a run configuration. then the code can be executed in debug mode.
Debug mode runs slower and takes more memory because it remembers values in each step, but it is ok.
Debug mode allows to stop the program in the middle of the execution and see values, and execution flow.
To see value while debugging:
I mouseover on variables, it shows them.
Sometimes I can use evaluate an expression to check the combined output for an expression,
Another way to evaluate expression: maybe to run expression inside the console,
or Another way to evaluate expression is right-clicking the code and select “evaluate” from the menu, (sometimes it is in the debug menu of the window).
a “breakpoints” means a place where it will stop: in which file and on which line.
To put “a breakpoint”, a place where to stop, It is possible to click usually left to the code, left to the line number, and put a dot there. (another click removes it. There is a list-of-breakpoints window to remove them all)
Anatomy of a debugger:
In the debugger,
In the toolbox debugger, there is a resume execution button or continue that is like a “play button” a triangle
(and a pause execution button or break button .)
To continue execution after a breakpoint, need to click resume execution or continue button.
(After putting a breakpoint inside the function,) while inspecting a function step by step:
The toolbox debugger has two buttons to go forward one step:
step over – means do a step in current code, do not enter sub-functions like a step to next visible line.
step into – means try to go into functions and function parameters.
step out – means go back and execute up to the previous function.
In the debugger, there are two main windows:
a call stack window, it contains “in-depth” a list of functions, that were before this function. clicking on each function shows function’s local variables. also, this window show, what functions, were called before that function.
a local variables window – mouse over on them shows them, right-click sometimes, there is like an enlarged view option.
There is also a global variables window (which is kind of auxiliary to the local variables.)
There is a tool window “watch” it allows to type there a variable name and an expression and to see it each step inside the correct function.
There is also a console window – a line to type code that runs inside the program at the paused current place. useful for modifications during runtime.
There is a thread selection box for selecting the running thread we want to debug. when a program has multiple threads that run in parallel
each thread has its own call stack (advanced use) .
Debugging using vscode
Nice to know, here is a built-in debugger
Debugging with pycharm