Repository navigation
Add start_dir argument to find_dotenv - #752
Open
GloballyOptimized wants to merge 1 commit into
Open
GloballyOptimized wants to merge 1 commit into
GloballyOptimized wants to merge 1 commit into
Conversation
find_dotenv() either searches from the current directory (usecwd=True or an interactive session) or infers the caller's file by walking the stack. There was no way to say where the search should start, short of changing the working directory. `start_dir` sets the starting point explicitly: a directory, or a file whose directory is used (e.g. `find_dotenv(start_dir=__file__)`). When it is given, the stack is not inspected at all, so the search no longer depends on how the code was invoked. `~` is expanded, and a start path that does not exist raises the same IOError as before. Closes theskumar#494 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #494
Adds an optional
start_dirargument tofind_dotenvto choose where the upward search starts:On the earlier question in #494 ("does
load_dotenv('/path/to/.env')not solve it?"): an exact path works when you know where the file is, but not when you want the usual upward search starting from somewhere other than the caller's file, e.g. several projects in one repo sharing a.envin a parent folder. It also gives callers a deterministic way around the stack-frame detection, which is behind reports like #499, #531 and #596.When
start_diris given, the stack isn't inspected at all, and it takes precedence overusecwd.~is expanded, and a start path that doesn't exist raises the same "Starting path not found" error as before. Calls withoutstart_dirare unchanged.Tests: 7 new cases in
tests/test_main.py, covering upward search, str/Path/file inputs, no stack inspection, precedence overusecwd,~expansion, not found, and a missing start path. They fail without the change and pass with it. The rest of the suite passes locally on Windows / Python 3.11, apart from CLI tests that pick up an older globaldotenvon my PATH, which is unrelated to this change. ruff and mypy are clean.If adding a parameter here isn't the direction you want for
find_dotenv, no problem: let me know and I'll close this PR.This was prepared with AI assistance (Claude Code); I reviewed and tested it.