Skip to main content

🛡️ envguard

Validate your environment variables before they break your app — CLI scanner + runtime guard.

Python 3.8+ License: MIT


🤔 Why envguard?

python-dotenv loads your .env file. But what if a required variable is missing? Your app crashes at runtime — sometimes deep inside a function, hours after startup.

envguard catches missing variables before your app runs.


📦 Installation

pip install envguard

🚀 Two Ways to Use It

1. CLI — scan a project for missing vars

envguard scan ./myproject
🛡️  envguard scan

  📁 Scanning : /home/user/myproject
  📄 .env file: /home/user/myproject/.env

  ✅ DB_HOST          → localhost  (main.py:5)
  ✅ DB_NAME          → mydb       (main.py:6)
  ❌ API_KEY          → MISSING    (api.py:12)
  ⚠️  OLD_VAR         → in .env but not used in code

  Results: 2 ok  |  1 missing  |  1 unused

  💡 Add the missing variables to your .env file.

2. Library — block app startup if vars are missing

from envguard import guard

guard.require("DB_HOST", "DB_NAME", "API_KEY")
guard.typed("PORT", int)
guard.typed("DEBUG", bool)
guard.start()   # raises EnvGuardError if anything is wrong

📖 CLI Reference

envguard scan [path] [options]

Arguments:
  path              Directory to scan (default: current directory)

Options:
  --env FILE        Path to .env file (default: auto-detect)
  --no-unused       Don't report .env vars unused in code
  --strict          Exit with code 1 if any vars are missing

Examples:
  envguard scan .
  envguard scan ./myproject --env .env.production
  envguard scan . --strict

📖 Library Reference

guard.require(*names)

Mark variables as required — fails if missing or empty.

guard.require("DB_HOST", "API_KEY", "SECRET_TOKEN")

guard.typed(name, type)

Require a variable to be castable to a Python type.

guard.typed("PORT", int)       # "5432" → 5432
guard.typed("RATE", float)     # "3.14" → 3.14
guard.typed("DEBUG", bool)     # "true" / "1" / "yes" → True

guard.start()

Run all validations. Raises EnvGuardError with all failures listed.

from envguard import guard, EnvGuardError

try:
    guard.require("DB_HOST").typed("PORT", int).start()
except EnvGuardError as e:
    print(e)
    # envguard: environment validation failed:
    #   ❌ 'DB_HOST' is missing from environment
    #   ❌ 'PORT' is missing (expected int)

guard.check()

Like start() but returns True/False instead of raising.

if not guard.require("API_KEY").check():
    print("Some env vars are missing!")

🔍 What envguard scans for

os.getenv("VAR")              # ✅ detected
os.getenv("VAR", "default")   # ✅ detected
os.environ["VAR"]             # ✅ detected
os.environ.get("VAR")         # ✅ detected

🆚 envguard vs python-dotenv

Feature python-dotenv envguard
Load .env file ✅ ❌ (use together)
Validate required vars ❌ ✅
Type checking ❌ ✅
Scan codebase for usage ❌ ✅ CLI
Report unused .env vars ❌ ✅

They work great together:

from dotenv import load_dotenv
from envguard import guard

load_dotenv()                        # load .env into os.environ
guard.require("DB_HOST", "API_KEY")  # then validate
guard.typed("PORT", int)
guard.start()

🧪 Running Tests

pip install pytest
pytest tests/ -v

📄 License

MIT — free to use in personal and commercial projects.

Metadata

Release files for envguard-py 0.1.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 envguard-py 0.1.0
File Size Uploaded
envguard_py-0.1.0.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for envguard-py 0.1.0
File Interpreter ABI Platform
envguard_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.7 kB

Release files / envguard_py-0.1.0.tar.gz

Download URL envguard_py-0.1.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c7c3e6ef39fe827020dfba4502eae030266d33bb2e89cd5c7cc54e4c79795873
BLAKE2b-256 checksum
How to use checksums
2449f99b502de6d53edf79264c23ec49794ab19fdd2de42e0de68538b166c777
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / envguard_py-0.1.0-py3-none-any.whl

Download URL envguard_py-0.1.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51435c75b7914acb82a93e44486ef1b061569390446a61610e81ac76abf88f38
BLAKE2b-256 checksum
How to use checksums
387dcc1f723e61bab64a52e050460d041ba0ffe4113f18bc2cad8094531135d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

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