Pycharm error: Improperly configured - django

After having an unexpected shutdown on my DEV machine, when going back to Pycharm project, I noticed the Django view file I was editing (which had 700+ lines) when that happened, it was completely empty. I managed to restore it from a backup; no loss there.
The problem comes up when trying to debug, it returns this error: "ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings." Process finished with exit code 137
The Pycharm settings Django Support (project root, settings & manage script) have the expected values as well.
If I run the project with the ./manage .py runserver command, everything is fine. I can even access the DB with manage.py dbshell. I looked at my settings file and everything seems OK. I also updated from version 3.0.1 to 3.1.1, and no difference.
I'm using Django 1.6.1 and postgresql 9.2.7. What can I do?

For Pycharm, just go to Run -> Edit Configurations, select your project on the right of the window from Debug Configuration, and you will see Environment variables on the right. Make sure you have set DJANGO_SETTINGS_MODULE=mysite.settings, if not just add one, it is as easy as fill a key value pair from the pop up dialog.

I'm using PyCharm Professional and the answers provided here didn't work for me.
I went to Build, Execution, Deployment -> Consule -> Django Console and then added DJANGO_SETTINGS_MODULE=my_app_name.settings to Environment Variables.

Related

Created a brand new Django Project, attempting to run the server generates an Improperly Configured Error

I have been working on a Django Project for a bit, until I took a break. One month later I dug back in to the project and went to run the server. I received an error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I figured I must have tweaked something by accident. So I created a brand new Django Project (using a virtual environment), and just went to test the server. I received the same error. I tried the python manage.py shell solution listed in another answers but to no avail.
If it helps I'm on Linux with Django version 2.1.5 and Python 3.6.
Edit:
If anyone encounters something similar I found using python3 manage.py runserver works in place of using django-admin. Per Greg's answer below, I did begin to receive a new error ModuleNotFoundError: No Module named "mysite" exists. I will continue to search for an answer on that front.
Going off of the comments here.
If "env | grep DJANGO_SETTINGS_MODULE" returns empty, it means you have to set an environment variable stating where your settings.py file is located.
This can be done by doing the following:
export DJANGO_SETTINGS_MODULE=mysite.settings
Be sure to replace "mysite" with the name of your app!

Django behaves differently in PyCharm and terminal

I was getting an error in my Django program, but only when I ran it from PyCharm, and not from terminal. I verified that the following things were the same:
Environment variables in the run configuration and in my virtualenv's activate script
Python executable from virtualenv
Django settings file
Skipping the debugging process, I discovered that when I'd copied my environment variable from my activate script to the Django run configuration in PyCharm, I'd left in single quotes that changed a password, i.e:
.virtualenvs/.../bin/activate:
SOME_PASS='THE_PASSWORD'
PyCharm's run configuration password:
'THE_PASSWORD'
The quotes were ignored by the shell, but taken as part of the password in PyCharm. (So the environment variables were different)
The easy fix is to change the password in PyCharm. Does anyone know how to have PyCharm read from the activate script, and not duplicate this information?

how to set production environment variables in django

I am having some trouble setting environment variables in my production environment, I have some logic in django settings that depends on it. I have tried exportng it through variouse places '/etc/profile, ~.bashrc, etc'
The problem is that those only set them when there is a login (I believe?) But if I navigate a browser to the server I can see that the variable was not set correctly through the debug. I am running django1.7/gunicorn/nginx. I suspected to put it in the gunicorn /etc/init/gunicorn.conf but that did not work.
How can I set this?
You could set them in the WSGI file. – Simeon Visser
Thanks, this solved my problem for my purposes. I don't believe that the wsgi file runs on the dev server so it worked perfectly and only set the variable on the production. Thanks

Pycharm runserver error with custom settings.py files

just started using pycharm on a existing project, and having difficulty using the run/debug configurations in order to run the test server. The issue stems from their being no settings.py module as standard in django projects. Instead there is settings_base.py and then two different settings modules (settings_live.py and settings_test.py) that extend it by using 'from settings_base import *'. This causes manage.py to fail when running runserver because it can't find settings.py.
In the Django Support settings I have set the project root, and set Settings to settings_test.py however this has not helped. In the Django server run configuration I have setup I also have...
DJANGO_SETTINGS_MODULE=settings_test
... in the Environment Variables section. However when I use runserver it still says
Error: Can't find the file 'settings.py' in the directory containing '/home/pete/Projects/the_project/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I tried wrapping the entire settings_test.py module in a try/except to see if it really was an import error, however it did not seem to work. Is there something I am missing?
Maybe you should try adding --settings=settings_test to the "Additional options" in your PyCharm Run configuration and make sure that "Working directory" points to correct path.

Heroku Django DEBUG Setting not applied

I currently have a running production Django application on Heroku. Unfortunately, I haven't been able to turn off the DEBUG setting on Heroku. Turning it off locally works fine, but when pushed to Heroku (after heroku config:set DEBUG=False), it doesn't apply.
The error pages are still the default DEBUG ones instead of the 404, 403, and 500 templates in our template root.
I have also tried using a DJANGO_DEBUG setting in case there were any environment conflicts with DEBUG, and casting the result to a boolean in the settings file. heroku config shows the settings in the environment are correct. This is on Django 1.3, Heroku Cedar.
Any tips or solutions?
Does your django settings.py file even look in the environment?
It does not, by default, care about anything you've set in the environment (via "config:set"). If you're "casting" the environment to a boolean, make sure you're casting it correctly. bool('False') is still True.
It's simplest just to detect if the environment variable exists so you don't have to worry about type casting or specific formats of the configuration.
DEBUG = os.environ.get('DEBUG', False)
To disable debug, remove the variable from the environment instead of trying to type cast... it just seems much more reliable and fool proof. config:unset DEBUG
The problem is that the environment variable is not a boolean, rather a string.
So do place below line in settings.py
DEBUG = (os.environ.get('DEBUG_VALUE') == 'True')