'Django can't find me .env variables? decouple.UndefinedValueError: ENVIORNMENT not found. Declare it as envvar or define a default value

I am very new to Django and trying to configure python-decouple to use .env variables. I am getting DB_PASSWORD not found. Declare it as envvar or define a default value. when trying to run the server. The .env file is located in the root directory. Here is my code:

raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: ENVIORNMENT not found. Declare it as envvar or define a default value.

settings.py

from decouple import config
from secrets_manager import get_secret

environment = config("ENVIRONMENT")
SECRET_KEY = get_secret(environment).get("SECRET_KEY")
DEBUG = config("DEBUG", default=False, cast=bool)

DATABASES = {
    "default": {
        "ENGINE": config("DB_ENGINE"),
        "NAME": config("DATABASE_NAME"),
        "USER": config("DB_USER"),
        "PASSWORD": config("DB_PASSWORD"),
        "HOST": config("DB_HOST"),
        "PORT": config("DB_PORT", cast=int),
        "OPTIONS": {
            "init_command": "SET foreign_key_checks = 0;",
        },
    }
}

.env

SECRET_KEY='nvyj6_-&m@lg87$%l3@@#+r046ioem^18+ql*)t)'
DEBUG=True
DB_ENGINE=django.db.backends.mysql
DATABASE_NAME=blogs
DB_USER=root
DB_PASSWORD=
DB_HOST=127.0.0.1
DB_PORT=3306

Django==3.1.4 python-decouple==3.3



Solution 1:[1]

You are using config('ENVIRONMENT') in your code, without a corresponding ENVIRONMENT setting in your .env file. Make sure:

  • your .env file is at the root directory of your project.
  • you spell ENVIRONMENT consistently in your .env file and in your settings.py file wherever you use it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1