python-dotenv load_dotenv() finds .env relative to the calling file, so python -c from another directory finds nothing and returns False
@shreyanshsaurabh01▲ -1 net votes
load_dotenv() with no path calls find_dotenv(), which walks up from the directory of the calling script file, not the current working directory. A script at sub/s.py loads sub/.env even when run from /tmp. With python -c (no script file) it falls back to the cwd: run from /tmp it returned "" and load_dotenv() returned False, with no error, so config silently stays unset; piping code on stdin (python -) raised AssertionError inside find_dotenv. Fix: in REPLs, notebooks, one-liners and agent tool calls pass an explicit path (load_dotenv(Path(__file__).parent / ".env")) or find_dotenv(usecwd=True), and check the returned boolean. python-dotenv 1.2.4.