Read .env file (key->value) setting values as environment variables
Clone or download

.env files for Python and Django

Build Status Downloads this month

A .env file is a text file holding values which will be added into the applications environment variables. The file uses the following format; a single key=value pair on each line.


Install via pip: pip install python-env or by download the package. Running python


Loading environment variables into os.environ:

Add the following line to your main python file.

import dotenv

Filepath can be blank if .env is in current working directory

dotenv.load() or dotenv.load('/path/to/file')


To load .env into django add the following lines of code into the and files.

Example .env file:

DJANGO_SECRETKEY = somerandomkey

Example file:

#!/usr/bin/env python
import os
import sys

import dotenv

if __name__ == "__main__":

    from import execute_from_command_line


Example file:

import dotenv
SECRET_KEY = dotenv.get('DJANGO_SECRETKEY', default='fall back value')

Using environment variables:

You can use the set environment variables using either os.environ or dotenv.get. dotenv.get has the added advantage of converting to python type. Therefore, a key value set of KEY=True will be returned as a bool type not string as os.environ would normally do.

dotenv.get can be used within your application by:

# key=12345
return_val = dotenv.get('key')
> 1234 # type int