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]"
Metadata
Release files for envappconfig 2024.339.1353
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| envappconfig-2024.339.1353.tar.gz | 7.7 kB | With provenance | Details for envappconfig-2024.339.1353.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| envappconfig-2024.339.1353-py3-none-any.whl | Python 3 | none | any | Details for envappconfig-2024.339.1353-py3-none-any.whl |
Total release size: 16.2 kB
Release files / envappconfig-2024.339.1353.tar.gz
| Download URL | envappconfig-2024.339.1353.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
942033ceab4bf331569d3532c90511fcf6f6879e7a65f51d857ccb54c5d3d276
|
|
BLAKE2b-256 checksum How to use checksums |
7cab78b577aa3be33f8a6a65851625296f3729c888483e51a7c4abc2aa1ba942
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2024.
Transparency logRelease files / envappconfig-2024.339.1353-py3-none-any.whl
| Download URL | envappconfig-2024.339.1353-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
38670ea671a24165a582e2393b435e3519cf7ca51cc8c59aaccb8680afb78717
|
|
BLAKE2b-256 checksum How to use checksums |
d5e8686400562a9a7d330e3ef400813bda498eea8aa9cdc0fd69bf42265670db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2024.
Transparency logSecurity for envappconfig 2024.339.1353
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for envappconfig-2024.339.1353.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2024.
Transparency logA 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.