You are on page 1of 3

Computer science → Fundamentals → Dev tools → IDE → PyCharm

Debugging Python applications in PyCharm


Theory Practice 25% completed, 0 problems solved

Theory 1 required topic


4 minutes reading
Running Python
applications in PyCharm
Skip this topic Start practicing

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.

§1. 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: Table of contents:

1. Click the gutter at the executable line of code where you want to set the ↑ Debugging Python applications in
breakpoint. PyCharm

§1. Breakpoints

§2. Line-by-line execution

§3. Expression evaluation at runtime

§4. Conclusion

Discussion

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.

Right beside the Debug tool window, there is the Debug Console. Like the Python
Console, it is interactive, so you can use it to type commands, execute them, and
review results:

§2. 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.

§3. 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.

§4. 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.

Report a typo

116 users liked this piece of theory. 5 didn't like it. What about you?

Start practicing Skip this topic

Comments (5) Useful links (0) Show discussion

Tracks About Become beta tester

Pricing Contribute Be the first to see what's new


For organizations Careers

Terms Support Made with by Hyperskill and JetBrains

You might also like