Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i debug/breakpoint my django app using pycharm?

Tags:

django

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 "runserver" command, and that instead i'd need to setup a "run configuration"? I made a "django server" one, but i don't know what values to put 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 break point
  2. go to the page that triggers that breakpoint and follow the code's inner working in pycharm

i'd be thrilled!

cheers!

UPDATE: in case you're wondering, 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.

UPDATE: as per my answer below, pycharm is broken for add_to_builtins.

like image 381
bharal Avatar asked Jun 28 '12 23:06

bharal


1 Answers

  1. Setup your virtual environment

    1. Create or open your project in PyCharm
    2. Go to File - Settings in the menu (or just click on the settings icon)
    3. Go to Python Interpreter
    4. Click on Add in the top
    5. Go to the bin folder where you created the virtual environment and select python
  2. Set a breakpoint

    1. Next to the line of code you want to set the breakpoint. To the left there is usually a grey line. Usually next to the line numbers. Just click there and a big red dot will appear. Looks like this
  3. Hit the Run in debug mode button.

    1. Next to the Green play button there is a button with a bug on it. Use that to launch the runserver in debug mode. Now when you use your web application and the code hits the breakpoint it will stop there and you will be able to step in and over or resume in pycharm.

      • If you still are not able to debug you might need to edit the pycharm project config. You can do that like this.

You can look at http://garmoncheg.blogspot.it/2012/01/establishing-dev-environment-with.html too

like image 172
darren Avatar answered Oct 02 '22 08:10

darren