Using Environment Variables In Django

While working with web applications often we need to store sensitive data for authentication of different modules such as database credentials and API keys. These sensitive keys should not be hardcoded in the file instead they should be loaded with Environment variables on runtime.

An environment variable is a variable whose value is set outside the program, typically through a functionality built into the operating system. An environment variable is made up of a name/value pair.

Environment variables help us keep secrets (for example, Passwords, API tokens, and so on) out of version control, therefore, they are considered an integral part of the popular Twelve-Factor App Design methodology and a Django best practice because they allow a greater level of security and simpler local/production configurations.

Also, environment variables provide a greater degree of flexibility for switching between local development setup and production setup.

Therefore Adding environment variables is a necessary step for any truly professional Django project.

Creating Environment Variables

Create a .env file in the same directory where resides and add the following key-value pair inside the file.


We will use django-environ for managing environment variables inside our project. So let’s install the package.

pip install django-environ

With that now, we can access the environmental variables in our codebase.

import environ

env = environ.Env()
# reading .env file

# Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env("DATABASE_NAME"),
        'USER': env("DATABASE_USER"),
        'HOST': env("DATABASE_HOST"),
        'PORT': env("DATABASE_PORT"),

Save the file and run the server everything should be working smoothly.

Additionally, you can also provide default values as follows.

SECRET_KEY = env("SECRET_KEY", default="unsafe-secret-key")

Don’t forget to add <span class="pre">.env</span> in your <span class="pre">.gitignore</span> also, it’s advisable to create a .env.example with a template of all the variables required for the project.


Support Django Central

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.
Buy Me a Coffee at

14 thoughts on “Using Environment Variables In Django”

  1. If you are leaving .env out of your repo via gitignore, how is this supposed to get your variables on an actual production deployment?

  2. And how about anyone using >= Django 2.1? The documentation for django-environ says it supports up to 2.0. We are using 2.1, and the import environ is not recognized. What’s the solution for 2020?

  3. I followed the instructions here and am getting this error ModuleNotFoundError: No module named ‘environ’

    Any idea why it’s not finding the environ module?

    • may be you didnt install the package correctly
      may be you didnt import enviorn
      may be you installed the package globally while using a virtual env .


Leave a Comment