aisflat439 aisflat439 - 1 year ago 143
Python Question

SECRET_KEY errors with enviroment variables

I am working through the TaskBuster Django tutorial whose goal is to help in the process of setting up a good development setup for a project.

When I run the command

In either my "Dev" environment or my "Test" environment I get the same output so I believe that my $ENVIROMENTS/bin/postactivate and predeativate variables are set up correctly.

my folder contains

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See

# Get SECRET_KEY from the virtual environment
from django.core.exceptions import ImproperlyConfigured

def get_env_variable(var_name):
return os.environ[var_name]
except KeyError:
error_msg = "Set the %s environment variable" % var_name
raise ImproperlyConfigured(error_msg)

SECRET_KEY = get_env_variable('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!


# Application definition



ROOT_URLCONF = 'bapsite.urls'

'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'context_processors': [

WSGI_APPLICATION = 'bapsite.wsgi.application'

# Database

'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

# Internationalization



USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'

os.path.join(BASE_DIR, "static"),

However, when I try to run the tests I get this output.

Traceback (most recent call last):
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/", line 28, in get_env_variable
return os.environ[var_name]
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/", line 725, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 10, in <module>
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/", line 338, in execute_from_command_line
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/", line 330, in execute
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/commands/", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/", line 378, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/", line 351, in create_parser
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/core/management/commands/", line 52, in add_arguments
test_runner_class = get_runner(settings, self.test_runner)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/test/", line 144, in get_runner
test_runner_class = settings.TEST_RUNNER
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/", line 48, in __getattr__
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/site-packages/django/conf/", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/devin/.virtualenvs/bapsite_test/lib/python3.5/importlib/", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/", line 2, in <module>
from .base import *
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/", line 33, in <module>
SECRET_KEY = get_env_variable('SECRET_KEY')
File "/home/devin/DjangoProjects/bap_project/bapsite/settings/", line 31, in get_env_variable
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

This leads me to believe that there is some issue with the secret key but I"m unclear how to troubleshoot from here.

is the return os.environ[var_name] returning the wrong value? How do I see what that is returning so I can point it to what I'd prefer it to return? Am I on the correct path to figuring this out?

Here is the testing file I am using currently

from selenium import webdriver
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.testing import LiveServerTestCase

class HomeNewVisitorTest(LiveServerTestCase):

def setUp(self):
self.browser = webdriver.Firefox()

def tearDown(self):

def get_full_url(self, namespace):
return self.live_server_url + reverse(namespace)

def test_home_title(self):
self.assertIn("TaskBuster", self.browser.title)

def test_h1_css(self):
h1 = self.browser.find_element_by_tag_name("h1")
"rgba(200, 50, 255, 1)")

Answer Source

You don't have an environment variable named SECRET_KEY. You need to set it before you run your program.

On Unix

export SECRET_KEY="password"

On Windows

set SECRET_KEY="password"

It's worth noting that the variable will disappear when you close the terminal. There are ways around that if you'd like to look for them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download