Skip to main content

Pypi PypiDownloads ReadTheDocs GithubActions Codecov GitlabCIPipeline GitlabCICoverage

kwconf defines small configuration objects that work from Python kwargs, command line arguments, environment variables, and JSON/YAML files. It is the successor to scriptconfig, with the same small-script ergonomics and a clearer parser model.

Read the Docs

http://kwconf.readthedocs.io/en/latest/

Github

https://github.com/Erotemic/kwconf

Pypi

https://pypi.org/project/kwconf

Features

  • Define config once, then read it from kwargs, argv, env, or files.

  • Use the object like a dataclass, dict, or argparse namespace.

  • Start with plain defaults. Add Value for help text, aliases, choices, flags, positions, nargs, default factories, or a custom parser.

  • Coerce only string-only sources: sys.argv tokens and os.environ values. Python values are used as Python values.

  • Use the default parsers: auto for scalars, csv for comma lists, and yaml for YAML-shaped values.

  • Build argparse-backed CLIs, modal subcommands, nested config trees, dotted overrides, and YAML/JSON load/dump.

  • Ship with py.typed and zero required runtime dependencies.

Installation

pip install kwconf

# optional extras
pip install kwconf[yaml]    # YAML config load/dump and parser='yaml'
pip install kwconf[ubelt]   # rich repr, Config.__json__, port_to_argparse

Quickstart

Start with plain class attributes. Type annotations are optional.

import kwconf


class DemoConfig(kwconf.Config):
    count = 1
    mode = kwconf.Value('fast', choices=['fast', 'safe'])
    tags = kwconf.Value(default_factory=list, nargs='+')


cfg = DemoConfig.cli(argv=['--count=3', '--mode=safe', '--tags', 'a', 'b'])
assert cfg.count == 3
assert cfg['mode'] == 'safe'
assert cfg.tags == ['a', 'b']

The same class works from Python, files, env, or argv:

cfg = DemoConfig(count=2)
cfg = DemoConfig().load({'count': 2})
cfg = DemoConfig.cli(data={'count': 2}, argv=False)
cfg = DemoConfig.cli(argv='--count=2 --mode=safe')
cfg = DemoConfig.from_env(prefix='DEMO_')
cfg = DemoConfig.from_yaml('demo.yaml')

Parser basics

A parser tells a field how to read a CLI/env string.

import kwconf


class ParserConfig(kwconf.Config):
    scalar = kwconf.Value(None)                         # parser='auto'
    nums = kwconf.Value(default_factory=list, parser='csv')
    payload = kwconf.Value(None, parser='yaml')


cfg = ParserConfig.cli(argv=[
    '--scalar=3',
    '--nums=1,2,3',
    '--payload={enabled: true, size: 4}',
])
assert cfg.scalar == 3
assert cfg.nums == [1, 2, 3]
assert cfg.payload == {'enabled': True, 'size': 4}

auto reads scalar strings such as 3, true, and null. csv splits commas and applies auto to each part. yaml uses yaml.safe_load for lists, dicts, and scalars; install kwconf[yaml] for that parser. See the coercion manual for the detailed parser contract.

Growing a script

kwconf is designed for scripts that start as a dictionary and grow into a CLI with minimal churn.

import kwconf


class MyConfig(kwconf.Config):
    simple_option1 = 1
    simple_option2 = 2


def main(argv=None, **kwargs):
    config = MyConfig.cli(argv=argv, data=kwargs)
    return run_algorithm(config)


def run_algorithm(config):
    # Existing dict-style code can keep using config['simple_option1'].
    ...

Add metadata where the CLI needs it:

class MyConfig(kwconf.Config):
    simple_option1 = kwconf.Value(1, help='first simple option')
    simple_option2 = kwconf.Value(2, help='second simple option')

Typed path

Annotations improve static checks, editor help, parser selection, and runtime validation.

class TrainConfig(kwconf.Config):
    lr: float = 1e-3
    mode: str = kwconf.Value('fast', choices=['fast', 'safe'])
    tags: list[str] = kwconf.Value(default_factory=list, nargs='+')


cfg = TrainConfig.cli(argv=['--lr=0.01', '--tags', 'cat', 'dog'])
assert cfg.lr == 0.01
assert cfg.tags == ['cat', 'dog']

Runnable examples

The checked-in examples live in examples/. Run commands from the repo root:

python examples/01_minimal_config.py --help
python examples/01_minimal_config.py --width=128 --height=96 --method=lanczos --dst=thumb.png --tags demo small --dry-run
python examples/03_config_files.py --config examples/data/report.yaml --limit=3 --format=json
python examples/run_all.py

Use examples/README.md as the map. Each example focuses on one surface: basic configs, CLI flags, files, nested configs, modals, large app structure, and migration helpers.

Scriptconfig migration

Use the migration guide when porting existing code or prompting an LLM that already knows scriptconfig.

  • import scriptconfig as scfg -> import kwconf.

  • scfg.Config / scfg.DataConfig -> kwconf.Config.

  • type= -> parser= for new code.

  • cmdline= -> argv=. Recent scriptconfig already supports argv; older examples often emphasize cmdline.

  • --config / --dump / --dumps are opt-in via special_options=True or __special_options__ = True.

  • Comma-separated CLI strings stay strings. Use nargs='+', parser='csv', or parser='yaml' for structured text input.

See the migration guide for the checklist, footguns, and exact replacements.

Next steps

Read the documentation for the core contract, parser model, nested configs, modal CLIs, and migration notes. The examples/ directory contains runnable scripts for the main patterns.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kwconf-0.10.1.tar.gz (168.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kwconf-0.10.1-py3-none-any.whl (109.2 kB view details)

Uploaded Python 3

File details

Details for the file kwconf-0.10.1.tar.gz.

File metadata

  • Download URL: kwconf-0.10.1.tar.gz
  • Upload date:
  • Size: 168.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for kwconf-0.10.1.tar.gz
Algorithm Hash digest
SHA256 9d1dc620a9e424c69a850d9a0870475e316796d559ded93c45e91f57ec93f5d6
MD5 8b45f1349b70e15955f1a16fcda2894f
BLAKE2b-256 ae73467aa6ef5071e33a6e3db047f8dd70ec2b274f48cc3c3821b365e41d046e

See more details on using hashes here.

Provenance

The following attestation bundles were made for kwconf-0.10.1.tar.gz:

Publisher: release.yml on Erotemic/kwconf

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

File details

Details for the file kwconf-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: kwconf-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 109.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for kwconf-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49bf20000c2ec45f9ec74494ee0e9654dc50ed844d4396b5a21ca8ffcbcd9dc6
MD5 ac5ea751da8c036da7efff8760564642
BLAKE2b-256 ec0afa4ca66a2651a02c45203cef2676c425f572b8f554809ccad650f28ae0d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for kwconf-0.10.1-py3-none-any.whl:

Publisher: release.yml on Erotemic/kwconf

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 Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page