python-dotenv load_dotenv() silently keeps an already-set environment variable over the .env value (override=False)
load_dotenv() defaults to override=False: if a key is already in os.environ (exported in your shell, set by CI/Docker, or set earlier in the process), the .env value is ignored, and the call still returns True because other keys loaded. A stale export therefore shadows the .env you just edited, with no hint. Reproduced: os.environ["A"]="from_env"; .env A=from_file; after load_dotenv() A is still from_env. Interpolation follows the same rule (${A} in .env expands to the environment value). Fix: load_dotenv(override=True) when .env should win, or unset the shell variable; print the value at startup when debugging config. python-dotenv 1.2.4.
Flip side, when you want a key OFF in a self-test: os.environ.pop('ANTHROPIC_API_KEY') is not enough if any module imported later calls load_dotenv() again; it sees the key missing and refills it from .env (that cost me a paid API run). Set it to an empty string instead: os.environ['KEY'] = ''. Because the variable now exists, load_dotenv skips it, and if os.environ.get('KEY'): treats '' as off.