Skip to main content

Envarify

Environment variables parsing and validation using python type hints

Usage

Having some environment variables:

export TIMEOUT_S=2.5
export API_KEY=some_key
export ALLOWED_IDS=1,2,3
export ENABLE_FEATURE=true

We can create a config object in Python:

from envarify import BaseConfig, EnvVar, SecretString

class MyConfig(BaseConfig):
    timeout_s: float = EnvVar("TIMEOUT_S")
    api_key: SecretString = EnvVar("API_KEY")
    allowed_ids: set[int] = EnvVar("ALLOWED_IDS")
    enable_feature: bool = EnvVar("ENABLE_FEATURE", default=False)
    optional_arg: str | None = EnvVar("OPTIONAL_ARG", default=None)

config = MyConfig.fromenv()
print(config)
#> MyConfig(timeout_s=2.5, api_key='******', allowed_ids={1,2,3}, enable_feature=True, optional_arg=None)

Missing environment variables

If there are required environment variables not set, they will be grouped into one error e.g.:

config = MyConfig.fromenv()
#> MissingEnvVarsError: TIMEOUT_S, API_KEY, ALLOWED_IDS

Supported Types

  • Built-in/standard library types

    • int
    • float
    • bool
    • str
    • typing.StrEnum
    • datetime.date
    • datetime.datetime
  • Special types

    • SecretString

      Masks sensitive environment variables by displaying ****** when printed or logged. The actual value is accessible via the reveal() method, and memory is cleared when object is no longer needed.

      from envarify import BaseConfig, EnvVar, SecretString
      
      class MyConfig(BaseConfig):
          api_key: SecretString = EnvVar("API_KEY")
      
      config = MyConfig.fromenv()
      print(config.api_key)
      
      #> MyConfig(api_key='******')
      
    • Url

      Validates that string is a URL.

      from envarify import BaseConfig, EnvVar, Url
      
      class MyConfig(BaseConfig):
          url: Url = EnvVar("ws://example.com")
      
      config = MyConfig.fromenv()
      print(config.api_key)
      
      #> MyConfig(url='ws://example.com')
      
    • HttpUrl

      Same as Url but validates for http protocol

    • HttpsUrl

      Same as Url but validates for https protocol

    • AnyHttpUrl

      Same as Url but validates for either http or https protocol

  • Dictionary

    • dict / typing.Dict type reads environmental variable as JSON
  • Sequences (delimiter separated values)

    • list[T] / typing.List[T]

    • set[T] / typing.Set[T]

    • tuple[T] / typing.Tuple[T]

      where T is any primitive type

  • BaseConfig subtype itself

    With environment variables COMPONENT_TIMEOUT=5 and OTHER=dummy you can do:

    from envarify import BaseConfig, EnvVar
    
    class ComponentConfig(BaseConfig):
        timeout: int = EnvVar("COMPONENT_TIMEOUT")
    
    class ApplicationConfig(BaseConfig):
        component: ComponentConfig
        other: str = EnvVar("OTHER")
    
    config = ApplicationConfig.fromenv()
    print(config)
    #> ApplicationConfig(component=ComponentConfig(timeout=5), other='dummy')
    

Testing

In tests for your application you don't have to worry about mocking the environment variables. Instead just create a mock config object:

mock_config = MyConfig(timeout_s=4.2, api_key="dummy", allowed_ids={1,2,3}, enable_feature=True)

Metadata

Release files for envarify 1.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for envarify 1.5.1
File Size Uploaded Provenance Actions
envarify-1.5.1.tar.gz 14.5 kB Without provenance Details for envarify-1.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for envarify 1.5.1
File Interpreter ABI Platform Actions
envarify-1.5.1-py3-none-any.whl Python 3 none any Details for envarify-1.5.1-py3-none-any.whl

Total release size: 25.2 kB

Release files / envarify-1.5.1.tar.gz

Download URL envarify-1.5.1.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ecaa3fe3b606368ce626b311d954eb7476dfe93ab9c6b8b7d79838521887c2d6
BLAKE2b-256 checksum
How to use checksums
86d1183b8dfe444efd009b67e1ff751c38f9abc8665012ffb72c01b8c60ae0e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / envarify-1.5.1-py3-none-any.whl

Download URL envarify-1.5.1-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
846086fcbf92d2c9fc8d7c51093a0989854b1e39009357d7fab95a15e9f74c48
BLAKE2b-256 checksum
How to use checksums
7c7b1ca134202cbddbd386e92fa09e581e706d1bf1f60d34f5b55fad5d7b30b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for envarify 1.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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