8

In my settings, I'm using django-environ to set the key:

import environ

env = environ.Env()    
SECRET_KEY = env.read_env('SECRET_KEY')  

At the root of my project, I do have a .env file with the actual secret key:

.env:

SECRET_KEY=qgw6s66n3e$27mmddfua*8yq6n%gz(!mx8e=@zbixk50-h020  

The error will run during collectstatic. Here's the traceback:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 194, in fetch_command
    settings.INSTALLED_APPS
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/sju/Dev/django/blog-api/src/blog/settings/base.py", line 27, in <module>
    SECRET_KEY = env('SECRET_KEY')
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/environ/environ.py", line 130, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/environ/environ.py", line 275, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

What am I missing?

1
  • 1
    Consider deleting your SECRET_KEY in the question
    – harryghgim
    Jan 27, 2021 at 10:02

2 Answers 2

11

I think you are using it wrong. Based on the documentation you first need to load the .env file and then use it.

Try this:

import environ
env = environ.Env()
environ.Env.read_env()
SECRET_KEY = env('SECRET_KEY')

Also make sure the .env file is located in the directory as the settings file. Alternatively, you can pass explicitly pass the location of the file to read_csv().

5
  • actually the docs say that 'SECRET_KEY' needs to be in os.environ. Not sure I understand the reasoning there yet. Dec 30, 2020 at 20:47
  • 1
    well, mystery solved, environ.Env.read() places the variables in os.environ. My bad. Dec 30, 2020 at 21:20
  • 1
    @JamieMarshall really ?. i read in some doc that try keeping secret key in os.environ. so this method is doing it. right
    – legacy
    Jun 23, 2021 at 17:04
  • @legacy I verified this back when I commented. You would have to post a question with your code for us to try and see why it's not working for you Jun 24, 2021 at 19:17
  • 1
    @JamieMarshall yes you are right. sorry for making above comment, simply in that way. the documentation link you mentioned has good stuff and it works.
    – legacy
    Jun 25, 2021 at 1:05
2

Try this:

import os
import environ

BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')

Successful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.