Difference between django-environ and python-decouple?

3.2k Views Asked by At

Here I have used django-environ to set the environment variable but it is giving me SECRET_KEY error.How to properly configure the environmental variable?

I also used the python-decouple for this instead of django-environ which works fine but didn't work with django-environ.

What is the difference between django-environ and python-decouple which will be the best for this ?

settings

import environ
env = environ.Env()

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

.env file

DEBUG = True
SECRET_KEY = #qoh86ptbe51lg0o#!v1#h(t+g&!4_v7f!ovsl^58bo)g4hqkq #this is the django gives 

Got this exception while using django-environ

django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

1

There are 1 best solutions below

3
AKX On BEST ANSWER

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.