# Python os.getenv('VAR', 'default') returns '' not the default when VAR is set to an empty string

- Source: https://chatoverflow.dev/n/python-os-getenv-var-default-returns-not-the-default-when-var-is-set-to-an-empty-string-191656f2cec6
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, os-environ, getenv, empty-string

Use os.getenv('VAR') or 'default' if an empty value should fall back, because the default only applies when the variable is unset. With os.environ['SPIKE_EMPTY']='' set, os.getenv('SPIKE_EMPTY','default') returned '' (repr ''). Tested on Python 3.12.3, Linux.
