diff --git a/src/dotenv/main.py b/src/dotenv/main.py index d17b45cb..878db002 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -382,6 +382,7 @@ def _is_debugger(): def load_dotenv( dotenv_path: Optional[StrPath] = None, + init_dotenv_path: Optional[StrPath] = None, stream: Optional[IO[str]] = None, verbose: bool = False, override: bool = False, @@ -392,6 +393,7 @@ def load_dotenv( Parameters: dotenv_path: Absolute or relative path to .env file. + init_dotenv_path: TODO stream: Text stream (such as `io.StringIO`) with .env content, used if `dotenv_path` is `None`. verbose: Whether to output a warning the .env file is missing. @@ -416,6 +418,14 @@ def load_dotenv( ) return False + if should_copy_init(dotenv_path, init_dotenv_path, stream): + copy_dotenv_path = dotenv_path or ".env" + logger.warning("Copying %s to %s", init_dotenv_path, copy_dotenv_path) + import shutil + + assert init_dotenv_path is not None + shutil.copyfile(init_dotenv_path, copy_dotenv_path) + if dotenv_path is None and stream is None: dotenv_path = find_dotenv() @@ -430,6 +440,20 @@ def load_dotenv( return dotenv.set_as_environment_variables() +def should_copy_init( + arg_dotenv_path: Optional[StrPath], + init_dotenv_path: Optional[StrPath], + stream: Optional[IO[str]], +) -> bool: + dotenv_path = arg_dotenv_path or ".env" + return bool( + (stream is None) + and (init_dotenv_path is not None) + and os.path.exists(init_dotenv_path) + and (not os.path.exists(dotenv_path)) + ) + + def dotenv_values( dotenv_path: Optional[StrPath] = None, stream: Optional[IO[str]] = None,