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.11.0.tar.gz (172.1 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.11.0-py3-none-any.whl (110.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kwconf-0.11.0.tar.gz
Algorithm Hash digest
SHA256 cd8e9192a3c0e54750368980f9bf6b02676f75b76ac3830b656e977cc55f988f
MD5 56117d705bc2466763d8b03e6709685d
BLAKE2b-256 c67720ce9cf65bc20e769db9a506e72e291f7a38ee766393110858a15e93ea1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for kwconf-0.11.0.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.11.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for kwconf-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4691afdf7173ca0d42302a2cf17910917ac3bd02729e0b2f2cb2aa1e3b2ee18b
MD5 5856abbfcedcfea525ecf2caefa8b2a5
BLAKE2b-256 fa7a4b4a1c503cc621eb2256e5d18385ccfb546f536008cdb4db0189426f5e77

See more details on using hashes here.

Provenance

The following attestation bundles were made for kwconf-0.11.0-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