by Ori Roza

How to use PyCharm to debug your Python code

Debugging code in any language might be frustrating, but it is especially so in Python where we cannot recognize a bug immediately.

In addition, Python provides us with the PDB library as a tool for debugging, which can also be difficult to handle.

Luckily, we have the PyCharm IDE. It uses PyDev and gives us a new experience of debugging!

In this article, I will go over of the main and most useful debugging features PyCharm has to offer and teach you how to use them efficiently.

Breakpoints

Breakpoints might be unnecessary when we are facing a bug which occurs in a certain condition.

Also, when we have a lot of them, it’s a mess.

Fortunately, PyCharm gives us the ability to manage breakpoints in an efficient way:

  1. Press Ctrl+Shift+F8 (or Run->View Breakpoints)
  2. All the breakpoints that we set on the project will be listed as shown below (see 1)

3. As we can see, for each breakpoint we can set a condition that will trigger the breakpoint (see 2)

4. Also, we can set a very special condition which controls whether the breakpoint will be triggered when an exception occurs (see 3) in two different states:

a. On termination (after the script ends)

b. On raise (before the script ends)

Attach to local processes

Have you ever wondered to yourself whether it’s possible to debug a remote process?

Yes you can! (and it’s so easy!)

Whether you execute other processes in the background or create them as a part of the flow, PyCharm provides you with a very efficient way to debug remote processes:

  1. As shown below, open Run->Attach to Local Process

2. Now choose the Python process you want to be debugged:

3. Then, the process you chose will be debugged in PyCharm:

Python Interpreter With The Loaded Environment

Making calculations and manipulating the variables of the current debugged code saves time and allows us to make changes on an actual sandbox!

PyCharm provides us a Python interpreter with the loaded environment.

  1. On the console tab, press the marked button:

2. As you can see below, the interpreter recognizes our variables!

Conclusion

PyCharm provides us many great tools, and this debugger is one of them.

Debugging can be hard sometimes, but if you use the right tools, it can be easier and even fun!

I hope this article taught you something new, and I am looking forward to your feedback. Please, do tell — was this useful for you?