This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.environ.get() doesn't handle default value
Type: enhancement Stage:
Components: Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, marystern
Priority: normal Keywords:

Created on 2009-02-13 18:54 by marystern, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg81963 - (view) Author: Mary Stern (marystern) Date: 2009-02-13 18:54
os.environ.get('ENV_VAR, 'mydefault')

returns '' rather than 'mydefault' if not set.

It would be nice if this standard dict-style behavior was supported.
msg81965 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-02-13 19:21
>>> os.environ['FOO']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/UserDict.py", line 22, in __getitem__
    raise KeyError(key)
KeyError: 'FOO'
>>> os.environ.get('FOO')
>>> os.environ.get('FOO', 'bar')
'bar'


% export FOO=
% python2.5
>>> import os
>>> os.environ['FOO']
''

An environment variable set to '' is valid and is still considered set
(thats how the environment works).
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49503
2009-02-13 19:21:53gregory.p.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg81965
nosy: + gregory.p.smith
2009-02-13 18:54:59marysterncreate