英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论