When would os.environ['foo'] not match os.getenv('foo')? When would os.environ['foo'] not match os.getenv('foo')? python python

When would os.environ['foo'] not match os.getenv('foo')?


os.environ is created on import of the os module, and doesn't reflect changes to the environment that occur afterwards unless modified directly. Interestingly enough, however, os.getenv() doesn't actually get the most recent environment variables either, at least not in CPython. You see, in CPython, os.getenv() is apparently just a wrapper around os.environ.get() (see http://hg.python.org/cpython/file/6671c5039e15/Lib/os.py#l646). So it seems the main reason to use os.getenv() with the stated implementation is when you want to have a default value returned when an environment variable name isn't found in os.environ's keys rather than have a KeyError or whatever thrown, and you want to save a few characters.

It's entirely possible that the implementation on FreeBSD has some weird gimmick that causes it to act differently, but I'm not sure why that would be the case. Take a look at the copy of os.py on one of the FreeBSD machines you use, if you can.