[Django]-Difference between django-environ and python-decouple?

4👍

django-environ works fine, but you need to load the .env file – just instantiating an Env does not do that:

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

SECRET_KEY = env('SECRET_KEY')
DEBUG = env.bool("DEBUG", False)

In addition, I’ve found it an useful idiom to have “sane defaults” based on the DEBUG value (which must only be true when developing):

DEBUG = env.bool("DEBUG", False)
SECRET_KEY = env('SECRET_KEY', default=('insecure' if DEBUG else Env.NOTSET))

Setting Env.NOTSET as the default will have django-environ complain for unset values.

👤AKX

Leave a comment