为您的Python项目提供动态配置器
项目描述
dynaconf - Python配置管理。
特性
- 灵感来源于12因素应用指南
- 设置管理(默认值、验证、解析、模板化)
- 敏感信息保护(密码/令牌)
- 支持多种文件格式
toml|yaml|json|ini|py
,并提供自定义加载器。 - 完全支持环境变量来覆盖现有设置(包括dotenv支持)。
- 可选的多环境分层系统
[默认,开发,测试,生产]
- 内置对HashiCorp Vault和Redis作为设置和秘密存储的支持。
- 内置对Django和Flask Web框架的扩展。
- CLI 用于常见操作,例如
init, list, write, validate, export
。 - 完整文档请访问 https://dynaconf.com
安装
$ pip install dynaconf
在项目根目录初始化 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
提示:您可以在
dynaconf init -f <fileformat>
中选择toml|yaml|json|ini|py
,默认格式为 toml,也是推荐用于配置的格式。
Dynaconf init 会创建以下文件
.
├── 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)
在文件 config.py
中,Dynaconf init 生成以下模板
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.
)
提示:您可以根据需要创建文件,而不是使用上述的
init
命令,可以给文件命名而不是默认的config.py
(文件必须位于您的可导入 Python 路径中) - 更多可传递给Dynaconf
类初始化器的选项,请参阅 https://dynaconf.com
使用 Dynaconf
将设置放在 settings.{toml|yaml|ini|json|py}
username = "admin"
port = 5555
database = {name='mydb', schema='main'}
将敏感信息放在 .secrets.{toml|yaml|ini|json|py}
password = "secret123"
重要:
dynaconf init
命令会将.secrets.*
添加到您的.gitignore
文件中,以避免在公共仓库中公开,但您有责任在本地环境中保持其安全,对于生产环境,建议使用内置的 HashiCorp Vault 服务对密码和令牌进行支持。
可选地,您现在可以使用环境变量来覆盖执行或环境中的值。
# override `port` from settings.toml file and automatically casts as `int` value.
export DYNACONF_PORT=9900
在代码中导入 settings
对象
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
更多
- 设置模式验证
- 自定义设置加载器
- 密钥库服务
- 模板替换
- 等等...
您可以做更多的事情,请阅读文档: http://dynaconf.com
贡献
主要讨论发生在 讨论标签页。有关如何参与的更多信息,请参阅 CONTRIBUTING.md 指南
更多
如果您正在寻找类似于 Dynaconf 的东西来在您的 Rust 项目中使用: https://github.com/rubik/hydroconf
特别感谢 Caneco 为项目提供的标志。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪一个,请了解有关安装包的更多信息。