You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import environ
env = environ.Env()
environ.Env.read_env()
SECRET_KEY = env('SECRET_KEY')
Running this code will generate the following error
django.core.exceptions.ImproperlyConfigured: Set the q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t) environment variable
Even if I surround the secret key value with single or double quotes the error will still occur.
This error does not occur if I use a different python environment file package such as python-dotenv to read the same .env file.
The django-environ package seems to think the SECRET_KEY value is an environment variable itself.
I know this is a very niche situation as this secret key that I generated just happened to have a leading '$' character but it may catch people out if they're generating their secret key with a script. The error occurs for any secret key value that starts with a '$'.
Full stack trace:
Traceback (most recent call last):
File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 273, in get_value
value = self.ENVIRON[var]
File "/opt/anaconda3/envs/django_py/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/danieloram/Desktop/test_env/environ_secret_key.py", line 10, in <module>
SECRET_KEY = env('SECRET_KEY')
File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 123, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 284, in get_value
value = self.get_value(value, cast=cast, default=default)
File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 277, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t) environment variable
django version: 3.0.3
django-environ version: 0.4.5
The text was updated successfully, but these errors were encountered:
@sergeyklay is there a way to not to escape backslashes when reading environment variables? I'm able to successfully load a private key with python-dotenv but am having issues when loading with django-environ
@zacwellmer Current issues is duplicate for #60 and is related to using the dollar sign at the beginning of the value. Could you please open a new issue with code to reproduce?
I've come across this error which only seems to occur when using the django-environ package to read a SECRET_KEY from a .env file.
My .env file (with a generated secret key using the get_random_secret_key module from django)
SECRET_KEY=$q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t)
My settings.py file
Running this code will generate the following error
django.core.exceptions.ImproperlyConfigured: Set the q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t) environment variable
Even if I surround the secret key value with single or double quotes the error will still occur.
This error does not occur if I use a different python environment file package such as python-dotenv to read the same .env file.
The django-environ package seems to think the SECRET_KEY value is an environment variable itself.
I know this is a very niche situation as this secret key that I generated just happened to have a leading '$' character but it may catch people out if they're generating their secret key with a script. The error occurs for any secret key value that starts with a '$'.
Full stack trace:
django version: 3.0.3
django-environ version: 0.4.5
The text was updated successfully, but these errors were encountered: