Description
dotenv get KEY treats an empty string value as missing because the CLI uses a truthiness check (if stored_value:) instead of testing for key presence / None.
Empty values are valid in .env files (KEY= or KEY=""). The library API already returns them correctly via get_key / dotenv_values; only the CLI get command is wrong.
Steps to reproduce
import subprocess, sys, tempfile
from pathlib import Path
from dotenv import set_key
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / ".env"
set_key(path, "EMPTY", "")
result = subprocess.run(
[sys.executable, "-m", "dotenv", "-f", str(path), "get", "EMPTY"],
capture_output=True,
text=True,
)
print(result.returncode, repr(result.stdout))
Expected behavior
(exit code 0, empty value printed)
Actual behavior
(exit code 1 — same as a missing key)
dotenv get ZERO with ZERO=0 works (exit 0), so only falsy-but-present empty strings are affected.
Environment
- python-dotenv: 1.2.3 (current
main)
- Python: 3.14
- OS: Windows
Suggested fix
In src/dotenv/cli.py get, distinguish missing/None from an empty string, e.g. exit only when the key is absent or the stored value is None.
Description
dotenv get KEYtreats an empty string value as missing because the CLI uses a truthiness check (if stored_value:) instead of testing for key presence /None.Empty values are valid in
.envfiles (KEY=orKEY=""). The library API already returns them correctly viaget_key/dotenv_values; only the CLIgetcommand is wrong.Steps to reproduce
Expected behavior
(exit code 0, empty value printed)
Actual behavior
(exit code 1 — same as a missing key)
dotenv get ZEROwithZERO=0works (exit 0), so only falsy-but-present empty strings are affected.Environment
main)Suggested fix
In
src/dotenv/cli.pyget, distinguish missing/Nonefrom an empty string, e.g. exit only when the key is absent or the stored value isNone.