A zero-dependency Python tool for comparing, validating, and managing .env files. Diff environments, compare servers, validate against schemas, and generate .env.example files.
- Diff two
.envfiles (show added, removed, changed keys) - Compare environment variables between servers via SSH
- Export current environment to a file
- Validate
.envagainst a JSON schema (required keys, types, allowed values, patterns) - Generate
.env.examplefrom.env(strips sensitive values automatically) - Merge
.envfiles with conflict resolution strategies
Copy env_diff.py into your project or add it to your PATH. No dependencies required (Python 3.7+).
chmod +x env_diff.pypython env_diff.py diff .env.staging .env.production--- Removed (in .env.staging but not .env.production) ---
- DEBUG=***
+++ Added (in .env.production but not .env.staging) ---
+ SENTRY_DSN=***
~~~ Changed ---
~ DATABASE_URL (values differ)
Summary: 1 added, 1 removed, 1 changed, 8 unchanged
Show actual values:
python env_diff.py diff .env.staging .env.production --show-valuespython env_diff.py compare user@staging-server user@prod-server
python env_diff.py compare server1 server2 --filter "DATABASE,REDIS,API"python env_diff.py export -o env_snapshot.txt
python env_diff.py export --filter "NODE,npm,PATH" -o node_env.txtpython env_diff.py validate .env schema.jsonschema.json:
{
"required": ["DATABASE_URL", "API_KEY", "NODE_ENV"],
"properties": {
"DATABASE_URL": {
"type": "url",
"required": true
},
"API_KEY": {
"type": "string",
"min_length": 20
},
"NODE_ENV": {
"type": "string",
"allowed": ["development", "staging", "production"]
},
"PORT": {
"type": "integer"
},
"DEBUG": {
"type": "boolean"
},
"ADMIN_EMAIL": {
"type": "email"
},
"WEBHOOK_URL": {
"type": "url",
"pattern": "^https://"
}
},
"allow_extra": true
}VALIDATION PASSED
0 errors, 0 warnings
python env_diff.py example .env
python env_diff.py example .env -o .env.example
python env_diff.py example .env --keep-values # keep non-sensitive valuesSmart placeholders:
DATABASE_URL=https://example.com
API_KEY=your-secret-here
PORT=3000
DEBUG=false
ADMIN_EMAIL=user@example.com
# Overlay wins on conflicts (default)
python env_diff.py merge .env.defaults .env.local -o .env
# Base wins on conflicts
python env_diff.py merge .env.defaults .env.local -s base -o .env
# Ask interactively on each conflict
python env_diff.py merge .env.defaults .env.local -s ask -o .envThe validation schema is a JSON file with these fields:
| Field | Description |
|---|---|
required |
Array of required key names |
properties |
Object mapping key names to validation rules |
allow_extra |
Boolean, whether to warn on unknown keys (default: true) |
| Rule | Description |
|---|---|
type |
Expected type: string, integer, number, boolean, url, email |
required |
Boolean, whether this key is required |
allowed |
Array of allowed values |
pattern |
Regex pattern the value must match |
min_length |
Minimum value length |
MIT