@@ -39,6 +39,7 @@ def __init__(
39
39
encoding : Union [None , str ] = None ,
40
40
interpolate : bool = True ,
41
41
override : bool = True ,
42
+ base_env : Mapping [str , Optional [str ]] = os .environ
42
43
) -> None :
43
44
self .dotenv_path = dotenv_path # type: Optional[Union[str, _PathLike]]
44
45
self .stream = stream # type: Optional[IO[str]]
@@ -47,6 +48,7 @@ def __init__(
47
48
self .encoding = encoding # type: Union[None, str]
48
49
self .interpolate = interpolate # type: bool
49
50
self .override = override # type: bool
51
+ self .base_env = base_env # type: Mapping[str, Optional[str]]
50
52
51
53
@contextmanager
52
54
def _get_stream (self ) -> Iterator [IO [str ]]:
@@ -71,7 +73,9 @@ def dict(self) -> Dict[str, Optional[str]]:
71
73
raw_values = self .parse ()
72
74
73
75
if self .interpolate :
74
- self ._dict = OrderedDict (resolve_variables (raw_values , override = self .override ))
76
+ self ._dict = OrderedDict (
77
+ resolve_variables (raw_values , override = self .override , base_env = self .base_env )
78
+ )
75
79
else :
76
80
self ._dict = OrderedDict (raw_values )
77
81
@@ -212,6 +216,7 @@ def unset_key(
212
216
def resolve_variables (
213
217
values : Iterable [Tuple [str , Optional [str ]]],
214
218
override : bool ,
219
+ base_env : Mapping [str , Optional [str ]] = os .environ ,
215
220
) -> Mapping [str , Optional [str ]]:
216
221
new_values = {} # type: Dict[str, Optional[str]]
217
222
@@ -222,11 +227,11 @@ def resolve_variables(
222
227
atoms = parse_variables (value )
223
228
env = {} # type: Dict[str, Optional[str]]
224
229
if override :
225
- env .update (os . environ ) # type: ignore
230
+ env .update (base_env ) # type: ignore
226
231
env .update (new_values )
227
232
else :
228
233
env .update (new_values )
229
- env .update (os . environ ) # type: ignore
234
+ env .update (base_env ) # type: ignore
230
235
result = "" .join (atom .resolve (env ) for atom in atoms )
231
236
232
237
new_values [name ] = result
@@ -334,6 +339,7 @@ def dotenv_values(
334
339
verbose : bool = False ,
335
340
interpolate : bool = True ,
336
341
encoding : Optional [str ] = "utf-8" ,
342
+ base_env : Mapping [str , Optional [str ]] = os .environ ,
337
343
) -> Dict [str , Optional [str ]]:
338
344
"""
339
345
Parse a .env file and return its content as a dict.
@@ -342,8 +348,8 @@ def dotenv_values(
342
348
- *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
343
349
- *verbose*: whether to output a warning the .env file is missing. Defaults to
344
350
`False`.
345
- in `.env` file. Defaults to `False`.
346
351
- *encoding*: encoding to be used to read the file.
352
+ - *base_env*: dict with initial environment. Defaults to os.environ
347
353
348
354
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
349
355
"""
@@ -357,4 +363,5 @@ def dotenv_values(
357
363
interpolate = interpolate ,
358
364
override = True ,
359
365
encoding = encoding ,
366
+ base_env = base_env ,
360
367
).dict ()
0 commit comments