In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.
# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!
On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.
To find out if a bash variable is defined: Return true if a bash variable is unset or set to the empty string: if [ -z ${my_variable+x} ];
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 getenv
does:
Get an environment variable, return
None
if it doesn't exist. The optional second argument can specify an alternate default.
(this also means your check could just be if getenv("FOO")
)
you don't want to get it, you want to check for it's existence.
Either way, 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.
There is a case for either solution, depending on what you want to do conditional on the existence of the environment variable.
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:
buy_puppy()
else:
buy_kitten()
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', 'youtube.com')
or perhaps
server = os.environ.get('MY_CAT_STREAMS', 'youtube.com')
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)
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`
or
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
FOO=
or exported as
$ export FOO=
or forgot to set it in settings.py
# settings.py
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'))"
In case you want to check if multiple env variables are not set, you can do the following:
import os
MANDATORY_ENV_VARS = ["FOO", "BAR"]
for var in MANDATORY_ENV_VARS:
if var not in os.environ:
raise EnvironmentError("Failed because {} is not set.".format(var))
I'd recommend the following solution.
It prints the env vars you didn't include, which lets you add them all at once. If you go for the for loop, you're going to have to rerun the program to see each missing var.
from os import environ
REQUIRED_ENV_VARS = {"A", "B", "C", "D"}
diff = REQUIRED_ENV_VARS.difference(environ)
if len(diff) > 0:
raise EnvironmentError(f'Failed because {diff} are not set')
Considering that you're using an environment variable named Foo
which could change based on different environments, I would suggest to get its value and INFO (or DEBUG) log it even if you use its value (e.g. boolean) just as a toggle or decision-making criterion. It could help you when something goes wrong or just for sanity check later.
#python=3.8
import os
import logging
foo = os.getenv("FOO")
if foo:
logging.info(f"Proceeding since environment variable FOO exists {foo=}")
pass
else:
# proper action/logging based on the context
pass
I would advise against hardcoding alternatives in the code itself without proper logging. Using foo = os.getenv("FOO", "BAR")
could silently change the behavior of your code.
Also, it is best to have all your constants either in a config file or as environment variables and don't hardcode anything in the source code itself. Defaults and alternatives could go inside the config file and environment-changing constants should be env vars so that you could change them during the deployments easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With