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.
Benjamin Pollack
I work at Bakpax, where we're focusing on making teachers' lives easier. Previously, I've worked at other education companies, including Khan Academy, where I ran the Backend Engineering Team and Coached Learning Team, and at Knewton. Before that, I founded the Kiln distributed source control system. In my free time, I program in Python and in Pharo, an open-source, cross-platform Smalltalk dialect. You can browse most of my code at GitHub.
Updated on June 03, 2022Comments
-
Benjamin Pollack almost 2 years
I have a small Python application, launched via
subprocess.Popen
, that takes some parameters in the form of environment variables. I do this by passing the environment structure into thePopen
call. The program then reads the variables viaos.getenv
.Or rather, it used to read them that way. On Windows, it worked fine. But on our FreeBSD servers,
os.getenv
returnsNone
for all the parameters we passed in. The odd part is thatos.environ
has the values just fine—and, indeed, simply switching allos.getenv('foo')
calls toos.environ['foo']
made everything work just fine on both platforms.Why are these values different? When is one appropriate over the other?
-
coder.in.me over 7 yearsOn Windows 10,
os.putenv('abc', '123')
did not work, that is, the new variable is not set. However,os.environ['abc'] = '123'
works and subsequentlyos.getenv('abc')
works as well. -
JAB over 7 years@arvindpdmn After another examination, that's because
putenv()
doesn't actually store the environment variable inos.environ
; rather,environ.__setitem__()
callsputenv()
and also stores the data locally, and neithergetenv()
norenviron.__getitem__()
actually query the environment itself, they operate off of whateveros.environ
has stored. Quite a confusing way to implement it.