Read environment variables from file in Python

A .env file is a text file containing key value pairs of environment variables. This file is normally included with a project, but not committed to source.

How to read .env files using Python

Firstly, install the dotenv package.

1
pip install python-dotenv

Include the folllowing lines:

1
2
3
from dotenv import load_dotenv

load_dotenv()

Using load_dotenv(), the application will load the .env file and host environment variables. If this is the .env file for the application:

1
VARIABLE1=test

Then the output will be test for the following snippet:

1
2
3
4
5
6
from dotenv import load_dotenv
import os

load_dotenv()

print(os.environ.get("VARIABLE1")) # outputs test

However, if the host environment has a VARIABLE1 defined:

1
export VARIABLE1=test2

The output will change to test2.

If this is not desired behavior, the author may opt to use dotenv_values which returns a dict of values strictly parsed from the .env file:

1
2
3
4
from dotenv import dotenv_values

config = dotenv_values()
print(config) # outputs OrderedDict([('VARIABLE1', 'test')])

If the .env file resides on a different path than the default root directy of the project, use the dotenv_path option:

1
2
3
4
from dotenv import load_dotenv
from pathlib import Path

load_dotenv(dotenv_path=Path("path/to/file"))