Use the first; it directly tries to check if something is defined in
environ. Though the second form works equally well, it’s lacking semantically since you get a value back if it exists and only use it for a comparison.
You’re trying to see if something is present in
environ, why would you get just to compare it and then toss it away?
That’s exactly what
Get an environment variable, return
Noneif it doesn’t exist. The
optional second argument can specify an alternate default.
(this also means your check could just be
you don’t want to get it, you want to check for it’s existence.
getenv is just a wrapper around
environ.get but you don’t see people checking for membership in mappings with:
from os import environ if environ.get('Foo') is not None:
To summarize, use:
if "FOO" in os.environ: pass
if you just want to check for existence, while, use
getenv("FOO") if you actually want to do something with the value you might get.