Debug Your Python Code with PyCharm

Visual Debugging

Some coders still debug using print statements, because the concept is hard and pdb is intimidating. PyCharm’s python debugging GUI makes it easy to use a debugger by putting a visual face on the process. Getting started is simple and moving on to the major debugging features is easy.

Debug Everywhere

Of course PyCharm can debug code that you’re running on your local computer, whether it’s your system Python, a virtualenv, Anaconda, or a Conda env. In PyCharm Professional Edition you can also debug code you’re running inside a Docker container, within a VM, or on a remote host through SSH.

Debug Inside Templates PRO ONLY

When you’re working with templates, sometimes a bug sneaks into them. These can be very hard to resolve if you can’t see what’s going on inside them. PyCharm’s debugger enables you to put a breakpoint in Django and Jinja2 templates to make these problems easy to fix.

Note: to debug templates, first configure the template language.

JavaScript pro only

Any modern web project involves JavaScript, therefore, any modern Python IDE needs to be able to debug JavaScript as well. PyCharm Professional edition comes with the highly capable JavaScript debugger from WebStorm. Both in-browser JS and NodeJS are supported by the JavaScript debugger.

Debugging During TDD

Test-driven development, or TDD, involves exploration while writing tests. Use the debugger to help explore by setting breakpoints in the context you are investigating.

Debugging During TDD

This investigation can be in your test code or in the code being tested, which is very helpful for Django integration tests (Django support is available only in PyCharm Professional Edition). Use a breakpoint to find out what is coming from a query in a test case.

No Code Modification Necessary

PDB is a great tool, but requires you to modify your code, which can lead to accidentally checking in `pdb.set_trace()` calls into your git repo.

See What Your Code Does

Breakpoints

All debuggers have breakpoints, but only some debuggers have highly versatile breakpoints. Have you ever clicked ‘continue’ many times until you finally get to the loop iteration where your bug occurs? No need for that with PyCharm’s conditional breakpoints.

Sometimes all you want to do is see what a certain variable’s value is throughout code execution. You can configure PyCharm’s breakpoints to not suspend your code, but only log a message for you.

Exceptions can ruin your day, that’s why PyCharm’s debugger is able to break on exceptions, even if you’re not entirely sure where they’re coming from.

To help you stay in control of your debugging experience, PyCharm has an overview window where you can see all of your breakpoints, as well as disable some by checkbox. You can also temporarily mute all of your breakpoints until you need them.

See Variable Values at a Glance

As soon as PyCharm hits a breakpoint, you’ll see all your variable values inline in your code. To make it easy to see what values have changed since the last time you hit the breakpoint, changed values are highlighted.

Watches

Customize your variable view by adding watches. Whether they’re simple or complex, you’ll be able to see exactly what you want to see.

Control Your Code

Visually Step Through Your Code

If you want to know where your code goes, you don’t need to put breakpoints everywhere. You can step through your code, and keep track of exactly what happens.

Run Custom Code

In some cases, the easiest way to reproduce something is to force a variable to a certain value. PyCharm offers both `evaluate expression` to quickly change something, and a console if you’d like more control. The console can even use the ipython shell if it is installed.

Speed

Faster Than PDB

For Python 3.6 debugging, PyCharm’s debugger is the fastest debugger on the market. Even faster than PDB. What this means is that you can simply always run your code under the debugger while developing, and easily add breakpoints when you need them. Just make sure to click ‘install’ when PyCharm asks whether or not to install the Cython speedups.