Skip to main content

Simple app configuration via environment variables, in the spirit of argparse.

Project description

envappconfig

envappconfig is intended to provide simple configuration via environment variables, in the same spirit as argparse, which can be useful when developing and deploying 12-factor apps.

Features:

  • Autogenerates usage output if an environment variable is missing
  • Default settings for missing environment variables
  • Functions that transform the environment variable string to the value type you need
  • Environment variable prefixes

Basic example

from envappconfig import EnvAppConfig

env = EnvAppConfig(description='Amazing app')
env.add_env('port', default=1234, transform=int, help='The listen port')
env.add_env('mirror', help='The URL to mirror')
config = env.configure()

# Returns PORT from os.environ transformed to an int,
# or 1234 if PORT does not exist.
config.port

# Returns MIRROR from os.environ,
# or displays usage at env.configure()
# if MIRROR does not exist, then exits.
config.mirror

Adding a prefix

If all the environment variables for the app have the same prefix, it can be specified with the prefix parameter.

from envappconfig import EnvAppConfig

env = EnvAppConfig(prefix='MYAPP', description='Amazing app')
env.add_env('port', default=1234, transform=int, help='The listen port')
env.add_env('mirror', help='The URL to mirror')
config = env.configure()

# Returns MYAPP_PORT from os.environ transformed to an int,
# or 1234 if MYAPP_PORT does not exist.
config.port

# Returns MYAPP_MIRROR from os.environ,
# or displays usage at env.configure()
# if MYAPP_MIRROR does not exist, then exits.
config.mirror

Custom transforms

The transform parameter can be used to specify normal transforms, like int or float (the default is str), but it can also take custom transform functions. The transform function must take a single parameter, which will be filled in with the string value from the environment variable.

env = EnvAppConfig(description='Amazing app')

# Double the timeout specified in the TIMEOUT environment variable,
# or default to 60.
env.add_env('timeout', default=60, transform=lambda x: int(x) * 2, help='Timeout in seconds')
...

Adding more config values

Additional config values can be added to an existing namespace, which can be helpful when there's a config value that needs to be calculated based on other config values.

from envappconfig import EnvAppConfig

env = EnvAppConfig(description='Amazing app')
env.add_env('bind', help='IP address to bind to')
env.add_env('port', default=1234, transform=int, help='The listen port')
config = env.configure()
config.listen = f'{config.bind}:{config.port}'

# Returns the combined bind:port string.
config.listen

Forcing usage display

The environment variable usage output for an app can be forced by defining the ENVAPPCONFIG_SHOW_USAGE environment variable to any value. This is basically like --help for apps that take command line parameters. Forcing usage display can be useful when you've got a pre-built container for an app that uses envappconfig, and you need to see all the environment variable options. For example:

docker run -it --rm -e ENVAPPCONFIG_SHOW_USAGE=1 amazing:latest

or in Kubernetes:

env:
  - name: ENVAPPCONFIG_SHOW_USAGE
    value: "1"

Command Line

There are a couple options for using envappconfig at the command line (eg. when testing).

Prefix

If you've only got a couple environment variables to set, just put them before the command:

PORT=9999 NAME=foo python3 script_using_envappconfig.py

dotenv

If you have more environment variables to set, consider using dotenv. First put your environment variables in a file named .env:

PORT=9999
NAME=foo

Then call dotenv as follows, which will load up the variables from .env for this command:

dotenv run -- python3 script_using_envappconfig.py

You can install the dotenv command line tool with:

python3 -m pip install "python-dotenv[cli]"

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

envappconfig-2024.339.1353.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

envappconfig-2024.339.1353-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file envappconfig-2024.339.1353.tar.gz.

File metadata

  • Download URL: envappconfig-2024.339.1353.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for envappconfig-2024.339.1353.tar.gz
Algorithm Hash digest
SHA256 942033ceab4bf331569d3532c90511fcf6f6879e7a65f51d857ccb54c5d3d276
MD5 e1870a5d0958b3e90abe18abe4bd8d5d
BLAKE2b-256 7cab78b577aa3be33f8a6a65851625296f3729c888483e51a7c4abc2aa1ba942

See more details on using hashes here.

Provenance

The following attestation bundles were made for envappconfig-2024.339.1353.tar.gz:

Publisher: CI.yml on spectriclabs/envappconfig

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

File details

Details for the file envappconfig-2024.339.1353-py3-none-any.whl.

File metadata

File hashes

Hashes for envappconfig-2024.339.1353-py3-none-any.whl
Algorithm Hash digest
SHA256 38670ea671a24165a582e2393b435e3519cf7ca51cc8c59aaccb8680afb78717
MD5 e142e123ab7b9df3016f3ccd4b581b95
BLAKE2b-256 d5e8686400562a9a7d330e3ef400813bda498eea8aa9cdc0fd69bf42265670db

See more details on using hashes here.

Provenance

The following attestation bundles were made for envappconfig-2024.339.1353-py3-none-any.whl:

Publisher: CI.yml on spectriclabs/envappconfig

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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page