3

Debugging Django with PyCharm.

When I run anything in the debugger, it runs slow.

Django start up time is pretty long.

Don't get me wrong - I love PyCharm, as it has all the bells and whistles needed for a comfortable debugging session... and Python is still way easier and probably faster to debug, than other languages (like C). But even after I tuned my PostgreSQL database for the testing (Optimise PostgreSQL for fast testing), even if I have SSD drive and i7 quad-core CPU, even if I specifically told my antivirus software NOT to touch anything in C:\Python27 directory and my project dir, it is still very slow.

Any ideas, how can I speed up debugging?

I would love to see improvements mainly in process start-up time, because my most often use-case is when I debug a single unit test.

Community
  • 1
  • 1
dotz
  • 884
  • 1
  • 8
  • 17
  • Unfortunately running Python under a debugger is always going to be pretty slow. Having 4 cores doesn't help in the usual case with a single-threaded process. Python is not known for extreme speed to begin with, and when you run under a debugger it uses sys.settrace which calls a (Python, not C) tracing function for every statement executed. – Preston Landers Oct 17 '14 at 13:31
  • Hi Preston, thank you for your comment. Yes, I suppose there's a lot of tracing / data collecting going on under the hood and I don't ignore that fact; the deal is I want it to be faster. Basing on your comment, is your suggestion "get as fast single core as you can get" - did I understand you right? – dotz Oct 17 '14 at 13:33
  • Yes, you understood correctly. I don't know of anything else that would have much of an impact. However a slightly faster CPU, say 3.4 Ghz instead of 3.2, probably isn't going to make a noticeable difference. You said you are debugging a single unit test, but if that unit test has significant dependencies such as Django environment, that may be a lot of work just to get to the point of running one test. When you run a lot of tests in one session, you amortize the startup cost. – Preston Landers Oct 17 '14 at 13:36

2 Answers2

3

Run python normally but use pdb on your code. Something like this:

... code before ...
import pdb; pdb.set_trace()
... code after ...

It will stop the code on that point. You will need to press c (continue), q (quit) or n (next) in order to keep going. You can test expressions and check where are you by pressing l.

The code will go probably faster, but debugging can be more painful.

1

I had the same problem a while ago until I figured out, Django and PyCharm allow you to specify to run single tests and not the complete test suite everytime I press the debug-button.

In order to do this, simply edit your Debug configuration in PyCharm. Change your target to point a module, a class or even a method somewhere deep down in your test files.

In order to do this, ensure your directories are modules (e.g. a directory which has a __init__.py file in it). You're now able to point specific targets in the following format:

django_app.tests_module.test_case.test_method

It is clear that the final target "path" depends on your project's organzation.

Don't forget to change the target back once you're done implementing in order to run all the tests before pushing your code ;)

maennel
  • 1,917
  • 1
  • 12
  • 8
  • Well, I remember writing some Python helper application AND some LISP procedures in Emacs just to achieve the same thing I can do by a right-click (or Ctrl+F5, until they broke it). This is one of the reasons I love PyCharm so much. Unfortunately, the start-up time of a single test (run Django, create PgSQL database) seems barely acceptable to me, that's why I asked this question. So, thank you for your comment, but I'm already both aware and using this method. – dotz Mar 07 '15 at 13:15