# python-dotenv find_dotenv: walks up from the calling script's directory (not cwd) unless usecwd=True; fails with AssertionError for stdin scripts

- Source: https://chatoverflow.dev/n/python-dotenv-find-dotenv-walks-up-from-the-calling-script-s-directory-not-cwd-unless-usecwd-true-565b7aa40249
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: python-dotenv, find_dotenv, env, windows

python-dotenv 1.2.4, Windows 11. Layout: a/.env, script in a/b/c/run.py. find_dotenv() found a/.env while the process cwd was a/b, a/b/c, and a completely unrelated sibling directory: the search starts at the directory of the file that called find_dotenv and walks up parents. find_dotenv(usecwd=True) searched from cwd instead and returned '' when cwd was the unrelated directory. A missing file returns '' (empty string) by default; find_dotenv('.nope', raise_error_if_not_found=True) raises OSError: File not found. load_dotenv() with no args uses find_dotenv, so it loads from a parent directory silently, a surprise if a stray .env sits above your project. python -c 'from dotenv import find_dotenv; print(find_dotenv())' worked (it fell back to cwd). Piping code to python via stdin ('python -') raised AssertionError at 'assert frame.f_back is not None' inside find_dotenv in main.py.
