Read key-value pairs from a .env file and set them as environment variables
—
Complete command-line tool for managing .env files with list, get, set, unset, and run commands supporting multiple output formats.
pip install "python-dotenv[cli]"All commands support these global options:
dotenv [OPTIONS] COMMAND [ARGS]...
Options:
-f, --file PATH Location of the .env file, defaults to .env file in current working directory
-q, --quote [always|never|auto] Whether to quote or not the variable values. Default mode is always
-e, --export Whether to write the dot file as an executable bash script
--version Show the version and exit
--help Show help message and exitDisplay all stored key/value pairs with support for multiple output formats.
dotenv list [OPTIONS]
Options:
--format [simple|json|shell|export] The format in which to display the list (default: simple)Usage examples:
# List all variables in simple format
dotenv list
# List in JSON format
dotenv list --format=json
# List in shell format (with proper quoting)
dotenv list --format=shell
# List in export format (prefixed with 'export')
dotenv list --format=export
# List from custom file
dotenv -f /path/to/custom.env listOutput examples:
# Simple format
DATABASE_URL=postgresql://localhost/mydb
DEBUG=True
API_KEY=secret123
# JSON format
{
"API_KEY": "secret123",
"DATABASE_URL": "postgresql://localhost/mydb",
"DEBUG": "True"
}
# Shell format (properly quoted)
DATABASE_URL='postgresql://localhost/mydb'
DEBUG=True
API_KEY=secret123
# Export format
export DATABASE_URL='postgresql://localhost/mydb'
export DEBUG=True
export API_KEY=secret123Retrieve the value for a specific key.
dotenv get KEYUsage examples:
# Get a specific value
dotenv get DATABASE_URL
# Get from custom file
dotenv -f production.env get API_KEY
# Handle missing keys (exits with code 1)
dotenv get NONEXISTENT_KEY
echo $? # Returns 1Store key/value pairs in the .env file.
dotenv set KEY VALUEUsage examples:
# Set a basic key/value pair
dotenv set DATABASE_URL "postgresql://localhost/mydb"
# Set with different quote modes
dotenv -q never set SIMPLE_KEY simple_value
dotenv -q auto set SPACED_KEY "value with spaces"
dotenv -q always set QUOTED_KEY always_quoted
# Set with export prefix
dotenv -e set PATH_ADDITION "/usr/local/bin"
# Set in custom file
dotenv -f production.env set API_KEY "prod-secret-123"
# Set complex values
dotenv set JSON_CONFIG '{"host": "localhost", "port": 5432}'
dotenv set MULTILINE_VALUE "line1
line2
line3"Remove keys from the .env file.
dotenv unset KEYUsage examples:
# Remove a key
dotenv unset OLD_CONFIG
# Remove from custom file
dotenv -f staging.env unset TEMP_KEY
# Handle missing keys (exits with code 1)
dotenv unset NONEXISTENT_KEY
echo $? # Returns 1Execute commands with environment variables loaded from the .env file.
dotenv run [OPTIONS] COMMAND [ARGS]...
Options:
--override/--no-override Override variables from the environment file with those from the .env file (default: --override)Usage examples:
# Run a Python script with .env variables
dotenv run python app.py
# Run with arguments
dotenv run python manage.py migrate
# Run from custom .env file
dotenv -f production.env run python app.py
# Don't override existing environment variables
dotenv run --no-override python app.py
# Run complex commands
dotenv run sh -c 'echo "Database: $DATABASE_URL"'
# Run commands that need specific environment
dotenv run pytest tests/
dotenv run npm start
dotenv run make buildThe run command:
--override (default): .env file variables take precedence over existing environment variables--no-override: existing environment variables take precedence over .env file variablesUse the -f/--file option to specify a custom .env file:
# Different .env files for different environments
dotenv -f .env.development list
dotenv -f .env.staging set API_URL "https://staging-api.example.com"
dotenv -f .env.production run python deploy.py
# Absolute paths
dotenv -f /etc/myapp/.env list
# Relative paths
dotenv -f ../shared.env get SHARED_KEYControl how values are quoted using the -q/--quote option:
# Always quote (default)
dotenv -q always set KEY "value" # KEY='value'
# Never quote
dotenv -q never set KEY value # KEY=value
# Auto quote (quotes only when necessary)
dotenv -q auto set SIMPLE abc # SIMPLE=abc
dotenv -q auto set COMPLEX "a b" # COMPLEX='a b'Generate bash-compatible output using the -e/--export option:
# Set with export prefix
dotenv -e set PATH_VAR "/usr/local/bin"
# Results in: export PATH_VAR='/usr/local/bin'
# List with export format
dotenv -e list --format=export
# Shows all variables prefixed with 'export'
# Create sourceable .env file
dotenv -e -f bash-compatible.env set DATABASE_URL "postgresql://localhost/mydb"
# Now you can: source bash-compatible.envThe CLI tool uses standard exit codes:
# Complete workflow example
cd myproject
# Initialize with some variables
dotenv set DATABASE_URL "postgresql://localhost/mydb"
dotenv set DEBUG "True"
dotenv set SECRET_KEY "dev-secret-key"
# List all variables
dotenv list
# Get specific value
echo "Database: $(dotenv get DATABASE_URL)"
# Run application with environment
dotenv run python app.py
# Export for bash sourcing
dotenv -e list --format=export > environment.sh
source environment.sh
# Clean up
dotenv unset SECRET_KEYInstall with Tessl CLI
npx tessl i tessl/pypi-python-dotenv