Forums

Error: The SECRET_KEY setting must not be empty

Hi PythonAnywhere,

I can run python3.6 manage.py makemigrations in the bash console without any problems. However, when trying this in my virtual environment I get the following error:

raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

I get this same error when running a scheduled task using this command:

source virtualenvwrapper.sh && workon myevn && python /home/<<dir>>/<<dir>>/scheduledtask.py

I followed the instructions on https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/ and echo $SECRET_KEY gives the right SECRET_KEY value.

Do you have any idea what could go wrong? :)

Thanks in advance!

Regards, Karsten

Are you sure you're using the secret_key environment variable in your settings?

Ha Glenn,

Thank you for your reply! Haha, no, not sure about that! We just started programming in Python/ Django and we have to say that Pythonanywhere works great, so thanks for all the good work! :)

With regard to the secret_key environment variable; do you mean something like the line below in e.g. the wsgi.py file?

os.environ["SECRET_KEY"] = "secret key value"

We did exactly this for the wsgi.py file accessible from the web tab (actually in both wsgi.py files). In addition, we also added these lines of code to wsgi.py:

from dotenv import load_dotenv
project_folder = os.path.expanduser('/home/<<dir>>/<<dir>>')
load_dotenv(os.path.join(project_folder, '.env'))

In the activate and postactivate file we added

export DJANGO_SETTINGS_MODULE="<<account name>>.settings"
export SECRET_KEY="secret key value"
set -a; source /home/<<dir>>/<<dir>>/.env; set +a

We are not quite sure about what changes to make to the settings.py file. Do we have to use something like os.environ.get regarding the secret key value?

Regards, Karsten

This is within django's settings.py file. It needs something like

SECRET_KEY = os.environ["SECRET_KEY"]

in it.

Thanks! We used os.environ.get, so we will try the statement above and let you know!

you could also do

SECRET_KEY = os.environ.get("SECRET_KEY", "some value if your key is not in the environment")

in your settings.py file.

If it turns out there is no secret key variable in your environment, you need to figure out what's wrong with your load_dotenv code etc.

Thank you, looks like it's working now!! However, when doing the python3.6 manage.py makemigrations in the virutal environment (after doing workon myevn) it's giving this (new) error:

SystemCheckError: System check identified some issues:

ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

Any idea how we can fix this one as well? :)

So does your STATICFILES_DIRS in your settings.py contain a STATIC_ROOT?

The value of STATICFILES_DIRS is set like this:

STATICFILES_DIRS=[STATIC_DIR,]

STATIC_DIR contains the following:

STATIC_DIR = os.path.join(BASE_DIR,'static')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT is set to the following value...

STATIC_ROOT = u'/home/ziekenhuisroosters/ziekenhuisroosters/static'

... based on which we conclude that it contains a STATIC_ROOT :) Any idea which value STATICFILES_DIR should be given?

Thanks for all your help!!

STATICFILES_DIRS are the directories that collectstatic will get static files from. STATIC_ROOT is the directory where collectstatic will put static files. It's pretty pointless if you have STATIC_ROOT in the list of STATICFILES_DIRS. See the Django documentation on static files here

Thank you for clarifying!! :)