Python Box

7.4.1 · active · verified Thu Apr 09

python-box is an active library that provides an advanced Python dictionary with dot notation access, allowing dictionary keys to be accessed as attributes. It simplifies working with deeply nested data structures and offers various serialization methods (JSON, YAML, TOML, JSON5, TOON) and other utility classes. The current version is 7.4.1, and it maintains a regular release cadence with updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `Box` object from a standard Python dictionary and access its elements using convenient dot notation. It also shows how to modify and add new attributes, and highlights the behavior when accessing non-existent keys (raising an AttributeError by default).

from box import Box

# Create a Box from a dictionary
config = Box({
    'application': {
        'name': 'MyAwesomeApp',
        'version': '1.0.0',
        'settings': {
            'debug_mode': True,
            'log_level': 'INFO'
        }
    },
    'database': {
        'host': 'localhost',
        'port': 5432
    }
})

# Access items using dot notation
print(f"App Name: {config.application.name}")
print(f"Debug Mode: {config.application.settings.debug_mode}")

# Modify items
config.database.port = 5433
print(f"New DB Port: {config.database.port}")

# Add new items
config.application.author = 'AI Agent'
print(f"App Author: {config.application.author}")

# Example with missing key (will raise AttributeError without default_box_create_on_get)
try:
    print(config.non_existent_key)
except AttributeError as e:
    print(f"Caught expected error: {e}")

view raw JSON →