Skip to main content

A minimal Python library to validate environment variables at startup. Zero dependencies.

Project description

envgate

CI codecov PyPI version Python License: MIT

A minimal Python library to validate environment variables at startup. Zero dependencies.

Why?

Instead of your app crashing at runtime because DATABASE_URL is missing, envgate validates everything at startup and tells you exactly what's wrong.

Installation

pip install envgate

Quick Start

from envgate import get_env, validate

# Get a single variable with type coercion
port = get_env("PORT", type="int", default=8000)
debug = get_env("DEBUG", type="bool", default=False)

# Explicitly mark a variable as required
api_key = get_env("API_KEY", required=True)

# Parse comma-separated lists (or use a custom separator)
hosts = get_env("ALLOWED_HOSTS", type="list")            # ["a", "b", "c"]
ports = get_env("PORTS", type="list[int]", sep=":")      # [8000, 8001]

# Or validate multiple variables at once
config = validate({
    "DATABASE_URL": {"type": "str"},
    "REDIS_URL": {"type": "str"},
    "PORT": {"type": "int", "default": 8000},
    "DEBUG": {"type": "bool", "default": False},
})

If DATABASE_URL and REDIS_URL are missing and PORT is invalid, you get all errors at once:

envgate.exceptions.ValidationError: Environment validation failed:
    - Environment variable 'DATABASE_URL' is not set.
    - Environment variable 'REDIS_URL' is not set.
    - Environment variable 'PORT' has invalid value 'abc' (expected int).

Custom validators

Type coercion checks that PORT is an integer. A validator checks that the value also makes sense — e.g. that the port is in a usable range, or that a log level is one of a fixed set:

def in_port_range(p):
    if not (1024 <= p <= 65535):
        raise ValueError("must be in [1024, 65535]")

def is_known_level(level):
    if level not in {"debug", "info", "warning", "error"}:
        raise ValueError("must be one of debug|info|warning|error")

config = validate({
    "PORT": {"type": "int", "validator": in_port_range},
    "LOG_LEVEL": {"type": "str", "default": "info", "validator": is_known_level},
})

A validator signals failure by raising any exception — its message is captured and joined into the same collective ValidationError as missing and invalid-type errors:

envgate.exceptions.ValidationError: Environment validation failed:
    - Environment variable 'PORT' has invalid value '80': must be in [1024, 65535]
    - Environment variable 'LOG_LEVEL' has invalid value 'verbose': must be one of debug|info|warning|error

Supported Types

Type Example values
str Any string (default)
int "42", "-7", "0"
float "3.14", "42", "-2.5"
bool "true", "1", "yes", "on" / "false", "0", "no", "off"
list, list[str], list[int], list[float], list[bool] Comma-separated values — e.g. "a,b,c"["a", "b", "c"]. Pass sep=":" (or any character) to override the separator.

Contributing

Contributions are welcome! Check out the CONTRIBUTING.md for guidelines.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envgate-0.6.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envgate-0.6.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file envgate-0.6.0.tar.gz.

File metadata

  • Download URL: envgate-0.6.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for envgate-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d5002ffbdbd277c729e72f64b0709fa43274d1a1f97756ce66cf04acde09065d
MD5 c494db7a72935d207008ba1102877d8c
BLAKE2b-256 819753febecc3fcdbd042b33adb5bc3e375225e021adf9ece67e8ed78c035ad6

See more details on using hashes here.

File details

Details for the file envgate-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: envgate-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for envgate-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99b8e1ae7a8b55454cce45b3b2187ae3297e38d12c59b5e2f0f9f14caa43bf98
MD5 67967510760fb7d4bfdd19a63970d5dc
BLAKE2b-256 00916eb816061fa0148e54ad399af9c8444330389ac085192d327fc4645fdea0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page