@@ -39,6 +39,7 @@ def __init__(
3939 encoding : Union [None , str ] = None ,
4040 interpolate : bool = True ,
4141 override : bool = True ,
42+ base_env : Mapping [str , Optional [str ]] = os .environ
4243 ) -> None :
4344 self .dotenv_path = dotenv_path # type: Optional[Union[str, _PathLike]]
4445 self .stream = stream # type: Optional[IO[str]]
@@ -47,6 +48,7 @@ def __init__(
4748 self .encoding = encoding # type: Union[None, str]
4849 self .interpolate = interpolate # type: bool
4950 self .override = override # type: bool
51+ self .base_env = base_env # type: Mapping[str, Optional[str]]
5052
5153 @contextmanager
5254 def _get_stream (self ) -> Iterator [IO [str ]]:
@@ -71,7 +73,9 @@ def dict(self) -> Dict[str, Optional[str]]:
7173 raw_values = self .parse ()
7274
7375 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+ )
7579 else :
7680 self ._dict = OrderedDict (raw_values )
7781
@@ -212,6 +216,7 @@ def unset_key(
212216def resolve_variables (
213217 values : Iterable [Tuple [str , Optional [str ]]],
214218 override : bool ,
219+ base_env : Mapping [str , Optional [str ]] = os .environ ,
215220) -> Mapping [str , Optional [str ]]:
216221 new_values = {} # type: Dict[str, Optional[str]]
217222
@@ -222,11 +227,11 @@ def resolve_variables(
222227 atoms = parse_variables (value )
223228 env = {} # type: Dict[str, Optional[str]]
224229 if override :
225- env .update (os . environ ) # type: ignore
230+ env .update (base_env ) # type: ignore
226231 env .update (new_values )
227232 else :
228233 env .update (new_values )
229- env .update (os . environ ) # type: ignore
234+ env .update (base_env ) # type: ignore
230235 result = "" .join (atom .resolve (env ) for atom in atoms )
231236
232237 new_values [name ] = result
@@ -334,6 +339,7 @@ def dotenv_values(
334339 verbose : bool = False ,
335340 interpolate : bool = True ,
336341 encoding : Optional [str ] = "utf-8" ,
342+ base_env : Mapping [str , Optional [str ]] = os .environ ,
337343) -> Dict [str , Optional [str ]]:
338344 """
339345 Parse a .env file and return its content as a dict.
@@ -342,8 +348,8 @@ def dotenv_values(
342348 - *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
343349 - *verbose*: whether to output a warning the .env file is missing. Defaults to
344350 `False`.
345- in `.env` file. Defaults to `False`.
346351 - *encoding*: encoding to be used to read the file.
352+ - *base_env*: dict with initial environment. Defaults to os.environ
347353
348354 If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
349355 """
@@ -357,4 +363,5 @@ def dotenv_values(
357363 interpolate = interpolate ,
358364 override = True ,
359365 encoding = encoding ,
366+ base_env = base_env ,
360367 ).dict ()
0 commit comments