python-decouple: Manage Environment Variables in Django

Photo by PiggyBank on Unsplash

python-decouple: Manage Environment Variables in Django

To organize your settings to have different parameters in your different environments like development and production you need to work with environment variables.

What is python-decouple

python-decouple is a generic Python library that helps to organize settings (development, production, stage, etc) without having to redeploy the app.

It was specifically built for Django but now is used to manage multiple environments or settings across python development.

Here are the advantages of using python-decouple:

  • You can store variables in .env or ini files.

  • Setting default environment values.

  • Conversion of environment values to correct data type.

Why not just use os.environ

It works, but since os.environ only returns strings, it’s tricky.

Let’s say you have an environment variable DEBUG=False. If you run

if os.environ['DEBUG']: 

It outputs True, because os.environ['DEBUG'] returns the string "False". Since it’s a non-empty string, it will be evaluated as True.

Python Decouple provides a solution that solves this problem like this

from decouple import config
DEBUG = config('DEBUG', cast=bool)

Why use environment variables

A web application whether small or large will have a lot of app secrets such as SECRET_KEY, API keys, database credentials etc.

The convenient way for anyone to use these is to hardcode them in the source code. It works, but it is also the most insecure way. You often push the code to repositories for version management or share it among others resulting in exposing the app secrets.

The easiest and most secure way is to use these secrets as environment variables and import them directly into your app. Python decouple manages this for you. It also helps in managing application configurations and secrets based on the development environments.

How to install python decouple

You can install python-decouple using the command pip install python-decouple

Optional: If you are not using virtual environments to install third party packages, I would strongly advice you to learn about virtual environment to avoid version issues later.

How to use python decouple in django

Step 1: Create a .env file at the root level of your project

--- project
--- .env

Step 2: Set your environment variables inside .env file, I have set mine like this:


Step3: In your, access your environment variables like this:

from decouple import config

DEBUG = config('DEBUG', cast=bool)

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
     'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST'),

That's it for setting up a basic configuration, if you want to know more have a look at the python-decouple official doc.

Note: Add .env file to .gitignore, every user should have their own seperate .env file which should not be pushed alongwith your repository.

A code snippet of python-decouple with django taken from the offical python-decouple doc.

# coding: utf-8
from decouple import config
from unipath import Path
from dj_database_url import parse as db_url

BASE_DIR = Path(__file__).parent

DEBUG = config('DEBUG', default=False, cast=bool)

    'default': config(
        default='sqlite:///' + BASE_DIR.child('db.sqlite3'),

TIME_ZONE = 'America/Sao_Paulo'
USE_L10N = True
USE_TZ = True


EMAIL_HOST = config('EMAIL_HOST', default='localhost')
EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int)
EMAIL_HOST_USER = config('EMAIL_HOST_USER', default='')
EMAIL_USE_TLS = config('EMAIL_USE_TLS', default=False, cast=bool)

How to manage multiple django enviroment

Managing multiple environments is the most common requirement when we work in a team and across multiple servers like local or development, testing, staging, and production.

To solve this problem, you should have .env.template file at the root of your repository and this file should be pushed to your repository. Now any team member can follow this .env.template file to create .env file in their own system with their own set of values and the same with multiple servers.

That's it. I hope you can now manage environment variables.

Did you find this article valuable?

Support Nitin Raturi by becoming a sponsor. Any amount is appreciated!