cattrs-env

raw JSON →
1.0.4 verified Sat May 09 auth: no python

A Python library for parsing and validating environment variables using cattrs structured configuration. Version 1.0.4 supports Python >=3.7. Released under MIT license. Low release cadence.

pip install cattrs-env
error ModuleNotFoundError: No module named 'cattrs_env'
cause Library not installed or installed with a different name (e.g., cattrs-env).
fix
pip install cattrs-env
error TypeError: EnvSettings() takes exactly 1 argument (0 given)
cause Missing the attrs class argument when instantiating EnvSettings.
fix
EnvSettings(Config) where Config is an attrs-decorated class.
gotcha Environment variable names are uppercased by default. A field 'host' expects env var 'HOST'.
fix Use the 'var_name' argument in the field metadata to override: host: str = field(metadata={'var_name': 'MY_HOST'})
gotcha cattrs-env uses ast.literal_eval to parse string values (e.g., '8080' becomes int). If the string is not a valid Python literal, it falls back to str type without warning. This can silently misparse values like '8080 ' (with trailing space) as string.
fix Ensure env var values are valid Python literals (e.g., no extra whitespace, quotes for strings). Use the 'type' argument if you need explicit control.
deprecated No deprecations known.
fix N/A

Define an attrs class and use EnvSettings to automatically populate fields from environment variables with matching names (e.g., HOST, PORT).

from attrs import define
from cattrs_env import EnvSettings

@define
class Config:
    host: str = 'localhost'
    port: int = 8080

config = EnvSettings(Config).parse_env()
print(config.host, config.port)