@@ -5,11 +5,14 @@ Configuration provider
55
66.. meta ::
77 :keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection,
8- Option,Ini,Json,Yaml,Pydantic,Dict,Environment Variable,Default,Load,Read,Get
8+ Option,Ini,Json,Yaml,Pydantic,Dict,Environment Variable Interpolation,
9+ Environment Variable Substitution,Environment Variable in Config,
10+ Environment Variable in YAML file,Environment Variable in INI file,Default,Load,Read
911 :description: Configuration provides configuration options to the other providers. This page
1012 demonstrates how to use Configuration provider to inject the dependencies, load
1113 a configuration from an ini or yaml file, a dictionary, an environment variable,
12- or a pydantic settings object.
14+ or a pydantic settings object. This page also describes how to substitute (interpolate)
15+ environment variables in YAML and INI configuration files.
1316
1417.. currentmodule :: dependency_injector.providers
1518
@@ -42,12 +45,7 @@ where ``examples/providers/configuration/config.ini`` is:
4245.. literalinclude :: ../../examples/providers/configuration/config.ini
4346 :language: ini
4447
45- :py:meth: `Configuration.from_ini ` method supports environment variables interpolation. Use
46- ``${ENV_NAME} `` format in the configuration file to substitute value from ``ENV_NAME `` environment
47- variable.
48-
49- You can also specify a default value using ``${ENV_NAME:default} `` format. If environment
50- variable ``ENV_NAME `` is undefined, configuration provider will substitute value ``default ``.
48+ :py:meth: `Configuration.from_ini ` method supports environment variables interpolation.
5149
5250.. code-block :: ini
5351
@@ -56,6 +54,8 @@ variable ``ENV_NAME`` is undefined, configuration provider will substitute value
5654 option2 = {$ENV_VAR}/path
5755 option3 = {$ENV_VAR:default}
5856
57+ See also: :ref: `configuration-envs-interpolation `.
58+
5959Loading from a YAML file
6060------------------------
6161
@@ -72,12 +72,7 @@ where ``examples/providers/configuration/config.yml`` is:
7272.. literalinclude :: ../../examples/providers/configuration/config.yml
7373 :language: ini
7474
75- :py:meth: `Configuration.from_yaml ` method supports environment variables interpolation. Use
76- ``${ENV_NAME} `` format in the configuration file to substitute value from ``ENV_NAME `` environment
77- variable.
78-
79- You can also specify a default value using ``${ENV_NAME:default} `` format. If environment
80- variable ``ENV_NAME `` is undefined, configuration provider will substitute value ``default ``.
75+ :py:meth: `Configuration.from_yaml ` method supports environment variables interpolation.
8176
8277.. code-block :: ini
8378
@@ -86,6 +81,8 @@ variable ``ENV_NAME`` is undefined, configuration provider will substitute value
8681 option2: {$ENV_VAR}/path
8782 option3: {$ENV_VAR:default}
8883
84+ See also: :ref: `configuration-envs-interpolation `.
85+
8986:py:meth: `Configuration.from_yaml ` method uses custom version of ``yaml.SafeLoader ``.
9087To use another loader use ``loader `` argument:
9188
@@ -191,6 +188,73 @@ where ``examples/providers/configuration/config.local.yml`` is:
191188.. literalinclude :: ../../examples/providers/configuration/config.local.yml
192189 :language: ini
193190
191+ .. _configuration-envs-interpolation :
192+
193+ Using environment variables in configuration files
194+ --------------------------------------------------
195+
196+ ``Configuration `` provider supports environment variables interpolation in configuration files.
197+ Use ``${ENV_NAME} `` in the configuration file to substitute value from environment
198+ variable ``ENV_NAME ``.
199+
200+ .. code-block :: ini
201+
202+ section:
203+ option: ${ENV_NAME}
204+
205+ You can also specify a default value using ``${ENV_NAME:default} `` format. If environment
206+ variable ``ENV_NAME `` is undefined, configuration provider will substitute value ``default ``.
207+
208+ .. code-block :: ini
209+
210+ [section]
211+ option = {$ENV_NAME:default}
212+
213+ If you'd like to specify a default value for environment variable inside of the application you can use
214+ ``os.environ.setdefault() ``.
215+
216+ .. literalinclude :: ../../examples/providers/configuration/configuration_env_interpolation_os_default.py
217+ :language: python
218+ :lines: 3-
219+ :emphasize-lines: 12
220+
221+ If environment variable is undefined and doesn't have a default, ``Configuration `` provider
222+ will replace it with an empty value. This is a default behavior. To raise an error on
223+ undefined environment variable that doesn't have a default value, pass argument
224+ ``envs_required=True `` to a configuration reading method:
225+
226+ .. code-block :: python
227+
228+ container.config.from_yaml(' config.yml' , envs_required = True )
229+
230+ See also: :ref: `configuration-strict-mode `.
231+
232+ .. note ::
233+ ``Configuration `` provider makes environment variables interpolation before parsing. This preserves
234+ original parser behavior. For instance, undefined environment variable in YAML configuration file
235+ will be replaced with an empty value and then YAML parser will load the file.
236+
237+ Original configuration file:
238+
239+ .. code-block :: ini
240+
241+ section:
242+ option: ${ENV_NAME}
243+
244+ Configuration file after interpolation where ``ENV_NAME `` is undefined:
245+
246+ .. code-block :: ini
247+
248+ section:
249+ option:
250+
251+ Configuration provider after parsing interpolated YAML file contains ``None `` in
252+ option ``section.option ``:
253+
254+ .. code-block :: python
255+
256+ assert container.config.section.option() is None
257+
194258 Mandatory and optional sources
195259------------------------------
196260
@@ -310,6 +374,21 @@ configuration data is undefined:
310374 except ValueError :
311375 ...
312376
377+ Environment variables interpolation in strict mode raises an exception when encounters
378+ an undefined environment variable without a default value.
379+
380+ .. code-block :: ini
381+
382+ section:
383+ option: {$UNDEFINED}
384+
385+ .. code-block :: python
386+
387+ try :
388+ container.config.from_yaml(' undefined_env.yml' ) # raise exception
389+ except ValueError :
390+ ...
391+
313392 You can override ``.from_*() `` methods behaviour in strict mode using ``required `` argument:
314393
315394.. code-block :: python
0 commit comments