We want to debug our Python source code like professionals.
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
## Simple debugging in cell mode
If we use cells ( # %% ) (i.e. Python Interactive window ) then we can use the Variable button in the interactive Python window to open a variable viewer.
![image1](2022-04-01_18-40_0.png)
Also you can interact with the Python kernel directly via the command input line (the lower thing in the interactive window). There you can enter a Python command and press Shift + Enter to execute it.
|![image6](2022-04-01_21-30.png)| Continue -- Debugger continues to run the code until the next breakpoint is reached or until the program ends. If end of the program is reached, the debugger closes. |
|![image7](2022-04-01_21-30_2.png)| Step Over|
|![image8](2022-04-01_21-30_3.png)| Step Into|
|![image9](2022-04-01_21-31.png)| Step Out|
|![image10](2022-04-01_21-31_1.png)| Restart -- Starts the debugging process from the beginning. |
|![image11](2022-04-01_21-31_2.png)| Stop -- Stops the debugging and the debugger closes.|
* ["If the current line contains a function call, Step Over runs the code and then suspends execution at the first line of code after the called function returns."](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022#BKMK_Step_over_Step_out)
* ["The debugger steps through code statements, not physical lines."](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022#BKMK_Step_into__over__or_out_of_the_code)
* ["But when you step into this line, the debugger treats the condition as one step and the consequence as another."](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022#BKMK_Step_into__over__or_out_of_the_code)
* ["On a nested function call, Step Into steps into the most deeply nested function. For example, if you use Step Into on a call like Func1(Func2()), the debugger steps into the function Func2."](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022#BKMK_Step_into__over__or_out_of_the_code)
* ["Step Out continues running code and suspends execution when the current function returns. The debugger skips through the current function."](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022#BKMK_Step_over_Step_out)
**No breakpoint => No debugging.** You need to set breakpoints. Thus the debugger know where you want it to stop and allow you to investigate the state of your program.
You can create breakpoints by making these red dot in your source code. You just click left beside the line number (with the left mouse button) and get a new breakpoint.
In the breakpoint window, you see all your breakpoints. The X Symbol removes the breakpoint. Removing a blue checkmark disables the corresponding breakpoint.
We can use the context menu (right mouse click) to add "Add Conditional Breakpoint...". Expression allow to define a condition that needs to be true to activate the breakpoint.
Hit Count (defined via the context menu) allows to set an integer value for how often a position in a source code needs to be "hit" (i.e. run) until a breakpoint is activated.
Log Message allows you to add the generation debug messages (messages in the debug console). If this "breakpoint" is passed by the debugger then a debug message is generated. However this is not a breakpoint that stops running the program like a normal breakpoint would. The message is defined akin to ["Formatted String Literals"](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings) (also called f-string).
You can break on throwing exceptions even if we catch them. However, we need another breakpoint (disabled or enabled) otherwise we cannot enable the Raise Exception breakpoints.
When the debugger waits for the next command from us, we can use the **Debug Console** to interact with the program. For example: We can print the value of variables or replace them with new values. If the console is not open already then you find it in the menu under View -> Debug Console.
* [Navigate through code by using the Visual Studio debugger](https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2022)
* [Debugging in VS Code](https://code.visualstudio.com/docs/editor/debugging)
* [Python debugging in VS Code](https://code.visualstudio.com/docs/python/debugging)