How To: Using Python Decouple for Environment Variables in Django

Django

Introduction

Managing environment variables correctly is crucial for keeping sensitive information like API keys, database credentials, and secret keys secure. I previously covered How To: Environment Variables In Django but that isn't the only way, another easy way to manage environment variables in Django is by using the python-decouple package. Here I will walk you through setting up and using python-decouple effectively in your Django projects.

Why Use Python Decouple?

Similar to django-environ, Python Decouple helps:

  • Keep sensitive data out of your codebase

  • Make configuration changes easier without modifying code

  • Improve security by storing secrets in a separate file

Step-by-Step Setup

Step 1: Install Python Decouple

Run the following command to install python-decouple:

pip install python-decouple

Step 2: Create a .env File

In your project’s root directory (where settings.py is located), create a new .env file. This file will store your environment variables.

Example .env file:

SECRET_KEY=h^z13$qr_s_wd65@gnj7a=xs7t05$w7q8!x_8zsld#
DATABASE_NAME=postgresdatabase
DATABASE_USER=alice
DATABASE_PASS=supersecretpassword
DEBUG=True

Step 3: Modify settings.py

Update your settings.py file to use python-decouple for loading environment variables:

from decouple import config

SECRET_KEY = config("SECRET_KEY")
DEBUG = config("DEBUG", default=False, cast=bool)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config("DATABASE_NAME"),
        'USER': config("DATABASE_USER"),
        'PASSWORD': config("DATABASE_PASS"),
    }
}

Step 4: Add .env to .gitignore

To prevent your environment variables from being pushed to version control, add .env to your .gitignore file:

echo ".env" >> .gitignore

Advanced Features of Python Decouple

Casting Values

By default, all values in .env are treated as strings. You can use casting to convert them into other types:

DEBUG = config("DEBUG", default=False, cast=bool)
PORT = config("PORT", default=8000, cast=int)

Using a .ini Configuration File

Instead of a .env file, you can use a .ini configuration file for structured settings:

[settings]
SECRET_KEY=h^z13$qr_s_wd65@gnj7a=xs7t05$w7q8!x_8zsld#
DEBUG=True

Then, in settings.py, specify the configuration file:

from decouple import Config, RepositoryIni
config = Config(RepositoryIni("settings.ini"))

Why Use an .ini File Instead of a .env File?

  • Better Organization: .ini files allow settings to be grouped into sections.

  • Easier to Read & Manage: If you have a lot of configuration settings, .ini files can be more structured and manageable.

  • Multiple Configurations: You can separate different environments (e.g., [development], [production]) within the same .ini file.

When to Use Python Decouple

Python Decouple is ideal for:

  • ✅ Django applications that require simple, secure environment variable management.

  • ✅ Projects where configurations need to change without modifying code.

  • ✅ Teams following best practices for keeping secrets out of version control.

Conclusion

Python Decouple is a lightweight, easy-to-use tool that helps keep Django settings clean and secure. By using .env files, you can safely manage sensitive data while keeping your codebase organized.