Django Dotenv

1.4.2 · maintenance · verified Sun Apr 12

django-dotenv is a Python library designed to simplify the loading of environment variables from .env files specifically within Django projects. It aims to make `manage.py` and WSGI applications aware of .env configurations, addressing a common configuration challenge for Django applications. As of its last release, it offers basic .env file parsing functionality.

Warnings

Install

Imports

Quickstart

To integrate django-dotenv, call `dotenv.read_dotenv()` early in your Django project's `manage.py` or `wsgi.py` file. This loads variables from a `.env` file (typically in the project root) into `os.environ`, making them accessible throughout your application.

import os
import dotenv

# Recommended to call early in manage.py or wsgi.py
# For manage.py, place before os.environ.setdefault(...)
# For wsgi.py, place before application = get_wsgi_application()
dotenv.read_dotenv()

# Access environment variables as usual
SECRET_KEY = os.environ.get('SECRET_KEY', 'your-fallback-secret-key')
DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true'
DATABASE_URL = os.environ.get('DATABASE_URL', '')

print(f"SECRET_KEY: {SECRET_KEY}")
print(f"DEBUG: {DEBUG}")
print(f"DATABASE_URL: {DATABASE_URL}")

view raw JSON →