Top Related Projects
Canonical source repository for PyYAML
Tom's Obvious, Minimal Language
Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
Schema validation just got Pythonic
Hydra is a framework for elegantly configuring complex applications
Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Quick Overview
OmegaConf is a YAML-based hierarchical configuration system for Python applications. It provides a flexible way to manage configuration settings, supporting features like variable interpolation, type preservation, and merging of multiple configuration sources.
Pros
- Supports both YAML and Python dict configurations
- Offers powerful features like config merging, variable interpolation, and type preservation
- Integrates well with popular Python libraries and frameworks
- Provides a clean API for accessing and modifying configuration values
Cons
- Learning curve for advanced features
- May be overkill for simple configuration needs
- Limited support for complex data structures beyond nested dictionaries and lists
- Some users report occasional inconsistencies in behavior across different versions
Code Examples
- Creating and accessing a configuration:
from omegaconf import OmegaConf
cfg = OmegaConf.create({
"database": {
"host": "localhost",
"port": 5432
}
})
print(cfg.database.host) # Output: localhost
print(cfg.database.port) # Output: 5432
- Merging configurations:
from omegaconf import OmegaConf
base_cfg = OmegaConf.create({"a": 1, "b": 2})
override_cfg = OmegaConf.create({"b": 3, "c": 4})
merged_cfg = OmegaConf.merge(base_cfg, override_cfg)
print(merged_cfg) # Output: {"a": 1, "b": 3, "c": 4}
- Using variable interpolation:
from omegaconf import OmegaConf
cfg = OmegaConf.create({
"dir": "/home/user",
"path": "${dir}/config.yaml"
})
print(cfg.path) # Output: /home/user/config.yaml
Getting Started
To get started with OmegaConf, first install it using pip:
pip install omegaconf
Then, you can create and use a configuration in your Python code:
from omegaconf import OmegaConf
# Create a config
cfg = OmegaConf.create({
"app_name": "MyApp",
"debug": True,
"database": {
"host": "localhost",
"port": 5432
}
})
# Access config values
print(f"App Name: {cfg.app_name}")
print(f"Debug Mode: {cfg.debug}")
print(f"Database Host: {cfg.database.host}")
# Modify config values
cfg.debug = False
cfg.database.port = 5433
# Save config to file
OmegaConf.save(cfg, "config.yaml")
This example demonstrates creating a configuration, accessing and modifying values, and saving the configuration to a file.
Competitor Comparisons
Canonical source repository for PyYAML
Pros of PyYAML
- Widely adopted and well-established in the Python ecosystem
- Supports all YAML 1.1 features, including complex data types
- Can serialize Python objects to YAML and vice versa
Cons of PyYAML
- Lacks built-in support for advanced configuration features like variable interpolation
- No native handling of environment variables or command-line overrides
- Limited type validation and conversion capabilities
Code Comparison
PyYAML:
import yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
print(config['database']['host'])
OmegaConf:
from omegaconf import OmegaConf
config = OmegaConf.load('config.yaml')
print(config.database.host)
OmegaConf provides a more intuitive dot notation access to nested configurations, while PyYAML requires dictionary-style access. OmegaConf also offers additional features like type safety, config merging, and variable interpolation, which are not natively supported in PyYAML. However, PyYAML is more flexible in handling various YAML structures and has broader compatibility with different YAML versions.
Tom's Obvious, Minimal Language
Pros of TOML
- Simple and human-readable syntax, easier for non-technical users
- Widely adopted standard with implementations in many programming languages
- Supports more data types out of the box (e.g., dates, times)
Cons of TOML
- Less flexible for complex configurations
- Limited support for dynamic or programmatic configuration
- No built-in support for environment variable substitution
Code Comparison
TOML:
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
OmegaConf:
database:
server: 192.168.1.1
ports: [8001, 8001, 8002]
connection_max: 5000
Summary
TOML is a straightforward configuration format that's easy to read and write, making it suitable for simple configurations and non-technical users. It has broad language support and built-in data types. However, it lacks some advanced features that OmegaConf provides, such as dynamic configuration and environment variable substitution. OmegaConf, while using YAML syntax, offers more flexibility and programmatic options for complex configurations, making it more suitable for advanced use cases and developers who need more control over their configuration setup.
Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
Pros of python-dotenv
- Simple and straightforward, focusing solely on environment variable management
- Lightweight with minimal dependencies
- Supports multiple file formats (.env, .ini, .yaml)
Cons of python-dotenv
- Limited functionality compared to OmegaConf's hierarchical configuration
- Lacks advanced features like variable interpolation and merging of multiple configs
- No built-in support for type validation or conversion
Code Comparison
python-dotenv:
from dotenv import load_dotenv
load_dotenv()
import os
database_url = os.getenv("DATABASE_URL")
OmegaConf:
from omegaconf import OmegaConf
conf = OmegaConf.load('config.yaml')
database_url = conf.database.url
Summary
python-dotenv is a lightweight solution for managing environment variables, ideal for simple projects. OmegaConf offers more advanced configuration management with hierarchical structures and additional features. Choose python-dotenv for straightforward environment variable handling, and OmegaConf for more complex configuration needs in larger projects.
Schema validation just got Pythonic
Pros of Schema
- More flexible validation rules, allowing for complex data structures
- Supports custom validation functions for advanced use cases
- Provides clear error messages for validation failures
Cons of Schema
- Steeper learning curve due to more complex API
- Less integrated with configuration management workflows
- Requires more boilerplate code for basic use cases
Code Comparison
Schema:
from schema import Schema, And, Use, Optional
schema = Schema({
'name': And(str, len),
'age': And(Use(int), lambda n: 18 <= n <= 99),
Optional('gender'): And(str, lambda s: s in ('male', 'female'))
})
OmegaConf:
from omegaconf import OmegaConf
conf = OmegaConf.create({
'name': 'John',
'age': 30,
'gender': 'male'
})
Summary
Schema focuses on robust data validation with flexible rules, while OmegaConf emphasizes configuration management with a simpler API. Schema offers more control over validation but requires more setup, whereas OmegaConf provides an easier-to-use solution for managing configuration data with built-in features like variable interpolation and merging of multiple config sources.
Hydra is a framework for elegantly configuring complex applications
Pros of Hydra
- More comprehensive configuration framework with advanced features like command-line overrides and multi-run support
- Built-in plugin system for extending functionality
- Seamless integration with popular ML libraries and frameworks
Cons of Hydra
- Steeper learning curve due to more complex architecture
- Potentially overkill for simpler projects that don't require advanced configuration management
- Larger codebase and dependencies compared to OmegaConf
Code Comparison
OmegaConf:
from omegaconf import OmegaConf
config = OmegaConf.load('config.yaml')
print(config.database.host)
Hydra:
import hydra
@hydra.main(config_path="conf", config_name="config")
def my_app(cfg):
print(cfg.database.host)
if __name__ == "__main__":
my_app()
Both OmegaConf and Hydra provide powerful configuration management capabilities for Python projects. OmegaConf offers a simpler, more lightweight solution for basic configuration needs, while Hydra builds upon OmegaConf to provide a more feature-rich framework for complex configuration scenarios. Hydra's additional features come at the cost of increased complexity and a larger footprint, making it more suitable for larger projects with advanced configuration requirements.
Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Pros of Fire
- Simplicity: Fire automatically generates CLIs from Python objects, requiring minimal code
- Flexibility: Supports various Python types and can create CLIs from functions, classes, or modules
- Automatic help generation: Provides built-in help and usage information
Cons of Fire
- Limited configuration options: Less control over CLI structure and behavior
- Potential for unintended exposure: May expose internal methods or attributes not meant for CLI use
- Performance overhead: Dynamic introspection can impact performance for large-scale applications
Code Comparison
Fire:
import fire
def greet(name="World"):
return f"Hello {name}!"
if __name__ == '__main__':
fire.Fire(greet)
OmegaConf:
from omegaconf import OmegaConf
config = OmegaConf.create({
"name": "World"
})
def greet(cfg):
return f"Hello {cfg.name}!"
print(greet(config))
Fire focuses on automatic CLI generation, while OmegaConf emphasizes configuration management. Fire requires less boilerplate for simple CLIs, but OmegaConf offers more structured configuration handling, especially for complex applications.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
OmegaConf
Description | |
---|---|
Project | |
Code quality | |
Docs and support |
OmegaConf is a hierarchical configuration system, with support for merging configurations from multiple sources (YAML config files, dataclasses/objects and CLI arguments) providing a consistent API regardless of how the configuration was created.
Releases
Stable (2.3)
OmegaConf 2.3 is the current stable version.
Install with pip install --upgrade omegaconf
Previous release (2.2)
Install with pip install omegaconf==2.1
Previous release (2.1)
Install with pip install omegaconf==2.1
Previous release (2.0)
Install with pip install omegaconf==2.0.6
Live tutorial
Top Related Projects
Canonical source repository for PyYAML
Tom's Obvious, Minimal Language
Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
Schema validation just got Pythonic
Hydra is a framework for elegantly configuring complex applications
Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot