Skip to main content

align-dotenv

Keep your .env files aligned with their templates — without losing local values.

The template controls structure and known variables. Your local file controls existing values and whether each variable is active or commented out. Unknown local variables are kept by default. Unsupported local syntax causes a safe failure, not data loss.

Install

Python 3.10–3.14 is supported.

uv tool install align-dotenv
# or
pipx install align-dotenv
# or
python -m pip install align-dotenv

For development from a checkout:

python -m pip install .
# or
uv tool install .

Use

Explicit single-file mode

align-dotenv .env --template .env.example
align-dotenv .env --template .env.example --check
align-dotenv .env --template .env.example --unknown keep    # default
align-dotenv .env --template .env.example --unknown remove  # explicitly drop unknown keys
align-dotenv .env --template .env.example --unknown error   # fail, listing key names only

Project mode

Run without a target or template from the project root (the current working directory):

align-dotenv
align-dotenv --check
align-dotenv --unknown keep    # default
align-dotenv --unknown remove
align-dotenv --unknown error

Project mode recursively discovers .env*.example and .env*.template files and removes the suffix to find the corresponding local target. For example:

project/
├── .env                    ← .env.example
├── .env.example
└── apps/api/
    ├── .env.development    ← .env.development.template
    └── .env.development.template

Only existing targets are aligned; templates with missing targets are skipped and reported, never used to create targets. If two templates map to the same target (such as .env.example and .env.template), the command fails without writing. Discovery is sorted by target path and skips .git, node_modules, .venv, venv, and __pycache__ directories, as well as directory symlinks. Project mode validates and reconciles every pair in memory before writing any target; an invalid pair or --unknown error failure prevents all writes. After a successful preflight, changed files are replaced atomically one at a time (not as a cross-file transaction). Unchanged files are not rewritten.

--check performs the same full preflight without writing: exit 0 means all existing targets are aligned, 1 means at least one needs alignment, and 2 means an invalid project state (including ambiguous mappings or unsupported syntax).

Reconciliation

For example, with .env.example:

# Required setting
REQUIRED=template

# OPTIONAL=template

and a local .env:

OPTIONAL='local choice'
REQUIRED=local

alignment produces:

# Required setting
REQUIRED=local

OPTIONAL='local choice'

--check never writes: exit 0 means aligned, 1 means a change is needed. Invalid inputs, unsupported syntax, and --unknown error with unknown keys exit 2.

Syntax and safety

Understand it, preserve it, or refuse to modify it. The supported syntax is single-line KEY=value, export KEY=value, # KEY=value, and # export KEY=value, with keys matching [A-Za-z_][A-Za-z0-9_]*. Values are kept as raw text; this is not a full shell or dotenv parser. Unsupported meaningful local content (such as shell directives, line continuations, or unclosed quoted values) stops the operation without modifying the file. Errors show line numbers, not offending lines or values. Ordinary local comments and blank lines may be omitted because the template defines the layout. The final assignment wins if a key appears repeatedly.

The existing target must be a regular file, not a symlink or the template itself. Known lines use the template's line endings and final newline; unknown lines kept by default retain their original representation, so mixed endings are possible. Changes replace the target atomically in its directory, preserve its mode bits, and skip the write if already aligned.

Develop

PYTHONPATH=src python -m unittest discover -s tests -v
python -m compileall -q src tests

See CONTRIBUTING.md for contributor guidance.

Metadata

Release files for align-dotenv 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for align-dotenv 0.2.0
File Size Uploaded
align_dotenv-0.2.0.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for align-dotenv 0.2.0
File Interpreter ABI Platform
align_dotenv-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / align_dotenv-0.2.0.tar.gz

Download URL align_dotenv-0.2.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
da15253f5cb5d4770a9232a7323a966ae8443ad7437b95b0a5e85d4650fbd7f6
BLAKE2b-256 checksum
How to use checksums
6969608502dc0034e263aa700e80206478e833bff9b90eca6a661f93b6161780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release files / align_dotenv-0.2.0-py3-none-any.whl

Download URL align_dotenv-0.2.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51fbec177fd517f30ba05b8cd582e9d7c4ce96256c0bfdd8371054fab9497814
BLAKE2b-256 checksum
How to use checksums
e24c07591369f66c64b7042a94fe402c05e06186f6a2a25fcfecccf2e54c229f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page