Develop Django Under the Debugger
PyCharm Professional has long had great support for developing Django applications, including a run configuration tailored to the Django server. This winds up being a wonderful workflow, with a tool window showing the server output.
Sometimes, though, you hit a problem and want to debug your code. You stop the server, run it under the debugger, and do your debugging. Which also works really well: PyCharm’s visual debugger is a key selling point and integrates nicely into Django (e.g. template debugging.) And recently, PyCharm’s debugger has undergone dramatic speedups, especially when using Python 3.6. In fact, running under the debugger is approaching the speed of normal run configurations.
But still, it’s irritating to stop you server, restart under the debugger, stop the server, and restart under the regular runner. It messes up your flow, and you’ll be tempted to blasphemy of debugging with
So let’s break with the past with a crazy idea: always run under the debugger when developing Django.
Let’s start with the “normal” path. PyCharm usually makes a Django run configuration automatically, but if not, here’s how to do so: Run -> Edit Configurations -> + -> Django Server:
With this run configuration selected, click the green play button to start the Django server:
When done, click the red button to stop the server:
You can also restart the server, use keystrokes instead of the mouse, let Django restart automatically on code changes, get linked tracebacks to jump directly to errors, etc.
You’re likely familiar with all that. Let’s now do the same thing, but running under the debugger. Click the green button with the bug on it to start that Run/Debug configuration, but running under the debugger instead of the normal runner:
Note: If you get a link saying you can compile the debugger speedups under Cython (on Windows, we ship it by default), click the link, the speedups are worth it.
Let’s compare the restart-on-edit times side-by-side:
As you can see, the debugger’s restart hit isn’t that big, certainly compared to the savings versus other parts of your development workflow (e.g. moving print statements and reload versus moving breakpoints without restarting.)
I can proceed as normal, writing new views and trying them in the browser or the built-in REST client. But when I hit a problem, I don’t reach for print() — no, I set a breakpoint!
In fact, I can set a breakpoint in a template and then poke around at that line of template execution:
Much better than random print statements, which require a restart each time I want to inspect something.
Testing…Under the Debugger
Always develop under the debugger? Sounds weird…the debugger is for debugging. Let’s make it weirder: always do testing under the debugger.
For example, you might run Django tests under PyCharm’s handy Django Test run configuration:
But you can also launch your test runner under the debugger:
Test running under the debugger is an interesting case. Often with testing you are exploring, and sometimes, you are actually intending to produce bugs when writing a test case. Having the debugger close at hand can fit well with TDD:
No free lunch, of course. Even if we have increased performance, dramatically for Python 3.6, there is still a speed hit. Not just on startup, but on execution.
You may run into quirks in obscure usages. For example, when running pytest tests under the debugger, you can’t get to the console (known issue slated to be fixed in 2017.3.) Also, tracebacks might be longer since the code is running under pydevd.
At two recent conferences I mentioned this — do your Django development under the debugger — to people who visited the booth. The reactions were fascinating, as a look of horror turned to confusion then curiosity ending with interest.
For some, it might never fit your brain. For others, it might make all the sense in the world. Want to tell me I’m crazy? It’s possible. Leave me a comment below with any feedback.
Subscribe to Blog updates
Thanks, we've got you!
Five Things To Love About the New UI
Are you using the New UI yet? Not yet? Let me tell you why it's the best thing since sliced bread! Let’s get it enabled and take a look around. The easiest way to do that is from the Settings cog at the top-right of the UI: PyCharm will need to restart, but that’s it; you’re done! In t…
Tutorial: Visual testing with pytest
If you are like many Python developers out there you LOVE writing code! Tests? Not so much. Whether it’s the concept of testing or the interface of testing tools, testing is a chasm not all developers cross. The Visual Testing with pytest tutorial aims to bring Python testing to the masses, tak…
Django Custom Tags in PyCharm Professional 2020.1
PyCharm started its life with Django support and our new release adds some useful improvements for Django coding. Let's take a look at support for custom Django tags in PyCharm Professional 2020.1. But First, One More Thing Maybe it's bragging, but we need to show one existing feature related to thi…
Python 3.8 support in PyCharm
The release of Python 3.8 brought new features to the Python coding realm. The language is evolving according to its community's needs by addressing cases where new syntax or logic become necessary. From new ways of assigning expressions to restriction of usage of function declarations, calls, and v…