dynaconf3.2.6
Published
The dynamic configurator for your Python Project
pip install dynaconf
Package Downloads
Authors
Project URLs
Requires Python
>=3.8
Dependencies
- redis
; extra == "all"
- ruamel.yaml
; extra == "all"
- configobj
; extra == "all"
- hvac
; extra == "all"
- configobj
; extra == "configobj"
- configobj
; extra == "ini"
- redis
; extra == "redis"
- pytest
; extra == "test"
- pytest-cov
; extra == "test"
- pytest-xdist
; extra == "test"
- pytest-mock
; extra == "test"
- radon
; extra == "test"
- flask
>=0.12; extra == "test"
- django
; extra == "test"
- python-dotenv
; extra == "test"
- toml
; extra == "test"
- redis
; extra == "test"
- hvac
>=1.1.0; extra == "test"
- configobj
; extra == "test"
- toml
; extra == "toml"
- hvac
; extra == "vault"
- ruamel.yaml
; extra == "yaml"
dynaconf - Configuration Management for Python.
Features
- Inspired by the 12-factor application guide
- Settings management (default values, validation, parsing, templating)
- Protection of sensitive information (passwords/tokens)
- Multiple file formats
toml|yaml|json|ini|py
and also customizable loaders. - Full support for environment variables to override existing settings (dotenv support included).
- Optional layered system for multi environments
[default, development, testing, production]
- Built-in support for Hashicorp Vault and Redis as settings and secrets storage.
- Built-in extensions for Django and Flask web frameworks.
- CLI for common operations such as
init, list, write, validate, export
. - full docs on https://dynaconf.com
Install
$ pip install dynaconf
Initialize Dynaconf on project root directory
$ cd path/to/your/project/
$ dynaconf init -f toml
āļø Configuring your Dynaconf environment
------------------------------------------
š The file `config.py` was generated.
šļø settings.toml created to hold your settings.
š .secrets.toml created to hold your secrets.
š the .secrets.* is also included in `.gitignore`
beware to not push your secrets to a public repo.
š Dynaconf is configured! read more on https://dynaconf.com
TIP: You can select
toml|yaml|json|ini|py
ondynaconf init -f <fileformat>
toml is the default and also the most recommended format for configuration.
Dynaconf init creates the following files
.
āāā config.py # This is from where you import your settings object (required)
āāā .secrets.toml # This is to hold sensitive data like passwords and tokens (optional)
āāā settings.toml # This is to hold your application settings (optional)
On the file config.py
Dynaconf init generates the following boilerpate
from dynaconf import Dynaconf
settings = Dynaconf(
envvar_prefix="DYNACONF", # export envvars with `export DYNACONF_FOO=bar`.
settings_files=['settings.yaml', '.secrets.yaml'], # Load files in the given order.
)
TIP: You can create the files yourself instead of using the
init
command as shown above and you can give any name you want instead of the defaultconfig.py
(the file must be in your importable python path) - See more options that you can pass toDynaconf
class initializer on https://dynaconf.com
Using Dynaconf
Put your settings on settings.{toml|yaml|ini|json|py}
username = "admin"
port = 5555
database = {name='mydb', schema='main'}
Put sensitive information on .secrets.{toml|yaml|ini|json|py}
password = "secret123"
IMPORTANT:
dynaconf init
command puts the.secrets.*
in your.gitignore
to avoid it to be exposed on public repos but it is your responsibility to keep it safe in your local environment, also the recommendation for production environments is to use the built-in support for Hashicorp Vault service for password and tokens.
Optionally you can now use environment variables to override values per execution or per environment.
# override `port` from settings.toml file and automatically casts as `int` value.
export DYNACONF_PORT=9900
On your code import the settings
object
from path.to.project.config import settings
# Reading the settings
settings.username == "admin" # dot notation with multi nesting support
settings.PORT == 9900 # case insensitive
settings['password'] == "secret123" # dict like access
settings.get("nonexisting", "default value") # Default values just like a dict
settings.databases.name == "mydb" # Nested key traversing
settings['databases.schema'] == "main" # Nested key traversing
More
- Settings Schema Validation
- Custom Settings Loaders
- Vault Services
- Template substitutions
- etc...
There is a lot more you can do, read the docs: http://dynaconf.com
Contribute
Main discussions happens on Discussions Tab learn more about how to get involved on CONTRIBUTING.md guide
More
If you are looking for something similar to Dynaconf to use in your Rust projects: https://github.com/rubik/hydroconf
And a special thanks to Caneco for the logo.