Skip to main content

envwrap

CI coveralls codecov codacy

releases pypi/envwrap conda-forge::envwrap

Override parameter defaults via environment variables & config files.

import envwrap

@envwrap.envwrap("name", "app")
def func(a=1):
    ...

Lookup precedence

In descending order:

  • call (func(a=3))
  • environment (NAME_APP_FUNC_A=2, NAME_FUNC_A=2, NAME_APP_A=2, NAME_A=2)
    • UPPER_CASE env vars -> lower_case param names
    • other cases aren't supported because Windows ignores case
  • config file:
    • ./{name}.{toml,yaml,yml,json,ini,cfg}::{app.func.a,func.a,app.a,a}
    • platformdirs.{user,site}_config_path(name, False)/
      • {app}.{toml,yaml,yml,json,ini,cfg}::{func.a,a}
      • {name}.{toml,yaml,yml,json,ini,cfg}::{app.func.a,func.a,app.a,a}
    • ./pyproject.toml::tool.name.{app.func.a,func.a,app.a,a}
  • signature (def foo(a=1))

Typecasting precedence

In descending order:

  • if envwrap.envwrap(convert_config=False): unconverted config file value,
  • if convert_config=False: unconverted config file value
  • signature value's typehint
  • signature default value's type
  • envwrap.envwrap(types={'param_name': type})
  • unconverted
type accepted string values
bool true, yes, on, 1, y, t / false, no, off, 0, n, f, `` (empty)
None none, null, `` (empty)

[!TIP] Containers (list, dict, ...) aren't parsed to avoid mangling strings; use e.g. types={'param_name': ast.literal_eval} instead.

Installation

Any one of:

  • pip install envwrap
  • conda install -c conda-forge envwrap
  • pip install "git+https://github.com/tqdm/envwrap@main"

[!TIP] Note that tqdm ships with a basic tqdm.utils.envwrap, which falls back to the original env-var-only (no config file support) implementation if import envwrap fails.

Advanced Usage

CLI integration

"""CLI example program using envwrap for configuration management.

Usage:
  myapp.py [options] <arg1> [<arg2>]

Options:
  -h, --help    Show this help message and exit.
  -o=<value>, --option=<value>  An option [default: foo].

Arguments:
  <arg1>         An argument.
  <arg2>         An integer argument [default: 2:int].

Defaults above may be overridden by environment variables or config files:
- `MYAPP[_CLI]_*`
- `{.,~/.config/myapp,/etc/xdg/myapp}/myapp[/cli].{toml,yml,json,ini}::[cli.]*`
- `pyproject.toml::tool.myapp[.cli]*`
"""
import argopt, shtab, envwrap

ONLY_PASS_VALID = True # trim envwrap defaults based on parser's valid actions

if __name__ == "__main__":
    parser = argopt.argopt(__doc__)
    shtab.add_argument_to(parser)

    defaults = envwrap.get_defaults("myapp", "", "cli")
    if ONLY_PASS_VALID:
        valid = {i.dest for i in parser._actions}
        defaults = {k: defaults[k] for k in defaults.keys() & valid}

    parser.set_defaults(**defaults)
    args = parser.parse_args()
    print(args)

Live-reload

To force re-reading config files & environment variables without restarting the process:

envwrap.get_defaults.cache_clear()

Debugging

A CLI tool can print defaults. For example, with this config:

# config file: foo.toml
[test]
a = 1337
b = 2
python -m envwrap --help
FOO_A=42 python -m envwrap foo test

will print:

>>> @envwrap.envwrap('foo', '')
>>> def test(...):
...    ...
will use defaults:
{'a': '42', 'b': 2, ...}

contributors

Release files for envwrap 1.1.0

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

Source distribution (sdist)

Source distribution for envwrap 1.1.0
File Size Uploaded
envwrap-1.1.0.tar.gz 13.3 kB Details

Built distribution (wheel)

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

Total release size: 21.1 kB

Release files / envwrap-1.1.0.tar.gz

Download URL envwrap-1.1.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2d5be51ce9a4c84e46f67077cc36aa57a80474a438c78ddf8f9932e2aa282b76
BLAKE2b-256 checksum
How to use checksums
11958050ba81c9f936cc53c5437de48424be785325cfca0a3fd02cc9aee814cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.14.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 Sep 5, 2026.

Transparency log

Release files / envwrap-1.1.0-py3-none-any.whl

Download URL envwrap-1.1.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fea55aa52bbc24dd420f5d921e7ee10f8356bda0ce691be24b02a32d7bd9db68
BLAKE2b-256 checksum
How to use checksums
5805b3359c8a0cfacb004c70d0d0ae21868dd8c736c59ba01f51637c966e770a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.14.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 Sep 5, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

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