Search code examples
djangopycharm

How do I debug/breakpoint my Django application using PyCharm?


I'm trying to work out how to run the debug stuff that PyCharm seems to offer (well, it allows me to set breakpoints, anyway, so I'm assuming there's a nice GUI for it).

I've concluded that I cannot use the Ctrl + Shift + R and then the "runserver" command, and that instead I'd need to set up a "run configuration"? I made a "Django server" one, but I don't know what values to add, etc. When I run it, it tells me that some setting is wrong. I'm pretty sure it isn't, because the standard "runserver" command works fine.

And that's about all I concluded. If there is a nifty tutorial or steps to get it so I can

  1. put in a breakpoint
  2. go to the page that triggers that breakpoint and follow the code's inner working in PyCharm

I'd be thrilled!

Here is the error I got:

Traceback (most recent call last):

File "manage.py", line 11, in import settings

File "C:\development\PycharmProjects\dumpstown\settings.py", line 185, in add_to_builtins('gravatar.templatetags.gravatar')

File "C:\development\python\lib\site-packages\django\template\base.py", line 1017, in add_to_builtins

builtins.append(import_library(module))

File "C:\development\python\lib\site-packages\django\template\base.py", line 963, in import_library

raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))

django.template.base.InvalidTemplateLibrary: ImportError raised loading gravatar.templatetags.gravatar: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Where the application itself, on a "runserver" never has any issues.

As per my answer below, PyCharm is broken for add_to_builtins.


Solution

  • As near as I can tell, the answer is that PyCharm is broken. Which is a pain, but the solution is easy:

    If you dont' want to use the little green button at the top of PyCharm, or use the PyCharm debugging feature? Then don't worry, you don't need to do anything. Continue using Ctrl + Shift + R and runserver (or whatever your shortcut to manage.py is)

    If you do want to use the little green "run" button, or if you want to use PyCharm's debugging kit, then you absolutely cannot use the "add_to_builtins", at least in the settings.py file (I never put it anywhere else myself, PyCharm might require it elsewhere?). add_to_builtins doesn't work in PyCharm. It gets itself caught in a loop of grave consequences when you use the little green button or the debug button. Using Ctrl + Shift + R and runserver doesn't, curiously, have this problem.

    The good news is that "add_to_builtins" isn't a must-have, just a nice-to-have. Just add the "{% load x %}" command to each template where you use x and you will be set.

    Alternatively, save a hundred bucks and use some sort of free Eclipse tool.