dynaconf

Delphi源码 2025-08-16

dynaconf Python的配置管理。

特征

  • 受12因子申请指南的启发
  • 设置管理(默认值,验证,解析,模板)
  • 保护敏感信息(密码/令牌)
  • 多个文件格式toml|yaml|json|ini|py以及可自定义的加载程序。
  • 对环境变量的全面支持以覆盖现有设置(包括DOTENV支持)。
  • 多环境的可选分层系统[default, development, testing, production]
  • 内置支持Hashicorp保管库和Redis作为设置和秘密存储。
  • DjangoFlask Web框架的内置扩展。
  • CLI用于常见操作,例如init, list, write, validate, export
  • https://*dyn*a*conf.com上的完整文档

安装

$ pip install dynaconf 

在项目根目录上初始化dynaconf

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://dy*n*aconf*.com">
 $ 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://dy*n*aconf*.com

提示:您可以选择toml|yaml|json|ini|py on dynaconf init -f <fileformat> 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上生成以下锅炉

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. )">
 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 class initializer in https://dy*n*aconf*.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 "

.gitignore的是: dynaconf init命令将.secrets.*

您现在可以选择使用环境变量来覆盖每个执行或每个环境的值。

 # 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 

更多的

  • 设置模式验证
  • 自定义设置加载程序
  • 保险库服务
  • 模板替换
  • ETC...

您可以做更多的事情,请阅读文档: http://dynaconf.com

贡献

主要讨论在讨论选项卡上进行了更多有关如何参与贡献的信息。MD指南

更多的

如果您正在寻找类似于dynaconf在生锈项目中使用的东西:https://github.com/rubik/hydroconf

特别感谢Caneco的徽标。

下载源码

通过命令行克隆项目:

git clone https://github.com/dynaconf/dynaconf.git