Python的dynaconf未正确合并多个文件的设置。

huangapple go评论89阅读模式
英文:

Python's dynaconf does not merge multi-file settings correctly

问题

我使用dynaconf来管理我的程序设置。但我经常需要在调试和常规运行配置之间切换。

default.yaml 文件中,我有以下设置:

SETTINGS:
   RUN_WITH_SEED: false
   SEED: 42

   COLORS: ['red', 'white']

而在我的 debug.yaml 文件中,只有以下设置:

SETTINGS:
   RUN_WITH_SEED: true

然而,当我在默认配置后加载调试配置时,它也会删除未更改的设置。我真的想避免重新编写所有设置,因为大多数设置都没有更改,而且有相当多的设置。

这是我加载它们的方式:


config_files = ['config/default.yaml', 'config/.secrets.yaml', 'config/debug.yaml']

config = Dynaconf(
        envvar_prefix="DYNACONF",
        settings_files=config_files,
)

print(config.SETTINGS.COLORS)

输出:

"DynaBox'对象没有属性'colors'"
英文:

I am using dynaconf to manage my programs' settings. But i often need to switch between debugging and regular run profile.

In default.yaml I have:

SETTINGS:
   RUN_WITH_SEED: false
   SEED: 42

   COLORS: ['red', 'white']

And in my debug.yaml I have only:

SETTINGS:
   RUN_WITH_SEED: true

However, when I load the debug after the default it also removes the untouched settings. I really want to avoid writing all settings again because most are unchanged and there are quite a few.

This is how I load them:


config_files = ['config/default.yaml', 'config/.secrets.yaml', 'config/debug.yaml']


config = Dynaconf(
        envvar_prefix="DYNACONF",
        settings_files=config_files,
)

print(config.SETTINGS.COLORS)

Yields:

"'DynaBox' object has no attribute 'colors'"

答案1

得分: 1

mkashwin 帮助了我,有两个选项,我更喜欢其中一个:

config = Dynaconf(
envvar_prefix="DYNACONF",
settings_files=config_files,
merge_enabled=True
)

第二种方法是,在 YAML 的末尾添加 DYNACONF_MERGE: true,可以获得相同的行为。

英文:

mkashwin helped me and there are 2 options, one which i prefer:

config = Dynaconf(
envvar_prefix="DYNACONF",
settings_files=config_files,
merge_enabled=True
)

And second, by adding DYNACONF_MERGE: true to the end on the YAML we get the same behavior.

huangapple
  • 本文由 发表于 2023年2月8日 22:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定