@@ -5,11 +5,14 @@ Configuration provider
5
5
6
6
.. meta ::
7
7
: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
9
11
:description: Configuration provides configuration options to the other providers. This page
10
12
demonstrates how to use Configuration provider to inject the dependencies, load
11
13
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.
13
16
14
17
.. currentmodule :: dependency_injector.providers
15
18
@@ -42,12 +45,7 @@ where ``examples/providers/configuration/config.ini`` is:
42
45
.. literalinclude :: ../../examples/providers/configuration/config.ini
43
46
:language: ini
44
47
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.
51
49
52
50
.. code-block :: ini
53
51
@@ -56,6 +54,8 @@ variable ``ENV_NAME`` is undefined, configuration provider will substitute value
56
54
option2 = {$ENV_VAR}/path
57
55
option3 = {$ENV_VAR:default}
58
56
57
+ See also: :ref: `configuration-envs-interpolation `.
58
+
59
59
Loading from a YAML file
60
60
------------------------
61
61
@@ -72,12 +72,7 @@ where ``examples/providers/configuration/config.yml`` is:
72
72
.. literalinclude :: ../../examples/providers/configuration/config.yml
73
73
:language: ini
74
74
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.
81
76
82
77
.. code-block :: ini
83
78
@@ -86,6 +81,8 @@ variable ``ENV_NAME`` is undefined, configuration provider will substitute value
86
81
option2: {$ENV_VAR}/path
87
82
option3: {$ENV_VAR:default}
88
83
84
+ See also: :ref: `configuration-envs-interpolation `.
85
+
89
86
:py:meth: `Configuration.from_yaml ` method uses custom version of ``yaml.SafeLoader ``.
90
87
To use another loader use ``loader `` argument:
91
88
@@ -191,6 +188,73 @@ where ``examples/providers/configuration/config.local.yml`` is:
191
188
.. literalinclude :: ../../examples/providers/configuration/config.local.yml
192
189
:language: ini
193
190
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
+
194
258
Mandatory and optional sources
195
259
------------------------------
196
260
@@ -310,6 +374,21 @@ configuration data is undefined:
310
374
except ValueError :
311
375
...
312
376
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
+
313
392
You can override ``.from_*() `` methods behaviour in strict mode using ``required `` argument:
314
393
315
394
.. code-block :: python
0 commit comments