Alec D'Alelio
Alec D'Alelio

Reputation: 11

Unable to load environment variable (.env) in Django App

I am trying to launch a Django project and am unable to do so because it seems my local environment is disconnected from my .env file in the root directory. I've never had this problem before and assume there is an easy fix but I can't seem to figure it out. When I run basic commands like runserver I get the following error:

RATE_LIMIT_DURATION = int(os.environ.get("RATE_LIMIT_DURATION"))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

In my .env file, I have defined the variable as:

RATE_LIMIT_DURATION = 10

I have already confirmed that my database is setup and I am pretty sure my settings are in good shape otherwise.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": os.environ.get("DB_NAME"),
        "USER": os.environ.get("DB_USER"),
        "PASSWORD": os.environ.get("DB_PASSWORD"),
        "HOST": "localhost",
        "PORT": 8000,
    }
}

Upvotes: 0

Views: 1561

Answers (1)

Simply_Normal
Simply_Normal

Reputation: 31

When I want to include .env to my django project I just use load_dotenv from dotenv python library.

So basically in settings.py I put:

from dotenv import load_dotenv

load_dotenv()

Upvotes: 1

Related Questions