Create modular and flexible configurations in Python

image image Travis Binder License: MIT


Anyfig is a Python library for creating configurations (settings) at runtime. Anyfig utilizes Python classes which empowers the developer to put anything, from strings to custom objects in the config. Hence the name Any(con)fig.

Why Anyfig?

Anyfig was developed for my own machine learning experiments but has since generalized to support other types of Python projects. Since the configs are defined in normal Python code, Anyfig offers freedom and flexibility that isn't possible with other solutions.


  • Code in Python. No reading from .json or .yaml (unless you want to)
  • Avoid duplicated config-parameters with the help of inheritance and modularization
  • Override config-values via command line input
  • Freeze configs for immutability
  • Save / load configs

Basic Example

import anyfig
from pathlib import Path
import time

@anyfig.config_class  # Registers the class with anyfig
class MyConfig:
  def __init__(self):
    # Config-parameters goes as attributes
    self.experiment_note = 'Changed stuff'
    self.save_directory = Path('output')
    self.start_time = time.time()

# Instantiate config object
config = anyfig.init_config(default_config=MyConfig)

# Access config values with the dot notation

Online Demo

Feel free to play with the Online Demo hosted by Binder.


Install Anyfig from pip or the github repo

pip install anyfig
# dataclasses is a backport of the built-in dataclasses in Python 3.7
# More info at
pip install anyfig dataclasses
# From GitHub master branch
pip install git+

Citing Anyfig

Feel free to cite Anyfig in your research:

  author =       {Olof Harrysson},
  title =        {Anyfig - Configuring complex Python applications},
  howpublished = {Github},
  year =         {2020},
  url =          {}

Add Discussion as Guest

Log in