Skip to main content

Typed environment variable loader with validation rules

Project description

Environy

Typed environment variable loader with .env support and validation rules.

Environy loads values from os.environ first, then loads the configured env file into the same value set. The default env file is .env, and file values win when a key exists in both places.

Installation

pip install environy

Quick Start

from environy import Environy

env = Environy()

app_name = env.string("ENVIRONY_APP_NAME")
port = env.port("ENVIRONY_PORT", default=8080)
timeout = env.float("ENVIRONY_TIMEOUT", default=2.5)
debug = env.boolean("ENVIRONY_DEBUG", default=False)
api_url = env.url("ENVIRONY_API_URL")
config_file = env.file_path("ENVIRONY_CONFIG_FILE")
data_dir = env.folder_path("ENVIRONY_DATA_DIR")
mode = env.enum("ENVIRONY_MODE", ["development", "staging", "production"])

Optional Values

Values are required by default. Pass required=False to return None when a key is absent and no default was provided.

required_name = env.string("ENVIRONY_APP_NAME")
optional_note = env.string("ENVIRONY_OPTIONAL_NOTE", required=False)

Value Validation

Validate a parsed value with a lambda or callable. The callable receives the parsed value first, then any extra positional args passed after it.

def is_between(value: int, minimum: int, maximum: int) -> bool:
    return minimum <= value <= maximum

port = env.integer("ENVIRONY_PORT", is_between, 1, 65535)
name = env.string(
    "ENVIRONY_APP_NAME",
    lambda value, prefix: value.startswith(prefix),
    "environy-",
)
timeout = env.float("ENVIRONY_TIMEOUT", lambda value, maximum: value <= maximum, 5.0)

Use default as a keyword when combining it with validation.

port = env.integer("ENVIRONY_PORT", is_between, 1, 65535, default=8080)

Dependency Rules

Validate relationships between multiple variables after loading them.

env.require_distinct([
    "ENVIRONY_PRIMARY_REGION",
    "ENVIRONY_SECONDARY_REGION",
    "ENVIRONY_TERTIARY_REGION",
])

env.require_allowed_when(
    "ENVIRONY_MODE",
    "production",
    "ENVIRONY_LOG_LEVEL",
    ["warning", "error"],
)

env.require_present_when("ENVIRONY_AUTH_MODE", "oauth", "ENVIRONY_CLIENT_SECRET")

Env Files

Use a custom env file:

env = Environy(env_file=".env.local")

Disable env file loading:

env = Environy(env_file=None)

Boolean values accept true/false, yes/no, on/off, and 1/0.

Development

python -m pip install -e ".[test,build]"
python -m pytest
python -m build
python -m twine check dist/*

Run the example:

python examples/basic.py

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

environy-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

environy-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file environy-0.1.0.tar.gz.

File metadata

  • Download URL: environy-0.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for environy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5211dcee6b98ef36ef2b8cc237ae04f5f735eff07b6853ece63cf2bf2db88ff0
MD5 974fc36fbc88a1614258f518795ea6b1
BLAKE2b-256 8e54e5354c3a6d212963eef9546493a654e08c7d47b91f6332bff1ed7444a52b

See more details on using hashes here.

Provenance

The following attestation bundles were made for environy-0.1.0.tar.gz:

Publisher: publish.yml on twoNDchances/environy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file environy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: environy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for environy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32d606337ca2663bb5eebea472e41919fe09f9716b42eb8ba5795cb1b479b0c3
MD5 c3b38b5602412604f73f425dff049311
BLAKE2b-256 57611fc27853cb9a76c0ff5a7003e0d69b4f819bfeb8c97ba10cd4491a2e2be8

See more details on using hashes here.

Provenance

The following attestation bundles were made for environy-0.1.0-py3-none-any.whl:

Publisher: publish.yml on twoNDchances/environy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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