3 minutes read

You already know how to execute your code in PyCharm. Unfortunately, working on your code may get frustrating at some point, and pretty often, you will encounter difficult situations. In this topic, we're going to discuss what if something goes wrong and you get execution errors. PyCharm has a special tool to help you solve these issues – a visual debugger. With it, you can detect the problematic line, preview the variable values at a breakpoint, and breeze through the suspended script.

Breakpoints

One of the most common tools to detect undesirable behavior in code is breakpoints. If you add a breakpoint to a line, this is going to be a line where your code stops executing. Let's consider a typical workflow:

1. Click the gutter at the executable line of code where you want to set the breakpoint.

2. Right-click your Python script in the editor and select Debug <script name>

3. Review the debugging process in the Debug tool window.

As you can see, PyCharm initiates code execution and stops at the breakpoint. You can check the variable and special values. Use the Debug tool window toolbar to proceed with the execution or restart it.

One of the tabs in the tool window is the Debug Console. Like the Python Console, it is interactive, so you can use it to type commands, execute them, and review results:

Python 3.7 introduces the new built-in breakpoint() function, providing a straightforward method to initiate the Python debugger.

Line-by-line execution

If you want to see what your code does line-by-line, there's no need to put a breakpoint on every line, you can step through your code. There is the toolbar with the stepping actions:

  • Step over steps over the current line of code and takes you to the next line even if the highlighted line has method calls in it. The implementation of the methods is skipped, and you move straight to the next line of the caller method;

  • Step into steps into the method to show what happens inside it. Use this option when you are not sure the method is returning the correct result.

  • Step into my code prevents the debugger from stepping into library classes.

  • Step out steps out of the current method and takes you to the caller method.

For more actions available during the debugging process refer to Stepping through the program in the official docs.

Expression evaluation at runtime

While debugging your code, you can evaluate expressions to obtain additional details about the program state or try different scenarios at runtime. Just start typing the variable or method in the dedicated field and specify the expression for this code element:

You can try various expressions including arbitrary expressions. Learn more about available methods in the Evaluating expressions part of the official PyCharm docs.

Read more on this topic in Testing Python Code 101 with PyTest and PyCharm on Hyperskill Blog.

Conclusion

Let's sum up:

  • The visual debugger provides handy methods to discover problematic code in your Python script.

  • To stop execution at a certain line, you can use breakpoints.

  • During the debugging process, you can step through the suspended program executing code line by line.

  • The debugger allows you to evaluate expressions at runtime.

234 learners liked this piece of theory. 14 didn't like it. What about you?
Report a typo