draccus
Draccus is a Python library that provides a slightly opinionated framework for simple dataclass-based configurations, built as a fork of Pyrallis. It extends Pyrallis with advanced features such as support for subtyping configurations, the ability to include config files within other config files, and enhanced handling for containers of configurations. The current version is 0.11.5.
Warnings
- gotcha Draccus is a fork of Pyrallis. While it maintains much of Pyrallis's API, it introduces unique features like subtyping and nested config file inclusion. Code expecting direct Pyrallis behavior for these advanced scenarios might need adaptation.
- gotcha When using the `@draccus.wrap()` decorator, the decorated function's parameters are automatically populated by the parsed configuration. Avoid manually calling `draccus.parse()` within a function already wrapped by `@draccus.wrap()` unless a separate parsing context is explicitly intended.
- deprecated Earlier versions might have used `draccus.parse()` as the primary entry point. While still available, `@draccus.wrap()` is generally recommended for top-level application configuration due to its seamless integration.
Install
-
pip install draccus
Imports
- wrap
import draccus @draccus.wrap(...) def main(cfg: Config): ...
- parse
import draccus cfg = draccus.parse(config_class=Config)
Quickstart
from dataclasses import dataclass
import draccus
@dataclass
class TrainConfig:
"""Training Config for Machine Learning"""
workers: int = 8 # The number of workers for training
exp_name: str = 'default_exp' # The experiment name
@draccus.wrap()
def main(cfg: TrainConfig):
print(f"Training {cfg.exp_name} with {cfg.workers} workers...")
if __name__ == "__main__":
main()