Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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()

Expand All @@ -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,
Expand Down
Loading