1

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

humble_barnacle
  • 460
  • 1
  • 4
  • 19
Spiral
  • 917
  • 1
  • 9
  • 15

1 Answers1

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.
Jeff Booth
  • 461
  • 2
  • 5
  • Thanks, @Jeff for replying to me. I updated my question and updated the settings.py code. – Spiral Feb 01 '22 at 07:47
  • It looks like you're missing an ENVIRONMENT setting in your .env file. I also don't know what `secrets_manager` is. Is it a common package? Where can I find it? – Jeff Booth Feb 01 '22 at 08:05
  • Please can you explain how can add this missed ENVIRONMENT setting in my .env file. – Spiral Feb 01 '22 at 08:30
  • You'll need to explain what the `secrets_manager` is first. The `ENVIRONMENT` setting is used by `get_secret(environment)`, and I need to know what that does before I can advise you. – Jeff Booth Feb 01 '22 at 09:09