envsleuth
envsleuth parses Python source code with AST, finds reads through
os.getenv(), os.environ[], and os.environ.get(), then reports variables
that are missing from .env.
Install
pip install envsleuth
Usage
# scan current directory, check against ./.env
envsleuth scan
# specific directory, specific env file
envsleuth scan --path ./src --env .env.production
# CI mode — exits 1 if anything is missing
envsleuth scan --strict
# generate a .env.example from your code
envsleuth generate
# machine-readable output
envsleuth scan --json
Example output
Found 6 variables in code
checking against .env
⚠️ AWS_SECRET — not in .env but has default in code (probably ok)
✅ DATABASE_URL
✅ DEBUG
❌ REDIS_URL — missing from .env
at src/app.py:7
✅ SECRET_KEY
❌ STRIPE_API_KEY — missing from .env
at src/app.py:6
⚠️ 1 dynamic usage (variable name computed at runtime, can't check statically)
src/app.py:12 → getenv(name)
ℹ 1 variable in .env not referenced in code: UNUSED_VAR
3 ok 1 with default 2 missing
What it detects
Works with all three common patterns:
import os
a = os.getenv("A") # required — must be in .env
b = os.getenv("B", "fallback") # has default — warned but not required
c = os.environ["C"] # required (would raise KeyError without)
d = os.environ.get("D") # required
Also handles aliased imports:
from os import getenv, environ
import os as sys_os
a = getenv("A")
b = environ["B"]
c = sys_os.getenv("C")
Variables with names computed at runtime (e.g. os.getenv(f"PREFIX_{x}")) can't be checked statically — they're reported in a separate warning section so you know they exist.
Django and config libraries
envsleuth also understands the two most common third-party config patterns:
# django-environ
import environ
env = environ.Env()
SECRET_KEY = env('SECRET_KEY')
DEBUG = env.bool('DEBUG', default=False)
DATABASES = {'default': env.db('DATABASE_URL')}
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=[])
# python-decouple
from decouple import config
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
Calls through env(...), env.get_value(...), and the typed helpers are
detected: str, bytes, bool, int, float, json, list, tuple,
dict, url, db_url/db, cache_url/cache, email_url/email,
search_url, channels_url/channels, and path. FileAwareEnv and
Env.configured(...) are supported too, including defaults declared in their
schemas and statically known env.prefix settings. Aliased imports work as
well: from decouple import config as cfg.
CI: GitHub Actions annotations
Get missing env vars surfaced as PR annotations on the exact source lines:
# .github/workflows/env-check.yml
- name: Check env vars
run: envsleuth scan --output github --strict
Each missing var becomes an ::error annotation; dynamic lookups become
::warning. The format follows GitHub's workflow command
spec.
pre-commit hook
Add envsleuth to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/k38f/envsleuth
rev: v0.3.1
hooks:
- id: envsleuth
# optional overrides
# args: [--path, src, --env, .env]
Runs envsleuth scan --strict when Python, .env, .env.*, or .envignore
files change. There's also an opt-in envsleuth-generate hook for regenerating
.env.example manually via pre-commit run envsleuth-generate --hook-stage manual.
envsleuth generate
Scans your code and writes a .env.example with every variable found, a comment pointing at where it's used, and the default value from code if there is one:
$ envsleuth generate
Wrote 6 variables to .env.example
$ cat .env.example
# Generated by envsleuth — edit this file before committing.
# Each variable below is used somewhere in your code.
# used at src/app.py:8
AWS_SECRET=default-value
# used at src/app.py:3
DATABASE_URL=
# used at src/app.py:5
DEBUG=false
...
Use --force to overwrite an existing file, --output path/to/file to write elsewhere.
Generation is fail-closed: if a source file cannot be scanned or a variable
name cannot be written as a portable environment assignment, the command exits
with code 2 without creating or overwriting the target, even with --force.
Dynamic lookups are preserved as warning comments. Literal defaults are written
only when they can be represented consistently for both python-dotenv and a
POSIX shell; otherwise the value is left blank with a # default omitted note.
On Windows, generation also rejects names that differ only by case (for
example, FOO and foo) because the Windows environment cannot keep them
separate.
.envignore
Exclude variables from the "missing" check with glob patterns — one per line:
# .envignore
TEST_*
LEGACY_*
DEBUG_TOOL
Great for vars that come from CI, Docker, or your shell rc files rather than the local .env.
CLI reference
envsleuth scan
| Flag | Description |
|---|---|
--path, -p |
Directory or file to scan. Default: . |
--env |
Path to .env file. Default: ./.env |
--envignore |
Path to .envignore. Default: ./.envignore if present |
--strict |
Exit with code 1 if vars are missing |
--output, -o |
text (default), json, or github (Actions annotations) |
--json |
Alias for --output json (kept for backwards compat) |
--no-color |
Disable ANSI colors (also honours NO_COLOR env var) |
--exclude DIR |
Extra directory name to skip. Can be repeated |
--ext .EXT |
Extra file extension to scan (e.g. .pyi). Can be repeated |
--verbose, -v |
Show usage locations for every variable |
--no-update-check |
Skip the weekly PyPI version check |
envsleuth generate
| Flag | Description |
|---|---|
--path, -p |
Directory or file to scan. Default: . |
--output, -o |
Where to write. Default: ./.env.example |
--force, -f |
Overwrite existing output file |
--no-color |
Disable ANSI colors in the success message |
--exclude, --ext |
Same as in scan |
--no-update-check |
Skip the weekly PyPI version check |
Exit codes
0— the command completed successfully.1—scan --strictfound required variables missing from an existing.env.2— an operational failure, such as a missing.env, an incomplete scan, an invalid path, or a read/write/generation error. JSON and GitHub output still emit a structured error report first when possible.
Update notifications
envsleuth checks PyPI for new releases at most once per week. When a new version is available, it prints a single line to stderr:
ℹ envsleuth 0.3.1 is available (you have 0.3.0). Run: python -m pip install --upgrade envsleuth
The check is cached, runs with a short timeout, and stays silent on any error (offline, blocked network, etc). To disable it entirely:
# per-command
envsleuth scan --no-update-check
# globally for your shell
export ENVSLEUTH_NO_UPDATE_CHECK=1
The cache lives at ~/.cache/envsleuth/last_check.json (or $XDG_CACHE_HOME/envsleuth/...).
How it compares
| envsleuth | dotenv-linter | python-decouple | |
|---|---|---|---|
| Scans your code for env var usages | ✅ | ❌ | ❌ |
| Lints the .env file itself | ❌ | ✅ | ❌ |
| Runtime config reader with casting | ❌ | ❌ | ✅ |
Generates .env.example from code |
✅ | ❌ | ❌ |
| Language | Python | Rust | Python |
These tools solve different problems: envsleuth scans source code,
dotenv-linter inspects .env files, and python-decouple reads configuration at
runtime.
Dependencies
- click — CLI
- python-dotenv —
.envparsing - flashbar — progress bar used when scanning 20+ files
- packaging — PEP 440 version comparison for update checks
The scanner itself uses only the Python standard library (ast).
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file envsleuth-0.3.1.tar.gz.
File metadata
- Download URL: envsleuth-0.3.1.tar.gz
- Upload date:
- Size: 65.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77bdece2bd048ff305db202535cb0657ce8e1cab9a75deda2b4c7cf683921f26
|
|
| MD5 |
78b97513679bdbf1a9f243a0e2a498a5
|
|
| BLAKE2b-256 |
bb3b5c5e6c21b68d4828621f5d39bec1fac8970f648aef644e7715966e9ddfa9
|
Provenance
The following attestation bundles were made for envsleuth-0.3.1.tar.gz:
Publisher:
release.yml on k38f/envsleuth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envsleuth-0.3.1.tar.gz -
Subject digest:
77bdece2bd048ff305db202535cb0657ce8e1cab9a75deda2b4c7cf683921f26 - Sigstore transparency entry: 2212850782
- Sigstore integration time:
-
Permalink:
k38f/envsleuth@2da6d4708943acc9a95a34764e2242e77616dc6b -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/k38f
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2da6d4708943acc9a95a34764e2242e77616dc6b -
Trigger Event:
push
-
Statement type:
File details
Details for the file envsleuth-0.3.1-py3-none-any.whl.
File metadata
- Download URL: envsleuth-0.3.1-py3-none-any.whl
- Upload date:
- Size: 39.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38f3612fa953ed7ad26fdb66248269ae717707cad9e955a9cfb172b7a192acdd
|
|
| MD5 |
06036324da0dfb82e962bc92acd9683c
|
|
| BLAKE2b-256 |
2a42382f289419c889a519de2aa7cf9da079cc23e5877a1bb948d12ec2129c60
|
Provenance
The following attestation bundles were made for envsleuth-0.3.1-py3-none-any.whl:
Publisher:
release.yml on k38f/envsleuth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envsleuth-0.3.1-py3-none-any.whl -
Subject digest:
38f3612fa953ed7ad26fdb66248269ae717707cad9e955a9cfb172b7a192acdd - Sigstore transparency entry: 2212850860
- Sigstore integration time:
-
Permalink:
k38f/envsleuth@2da6d4708943acc9a95a34764e2242e77616dc6b -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/k38f
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2da6d4708943acc9a95a34764e2242e77616dc6b -
Trigger Event:
push
-
Statement type: