talira

python-dotenv

Read key-value pairs from a .env file and set them as environment variables

open sourceBSD-3-Clausefree

python-dotenv reads key-value pairs from a .env file and can set them as environment variables, so a Python app that takes its configuration from the environment, the 12-factor way, can load it from a file in development and from the real environment in production. load_dotenv() sets the variables in os.environ, while dotenv_values() returns them as a dict without touching the environment, so several files and the environment can be layered. It supports multiline values, POSIX-style ${VAR} expansion, streams and loading in IPython, and an optional dotenv CLI sets, lists and runs commands with a file's variables. PYTHON_DOTENV_DISABLED=1 turns loading off.

Problems it solves

  • load environment variables from a .env file in my Python app
  • keep local development settings out of my code and in a .env file
  • run a command with variables from a .env file

Features

  • load_dotenv() sets .env values in os.environ
  • dotenv_values() returns values as a dict without changing the environment
  • POSIX ${VAR} variable expansion
  • multiline values and streams
  • IPython extension
  • dotenv CLI to set, list and run with a file's variables

Limitations

  • Needs Python 3.10 or later1
  • Only ${VAR} is expanded: bare $VAR references are left as they are2

Alternatives

The same job

These do what python-dotenv does.

  1. dotenvx A secure dotenv, from the creator of dotenv
  2. pydantic-settings Typed Python app settings from environment variables, .env files and secret stores

Compare python-dotenv with the top 3