Package: pydantic-config
Pydantic ConfigSupport for Pydantic settings configuration file loadingInstallationPydantic Config can be installed via pip:pip install pydantic-configPydantic Config is also available on conda under the conda-forge channel:conda install pydantic-config -c conda-forgeOptional DependenciesPydantic-Config has the following optional dependencies:yaml -pip install pydantic-config[yaml]toml -pip install pydantic-config[toml]Only for python<3.11You can install all the optional dependencies withpip install pydantic-config[all]Usage# config.tomlapp_name="Python Application"description="Test application description"frompydantic_configimportSettingsModel,SettingsConfigclassSettings(SettingsModel):app_id:str=1app_name:str=Nonedescription:str=Nonelog_level:str='INFO'model_config=SettingsConfig(config_file='config.toml',)settings=Settings()print(settings)# app_id='1' app_name='Python Application' description='Test application description' log_level='INFO'Using multiple config filesMultiple config files can be loaded by passing alistof file names. Files will be loaded in the order they are listed.
Meaning later files in thelistwill take priority over earlier files.# config.tomlapp_name="Python Application"description="Test application description"// config.json{"description":"Description from JSON file","log_level":"WARNING"}frompydantic_configimportSettingsModel,SettingsConfigclassSettings(SettingsModel):app_id:str=1app_name:str='App Name'description:str=Nonelog_level:str='INFO'model_config=SettingsConfig(config_file=['config.toml','config.json']# The config.json file will take priority over config.toml)settings=Settings()print(settings)# app_id='1' app_name='Python Application' description='Description from JSON file' log_level='WARNING'Supported file formatsCurrently, the following file formats are supported:.yamlRequirespyyamlpackage.tomlRequirestomlipackage for python<3.11.json.iniRequiring config files to loadConfig files will attempt to be loaded from the specified file path. By default, if no file is found the file
will simply not be loaded (no error occurs). This may be useful if you want to specify config files that
may or may not exist. For example, you may have different config files for per
environment:config-prod.tomlandconfig-dev.toml.To disable this behavior setconfig_file_required=True. This will cause an error to be raised
if the specified config file(s) do not exist. Setting this toTruewill also prohibit theconfig_fileparameter from being set toNoneor empty[].MergingIf your configurations have existinglistordictvariables the contents will be merged by default. To disable
this behavior and override the contents instead you can set theconfig_mergeoption toFalsein the settingsConfigclass.# config.toml[foo]item1="value1"# config2.toml[foo]item2="value2"frompydantic_configimportSettingsModel,SettingsConfigclassSettings(SettingsModel):foo:dict={}model_config=SettingsConfig(config_file=['config.toml','config2.toml'],config_merge=True,)settings=Settings()print(settings)# foo={'item1': 'value1', 'item2': 'value2'}# If config_merge=False then config2.toml would override the values from config.toml# foo={'item2': 'value2'}Duplicate items in merged listsBy default,alllistitems will be merged into a single list regardless of duplicated items. To only keep
unique list items, setconfig_merge_unique=True. This will only keep unique items in within a list.