Home » Django » Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty

Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am trying to set up multiple setting files (development, production, ..) that include some base settings. Cannot succeed though. When I try to run ./manage.py runserver I am getting the following error:

(cb)[email protected] /srv/www/cb $ ./manage.py runserver
ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Here is my settings module:

(cb)[email protected] /srv/www/cb/cb/settings $ ll
total 24
-rw-rw-r--. 1 clime clime 8230 Oct  2 02:56 base.py
-rw-rw-r--. 1 clime clime  489 Oct  2 03:09 development.py
-rw-rw-r--. 1 clime clime   24 Oct  2 02:34 __init__.py
-rw-rw-r--. 1 clime clime  471 Oct  2 02:51 production.py

Base settings (contain SECRET_KEY):

One of the setting files:

Code in manage.py:

If I add from base import * into /srv/www/cb/cb/settings/__init__.py (which is otherwise empty), it magically starts to work but I don’t understand why. Anyone could explain to me what’s going on here? It must be some python module magic.

EDIT: Everything also starts to work if I remove this line from base.py

If I remove this line from web.templatetags.cb_tags, it also starts to work:

I guess it is because, in the end, it leads to

So it creates some weird circular stuff and game over.

Answers:

I had the same error and it turned out to be a circular dependency between something loaded by the settings and the settings module itself. In my case it was a middleware class which was named in the settings which itself tried to load the settings.

Questions:
Answers:

I ran into the same problem after restructuring the settings as per the instructions from Daniel Greenfield’s book Two scoops of Django.

I resolved the issue by setting

in manage.py and wsgi.py.

Questions:
Answers:

I had the same error with python manage.py runserver.

For me, it turned out that it was because of a stale compiled binary (.pyc) file. After deleting all such files in my project, server started running again. 🙂

So if you get this error, out of nowhere, i.e without making any change seemingly-related to django-settings, this could be a good first measure.

Questions:
Answers:

Remove .pyc files

Ubuntu terminal command for deleting .pyc :
find . -name "*.pyc" -exec rm -rf {} \;

I have got same error when I did python manage.py runserver. It was because .pyc file. I deleted .pyc file from project directory then it was working.

Questions:
Answers:

It starts working because on the base.py you have all information needed in a basic settings file. You need the line:

So it works and when you do from base import *, it imports SECRET_KEY into your development.py.

You should always import basic settings before doing any custom settings.


EDIT:
Also, when django imports development from your package, it initializes all variables inside base since you defined from base import * inside __init__.py

Questions:
Answers:

I solved this problem occurring on OS X with Django both 1.5 and 1.6 by deactivating all active sessions to virtualenv and starting it again.

Questions:
Answers:

I think that it is the Environment error,you should try setting : DJANGO_SETTINGS_MODULE='correctly_settings'

Questions:
Answers:

I hadn’t specified the settings file:

Questions:
Answers:

I just wanted to add that I got this error when my database name was spelled wrong in my settings.py file so the DB couldn’t be created.

Questions:
Answers:

I solved this problem on 1.8.4 by fixing the TEMPLATES settings which had a typo (removing TEMPLATES[‘debug’] solved it)

Go over the settings that you have changed recently, make sure all keys are by-the-book.

Questions:
Answers:

I solved this problem by removing the spaces around equal signs (=) in my .env file.

Questions:
Answers:

To throw another potential solution into the mix, I had a settings folder as well as a settings.py in my project dir. (I was switching back from environment-based settings files to one file. I have since reconsidered.)

Python was getting confused about whether I wanted to import project/settings.py or project/settings/__init__.py. I removed the settings dir and everything now works fine.