python-dotenv

python-dotenv reads key-value pairs from a .env file and can set them as environment variables.

Warning

When including private API keys in your .env file, make sure you DO NOT make your project "Open Source" when you share it.

Examples

1, Create a .env file in your project.

2, Set some environment variables in the key=value format like this:

DOMAIN=example.org
ADMIN_EMAIL=admin@${DOMAIN}
ROOT_URL=${DOMAIN}/app

3, In your code, load variables via this library.

import os
from dotenv import load_dotenv

# load all environment variables from .env file
load_dotenv()
# test, it should print "admin@example.org"
print(os.environ.get("ADMIN_EMAIL", "[not set]"))

Reference