Simple Settings

1.2.0 · active · verified Thu Apr 16

Simple Settings is a Python library designed to provide a flexible and straightforward way to manage project configurations, inspired by Django's settings system. It allows loading settings from various file formats (Python modules, CFG, YAML, JSON, TOML) and supports dynamic settings backends like AWS S3, Memcached, and SQL databases. The current version is 1.2.0, and it maintains an active, though irregular, release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Python settings file and load it using `LazySettings`. In a typical application, the settings module would be specified via a `--settings` command-line argument (e.g., `python your_app.py --settings=my_project_settings.development`) or the `SIMPLE_SETTINGS` environment variable, after which the global `simple_settings.settings` object becomes populated. Using `LazySettings` allows for explicit, programmatic control over which settings module to load.

import os
import sys
from simple_settings import settings

# --- Create a dummy settings file for demonstration ---
settings_content = """
FOO = 'bar'
BAZ = 123
SIMPLE_SETTINGS = {'required_settings': ['FOO']}
"""

settings_dir = 'my_project_settings'
os.makedirs(settings_dir, exist_ok=True)
with open(os.path.join(settings_dir, 'development.py'), 'w') as f:
    f.write(settings_content)

# Add the settings directory to Python's path so it can be imported
sys.path.insert(0, os.getcwd())

# Method 1: Set environment variable (deprecated but common in older setups)
# os.environ['SIMPLE_SETTINGS'] = 'my_project_settings.development'

# Method 2: Command line argument (preferred for non-programmatic loading)
# For programmatic loading, you'd typically use LazySettings or a custom loader
# For this quickstart, we'll demonstrate direct access after 'setting' the module path implicitly.
# In a real app, you would run like: python your_script.py --settings=my_project_settings.development

# As a workaround for a runnable quickstart, we'll manually set the internal _SETTINGS_MODULE
# This is NOT how you'd normally configure it in production for the 'settings' singleton
# Normally, the --settings CLI arg or SIMPLE_SETTINGS env var would handle this.
# Using LazySettings is the programmatic alternative.

# For this demonstration, we'll mimic the effect of `settings.configure()`
# if the env var or CLI was set.
# In a real application, you'd run `python your_app.py --settings=my_project_settings.development`
# and then `from simple_settings import settings` would automatically pick it up.
# To make this runnable without CLI args or env vars, we use LazySettings.

try:
    # Using LazySettings for a self-contained programmatic example
    app_settings = LazySettings('my_project_settings.development')
    print(f"FOO setting: {app_settings.FOO}")
    print(f"BAZ setting: {app_settings.BAZ}")

    # Demonstrate a missing required setting (this would raise an error)
    # with open(os.path.join(settings_dir, 'broken_settings.py'), 'w') as f:
    #     f.write("BAZ = 456")
    # broken_app_settings = LazySettings('my_project_settings.broken_settings')
    # print(f"Broken FOO: {broken_app_settings.FOO}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy settings file and directory
    os.remove(os.path.join(settings_dir, 'development.py'))
    # if os.path.exists(os.path.join(settings_dir, 'broken_settings.py')):
    #     os.remove(os.path.join(settings_dir, 'broken_settings.py'))
    os.rmdir(settings_dir)
    sys.path.pop(0)

view raw JSON →