Dynaconf
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
- breaking The global `settings` object (`from dynaconf import settings`) is deprecated in version 3.x. You should now explicitly instantiate `Dynaconf()` to manage your settings.
- breaking Automatic loading of settings files (e.g., `settings.toml`, `.secrets.yaml`) is disabled by default in Dynaconf 3.x. Files must now be explicitly passed via the `settings_files` argument during `Dynaconf` instantiation.
- breaking Loading of `.env` files is disabled by default in Dynaconf 3.x.
- gotcha Dynaconf's path resolution relies on the Current Working Directory (CWD). Running tests or applications from an IDE that changes the CWD (e.g., PyCharm setting CWD to `./tests`) can cause `OSError: Starting path not found` as Dynaconf may fail to locate settings files.
- breaking Versions 3.2.8 and 3.2.9 were yanked from PyPI, indicating potential issues or regressions. Users should avoid these specific versions.
- breaking Templating vulnerabilities (`@jinja` and `@format`) were fixed in version 3.2.13. Older versions using these features might be susceptible to security risks. [cite: latest release notes]
Install
-
pip install dynaconf -
pip install dynaconf[toml,yaml,redis,vault]
Imports
- Dynaconf
from dynaconf import Dynaconf
Quickstart
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}")