dynaconf - Configuration Management for Python.
toml|yaml|json|ini|py
and also customizable loaders.[default, development, testing, production]
init, list, write, validate, export
.DEMO: You can see a working demo here: https://github.com/rochacbruno/learndynaconf
$ pip install dynaconf
$ 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.
.
├── 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 setttings (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
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
There is a lot more you can do, read the docs: http://dynaconf.com
Main discussions happens on t.me/dynaconf learn more about how to get involved on CONTRIBUTING.md guide
If you are looking for something similar to Dynaconf to use in your Rust projects: https://github.com/rubik/hydroconf
【前言】 在项目中经常会遇到以下几种需要用到配置文件的场景: 相同的配置参数用在不同的代码中,如果需要调整,则需要手动将各个使用到的地方都相应调整。 密码等信息不想硬编码在项目文件中。 配置文件的格式有很多种,例如:json,ini,python模块,xml,yaml等也有各种各样的类库去处理配置文件,今天我们推荐一款好用的配置文件库 Dynaconf 。 【Dynaconf 简介】 官网:htt
1. 初始化 dynaconf init -f <fileformat> 命令可以初始化dynaconf,它支持toml|yaml|json|ini|py几种文件类型,toml也是默认的文件格式,他会自动创建: ├── config.py # Where you import your settings object (required) ├── .secrets.toml # S