Reputation: 16154
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
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.
Upvotes: 54
Views: 53274
Reputation: 6416
It depends on how you want to run your code for debugging.
You might not have a frontend or views. You just want to directly trigger the functions you want to test.
In this case, you want to start the debugger by running python manage.py shell
. Then, you will be able to run commands directly against your code and trigger any breakpoints. A simple example:
from structure.models import Clients
Clients.objects.create()
You have a frontend or will be calling your views through an API tester (Postman, etc.) to trigger the functionality you want to test.
Then you want to start the debugger by running python manage.py runserver
. Now, as long as the right requests are getting sent, any breakpoints you have set will get triggered.
Upvotes: 0
Reputation: 685
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, i.e., 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
Click on Edit configurations. You will get a screen like this one:
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 encounters 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 newly configured run button.
Upvotes: 50
Reputation: 16154
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.
Upvotes: 5
Reputation: 19399
Set up your virtual environment
Create or open your project in PyCharm
Go to File → Settings in the menu (or just click on the settings icon)
Go to Python Interpreter
Click on Add in the top
Go to the bin folder where you created the virtual environment and select python
Set a breakpoint
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. It looks like this
Hit the Run in debug mode button.
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.
You can look at http://garmoncheg.blogspot.it/2012/01/establishing-dev-environment-with.html too
Upvotes: 38
Reputation: 1986
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
Upvotes: 3