python – What is a good practice to check if an environmental variable exists or not?

python – What is a good practice to check if an environmental variable exists or not?

Use the first; it directly tries to check if something is defined in environ. Though the second form works equally well, its lacking semantically since you get a value back if it exists and only use it for a comparison.

Youre trying to see if something is present in environ, why would you get just to compare it and then toss it away?

Thats exactly what getenv does:

Get an environment variable, return None if it doesnt exist. The
optional second argument can specify an alternate default.

(this also means your check could just be if getenv(FOO))

you dont want to get it, you want to check for its existence.

Either way, getenv is just a wrapper around environ.get but you dont 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:

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.

There is a case for either solution, depending on what you want to do conditional on the existence of the environment variable.

Case 1

When you want to take different actions purely based on the existence of the environment variable, without caring for its value, the first solution is the best practice. It succinctly describes what you test for: is FOO in the list of environment variables.

if KITTEN_ALLERGY in os.environ:

Case 2

When you want to set a default value if the value is not defined in the environment variables the second solution is actually useful, though not in the form you wrote it:

server = os.getenv(MY_CAT_STREAMS,

or perhaps

server = os.environ.get(MY_CAT_STREAMS,

Note that if you have several options for your application you might want to look into ChainMap, which allows to merge multiple dicts based on keys. There is an example of this in the ChainMap documentation:

combined = ChainMap(command_line_args, os.environ, defaults)

python – What is a good practice to check if an environmental variable exists or not?

To be on the safe side use

os.getenv(FOO) or bar

A corner case with the above answers is when the environment variable is set but is empty

For this special case you get

print(os.getenv(FOO, bar))
# prints new line - though you expected `bar`


if FOO in os.environ:
    print(FOO is here)
# prints FOO is here - however its not

To avoid this just use or

os.getenv(FOO) or bar

Then you get

print(os.getenv(FOO) or bar)
# bar

When do we have empty environment variables?

You forgot to set the value in the .env file

# .env

or exported as

$ export FOO=

or forgot to set it in

os.environ[FOO] = 

Update: if in doubt, check out these one-liners

>>> import os; os.environ[FOO] = ; print(os.getenv(FOO, bar))

$ FOO= python -c import os; print(os.getenv(FOO, bar))

Leave a Reply

Your email address will not be published. Required fields are marked *