python-dotenv
python-dotenv is a Python library that reads key-value pairs from a .env file and sets them as environment variables. The current version is 1.2.2, released in March 2026. The library is actively maintained with regular updates.
Warnings
- breaking Dropped support for Python 3.7; minimum required version is now Python 3.10.
- gotcha Importing dotenv directly instead of from dotenv import load_dotenv will result in ImportError.
- gotcha The load_dotenv function does not override existing environment variables by default.
- gotcha The dotenv run command now forwards flags directly to the specified command.
Install
-
pip install python-dotenv
Imports
- load_dotenv
from dotenv import load_dotenv
Quickstart
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Access environment variables
SECRET_KEY = os.environ.get('SECRET_KEY', '')
DATABASE_PASSWORD = os.environ.get('DATABASE_PASSWORD', '')
print(f'SECRET_KEY: {SECRET_KEY}')
print(f'DATABASE_PASSWORD: {DATABASE_PASSWORD}')