A small Python library and CLI tool for coercing environment variables (and arbitrary strings) into boolean values.
Project description
envbool
Coerce environment variables to booleans.
Every project ends up with some version of this:
DEBUG = os.environ.get("DEBUG", "").lower() in ("1", "true", "yes")
VERBOSE = os.environ.get("VERBOSE", "").lower() in ("1", "true", "yes")
CACHE = os.environ.get("CACHE", "").lower() in ("1", "true", "yes")
envbool replaces that:
from envbool import envbool
DEBUG = envbool("DEBUG")
VERBOSE = envbool("VERBOSE")
CACHE = envbool("CACHE")
It also handles strict mode, warnings, custom value sets, config files, and a CLI for shell scripts.
Installation
pip install envbool
# or
uv add envbool
Usage
Lenient by default. Anything not recognized as truthy returns False. Unset and empty variables return the default.
from envbool import envbool
DEBUG = envbool("DEBUG") # False if unset or empty
CACHE = envbool("CACHE", default=True) # True if unset or empty
The built-in truthy values are true, 1, yes, on. Comparison is case-insensitive.
Strict mode raises InvalidBoolValueError for unrecognized values — useful for catching typos in production config.
from envbool import envbool, InvalidBoolValueError
try:
USE_SSL = envbool("USE_SSL", strict=True)
except InvalidBoolValueError as e:
print(f"Bad value for USE_SSL: {e.value!r}")
sys.exit(1)
Extend the value sets when your environment uses non-standard strings.
FEATURE = envbool("FEATURE_FLAG", extend_truthy={"enabled", "y"})
Coerce an arbitrary string (not from os.environ) with to_bool:
from envbool import to_bool
to_bool("yes") # True
to_bool("0") # False
to_bool("maybe", strict=True) # raises InvalidBoolValueError
CLI
The envbool command exits 0 for truthy and 1 for falsy, so it works naturally in shell scripts.
# Control flow via exit code
envbool DEBUG && echo "debug is on"
# Print the resolved value
echo "Verbose: $(envbool --print VERBOSE)"
# Pipe a string
echo "yes" | envbool && echo "truthy"
# Strict mode
envbool --strict ENABLE_CACHE || echo "cache is off or misconfigured"
Configuration
Put shared defaults in envbool.toml (or [tool.envbool] in pyproject.toml) at your project root:
strict = true
extend_truthy = ["enabled"]
extend_falsy = ["disabled"]
envbool walks up from the current directory to find the nearest config file, then falls back to a user-level config (~/.config/envbool/config.toml on Linux/macOS). Function arguments always override the config.
Set ENVBOOL_NO_CONFIG=1 to skip config discovery entirely.
API
| Symbol | Description |
|---|---|
envbool(var, ...) |
Read an env var and return bool |
to_bool(value, ...) |
Coerce a string to bool |
load_config() |
Inspect the loaded config |
DEFAULT_TRUTHY |
frozenset of built-in truthy strings |
DEFAULT_FALSY |
frozenset of built-in falsy strings |
InvalidBoolValueError |
Raised in strict mode on unrecognized values |
ConfigError |
Raised when a config file is malformed |
Both envbool() and to_bool() accept the same keyword arguments: default, strict, warn, truthy, falsy, extend_truthy, extend_falsy. truthy/falsy replace the effective set; extend_truthy/extend_falsy add to it.
Project details
Release history Release notifications | RSS feed
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 envbool-0.1.1.tar.gz.
File metadata
- Download URL: envbool-0.1.1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
929ef3eaf74a3db86ec6c72a7b74baa6c5ced25263121272f4712576d802a3ba
|
|
| MD5 |
9f60852ff9ac339abe97c39f1d6b6d2f
|
|
| BLAKE2b-256 |
b6871a965f11556bb0fe8c97cdd952f380b0da0e3e9ad5f99146059ed443b498
|
Provenance
The following attestation bundles were made for envbool-0.1.1.tar.gz:
Publisher:
cd.yml on jkomalley/envbool
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envbool-0.1.1.tar.gz -
Subject digest:
929ef3eaf74a3db86ec6c72a7b74baa6c5ced25263121272f4712576d802a3ba - Sigstore transparency entry: 1383135281
- Sigstore integration time:
-
Permalink:
jkomalley/envbool@c5b04565f192d8acdb62494fe526bcccb5130a13 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jkomalley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@c5b04565f192d8acdb62494fe526bcccb5130a13 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file envbool-0.1.1-py3-none-any.whl.
File metadata
- Download URL: envbool-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebded78e437c5ab980997f2ea918ebd2f5ba1339eae802e15ef7e390d093ce27
|
|
| MD5 |
a7e1b11305b1b619e8c607f299266330
|
|
| BLAKE2b-256 |
8a5964b34c3c96272a9e46141114c65787a764a82efd32d0108b6364fcfca687
|
Provenance
The following attestation bundles were made for envbool-0.1.1-py3-none-any.whl:
Publisher:
cd.yml on jkomalley/envbool
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envbool-0.1.1-py3-none-any.whl -
Subject digest:
ebded78e437c5ab980997f2ea918ebd2f5ba1339eae802e15ef7e390d093ce27 - Sigstore transparency entry: 1383135293
- Sigstore integration time:
-
Permalink:
jkomalley/envbool@c5b04565f192d8acdb62494fe526bcccb5130a13 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jkomalley
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@c5b04565f192d8acdb62494fe526bcccb5130a13 -
Trigger Event:
workflow_run
-
Statement type: