Part 1. Debugging Python Code
Preparing an example
Do you remember the quadratic formula from math class? This formula is also known as the A, B, C formula, it’s used for solving a simple quadratic equation: ax2 + bx + c = 0
. As manually solving quadratic formulas gets boring quickly, let’s replace it with a script.
Copy the following code into a file in your project (though it is recommended to type this code manually):
As you see, there is the main
clause here. It means that execution will begin with it, let you enter the desired values of the variables a
, b
and c
, and then enter the method demo
.
Placing breakpoints
To place breakpoints, just click the gutter next to the line you want your application to suspend at:
For more information, refer to Breakpoints.
Starting the debugger session
OK now, as we've added breakpoints, everything is ready for debugging.
PyCharm allows starting the debugger session in several ways. Let's choose one: click in the gutter, and then select the command in the popup menu that opens:
The debugger starts, shows the Console tab of the Debug tool window, and lets you enter the desired values:
The debugger suspends the program at the first breakpoint. It means that the line with the breakpoint is not yet executed. The line becomes blue:
By the way, you can enter Python commands in the Debug Console when the program is suspended:
On the stepping toolbar of the Debugger tab, click the (Resume Program) button to move to the next breakpoint.
Inline debugging
In the editor, you can see text in italics next to the lines of code:
What does it mean?
This is the result of so-called inline debugging. In this case, it shows the address of the Solver
object and the values of the variables a
, b
and c
you've entered.
Inline debugging can be turned off.
Note that you can do it in course of the debugger session!
Let's step!
So, you've clicked the button , and now see that the blue marker moves to the next breakpoint.
If you use the stepping toolbar buttons, you'll move to the next line. For example, click the (Step Over) button. Since the inline debugging is enabled, the values of the variables show in italics in the editor.
Now set another breakpoint at line 24 (b = int(input("b: "))
) and click again. The new cycle starts. Provide the value for a
in the Debug Console. The debugger will stop at the breakpoint.
While the (Step Over) button executes the program until the next line in the current method, the (Step Into) button lets you see how the current line is executed.
Click the (Step Into) button, and you will see that after the line a = int(input("a: "))
the debugger goes into the file parse.py:
If you want to concentrate on your own code, use the (Step Into My Code) button — thus you'll avoid stepping into library classes.
Watching
PyCharm allows you to watch a variable. Switch to the Threads & Variables tab, and type the name of the variable you want to watch. Note that code completion is available:
At first, you see an error—it means that the variable is not yet defined:
However, when the program execution continues to the scope that defines the variable, the watch gets the following view:
You can use the same field to evaluate expressions.
For more information, refer to the section Evaluate expressions.
Changing the format of decimal variables
In PyCharm debugger, you can preview int
variables in the hexadecimal or binary format. This might be particularly helpful when you debug network scripts that include binary protocols.
To change the display format, select one or several int
variables in the Variables list, right-click, and select from the context menu.
The format of the variables changes in both the list of the variables and in the editor.
Summary
This brief tutorial is over - congrats! Let's repeat what you've learnt from it:
You've refreshed your knowledge of the breakpoints and learnt how to place them.
You've learnt how to begin the debugger session, and how to show the Python prompt in the debugger console.
You've refreshed your knowledge about the inline debugging.
You've tried hands on stepping, watches and evaluating expressions.
The next step is intended for the Professional edition users: Debugging Django Templates.