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 setup.py
.
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 manage.py
and wsgi.py
files.
Example .env file:
DJANGO_SETTINGS_MODULE = settings.dev
DJANGO_SECRETKEY = somerandomkey
Example manage.py file:
#!/usr/bin/env python
import os
import sys
import dotenv
if __name__ == "__main__":
# sets DJANGO_SETTINGS_MODULE='settings.dev'
dotenv.load()
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Example settings.py file:
import dotenv
SECRET_KEY = dotenv.get('DJANGO_SECRETKEY', default='fall back value')
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