Dynaconf

3.2.13 · active · verified Thu Apr 09

Dynaconf is a dynamic configuration management library for Python projects, inspired by the 12-factor application guide. It supports multiple file formats (TOML, YAML, JSON, INI, Python), environment variables, Hashicorp Vault, and Redis for settings and secrets. It provides a flexible, layered system for multi-environment configurations and includes built-in extensions for Flask and Django. The library is actively maintained, with the current version being 3.2.13, and receives regular updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Dynaconf, load settings from specified TOML files (including a secrets file), and access configuration values. It highlights the importance of explicitly enabling `load_dotenv` and `environments` in version 3.x and shows how to access values, including sensitive ones, with a fallback to environment variables for production readiness.

from dynaconf import Dynaconf

# Create settings files (e.g., settings.toml and .secrets.toml)
# settings.toml:
# [default]
# FOO = 'bar'
# DATABASE_URL = 'sqlite:///mydb.sqlite'
#
# .secrets.toml:
# [default]
# API_KEY = 'secret-key-123'

settings = Dynaconf(
    envvar_prefix="DYNACONF",
    settings_files=['settings.toml', '.secrets.toml'],
    environments=True, # Enable layered environments
    load_dotenv=True # Explicitly enable .env file loading (disabled by default in 3.x)
)

# Access settings
print(f"Foo: {settings.FOO}")
print(f"Database URL: {settings.DATABASE_URL}")

# Access secret
# For production, consider using environment variables or a secure vault.
# Example: export DYNACONF_API_KEY="my_prod_api_key"
api_key = settings.get('API_KEY', os.environ.get('DYNACONF_API_KEY', 'default-api-key'))
print(f"API Key: {api_key}")

# Switch environment (e.g., in a development environment)
# export ENV_FOR_DYNACONF=development

# Example of setting a value programmatically (not persisted to file)
settings.set('NEW_SETTING', 'a new value')
print(f"New setting: {settings.NEW_SETTING}")

view raw JSON →