Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between django-environ and python-decouple?

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

like image 214
D_P Avatar asked Nov 26 '25 05:11

D_P


1 Answers

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.

like image 114
AKX Avatar answered Nov 27 '25 21:11

AKX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!