Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This project was working fine until I used environ to make SECRET_KEY and DEBUG as environment variable using environ. After I am getting this error:-

The output is:

(env) E:\ecommercedj>python manage.py runserver
Traceback (most recent call last):
  File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "c:\users\matruchhaya\appdata\local\programs\python\python38-32\lib\os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "E:\ecommercedj\env\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "E:\ecommercedj\env\lib\site-packages\django\conf\__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "c:\users\matruchhaya\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\ecommercedj\ecom\settings.py", line 11, in <module>
    SECRET_KEY = env('SECRET_KEY')
  File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 123, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "E:\ecommercedj\env\lib\site-packages\environ\environ.py", line 277, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

(env) E:\ecommercedj>

settings.py

import os
import environ

env = environ.Env()

# read th .env file
environ.Env.read_env()

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = env('SECRET_KEY')

DEBUG = env('DEBUG')

ALLOWED_HOSTS = ['*']
.....

.env file

SECRET_KEY value is unquoted

SECRET_KEY = my_secret_key_value
DEBUG = True

The project was running fine before adding environ. Am I getting the error because of eviron? How should I remove this error?

like image 544
Aman Sharma Avatar asked Jul 28 '20 15:07

Aman Sharma


3 Answers

Solved it!!

In the .env file remove the spaces between assignment operator and var, and between value and assignment operator. Like:

SECRET_KEY=my_secret_key_value
DEBUG=True
like image 82
Aman Sharma Avatar answered Oct 14 '22 05:10

Aman Sharma


The .env file should be in the same directory as settings.py

like image 13
nxoo Avatar answered Oct 14 '22 05:10

nxoo


  1. First Create .env file in the same directory as the settings

  2. Second, remove any space between variable and key-value (i.e KEY=my_key)

    import environ
    
    env = environ.Env()
    environ.Env.read_env(os.path.join(BASE_DIR, 'somepath/.env')) #<-- where ever your .env lies inside project directory
    SECRET_KEY = os.environ.get('somekeyInsidenvFile',env('somekeyInsidenvFile')) #
    
like image 3
Atif Shafi Avatar answered Oct 14 '22 06:10

Atif Shafi