djangodjango-settingspython-dotenv

Django os.getenv('SECRET_KEY') throwing "The SECRET_KEY setting must not be empty."


I'm setting up Django using os.getenv to prepare it for deploying using Docker but it seems it is not reading the .env file. Any idea why is not reading it?

Here is the setup:

.env

SECRET_KEY=foo
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1

settings.py abstraction

import os
from pathlib import Path


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG = os.getenv('DEBUG')
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS')

Solution

  • To get environment variables from docker or from the AWS Elastic Beanstalk i use os.environ.get('SECRET_KEY'), this is generally more reliable than os.environ['SECRET_KEY']