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

42,532

Solution 1

So i gave all the answers here a +1 for trying - but they're not the issue. 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.

Solution 2

The problem has little to do with DJANGO_SETTINGS_MODULE. Pycharm sets this when creating the project. Instead Go to the little green "Play button" for running scripts. You want to configure it to run manage.py, ie you are configuring the play button to run the command python manage.py runserver. Breakdown:

python------> run with python Intepreter

manage.py--------> run this script

runserver---------> arguments

So go to the drop down to the left of the green play button

green run button ,

click on edit configurations. You will get a screen like this one

this.

Fill it out as shown locating your manage.py source script. Make sure you include runserver on the arguments box. Now you can click the green debug button and you program will stop at the first breakpoint it ecounters allowing you to descend upon it in full debug mode watch variables and all. Now you can manually type python manage.py runserver on the terminal or use your newley configured run button.

Solution 3

  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

Solution 4

The problem is that the DJANGO_SETTINGS_MODULE variable which should point to your project's settings file, wasn't set anywhere, since the variable is reported to be undefined.

The solution and an explanation you can refer to can be found in a previous post

Share:
42,532

Related videos on Youtube

bharal
Author by

bharal

a bharal is a wild himilayan mountain goat! bleat! developer specialising in Java work at IBs

Updated on July 09, 2022

Comments

  • bharal
    bharal almost 2 years

    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.

  • seekme_94
    seekme_94 over 4 years
    Thanks a bunch buddy.
  • Tungata
    Tungata over 4 years
    Glad the answer helped you @seekme_94
  • Raymond
    Raymond over 4 years
    Thanks, but for me I need to add the additional environment variable DJANGO_SETTINGS_MODULE =<mysite>.settings, however it's still not hang and popup the debug window because it needs a restart of Pycharm!
  • Tungata
    Tungata over 4 years
    @Raymond the command is python manage.py -- settings <mysite>.settings runserver. So on the screen shot above at Environment Variables where currently there is PYTHONUNBUFFERED=1 Add DJANGO_SETTINGS_MODULE=<mysite>.settings such that when you click run or debug your project will be given the settings you want. At the End it must appear asPYTHONUNBUFFERED=1;DJANGO_SETTINGS_MODULE=<mysite>.setting‌​s