pip install dynaconf
dynaconf init -f <fileformat>
这里<fileformat> 的文件格式为toml|yaml|json|ini|py,默认为toml
输出文件:
.
├── config.py # 指定配置文件
├── .secrets.toml # Sensitive data like passwords and tokens (optional)
└── settings.toml # Application settings (optional)
from dynaconf import Dynaconf
settings = Dynaconf(
settings_files=['settings.toml', '.secrets.toml'],
)
key = "value"
a_boolean = false
number = 1234
a_float = 56.8
a_list = [1, 2, 3, 4]
a_dict = {hello="world"}
[a_dict.nested]
other_level = "nested value"
password = "s3cr3t"
token = "dfgrfg5d4g56ds4gsdf5g74984we5345-"
message = "This file doesn't go to your pub repo"
from config import settings
assert settings.key == "value"
assert settings.number == 789
assert settings.a_dict.nested.other_level == "nested value"
assert settings['a_boolean'] is False
assert settings.get("DONTEXIST", default=1) == 1
官方网址: https://www.dynaconf.com/