9

So, I want to check and verify if a given variable "abc" exists and that it's true. If the variable exist and is False, then I want it to go to else. Here is how I got it to work in python:

env = os.environ.copy()
if "abc" in env and env['abc'] == "True":
    print "Works"
else:
    print "Doesn't work"

Is there a better way to do it?

victor
  • 1,573
  • 11
  • 23
Jason
  • 2,246
  • 6
  • 34
  • 53
  • `env.get('abc', False) == True` – inspectorG4dget Jun 12 '17 at 17:34
  • that string of identical answers all within 10sec of eachother was impressive... – Aaron Jun 12 '17 at 17:35
  • 4
    Are you actually looking for a string of the value "True", or are you looking for a boolean `True`? – idjaw Jun 12 '17 at 17:35
  • 4
    If `env` is `os.environ` and these are literally environment variables, it'll be a string `"True"`, but it's not clear what `env` is and what preprocessing may have been done. – user2357112 Jun 12 '17 at 17:43
  • @idjaw I think because I am checking for env variable I am not sure if I can check for boolean! – Jason Jun 12 '17 at 17:47
  • Possible duplicate of [How do I check if a variable exists?](https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists) – victor Jun 12 '17 at 18:05
  • @VictorC. I tried your below suggestion in Python 2.7.9 but doesn't work there. Essentially I want something that works on 2.7 and 3.0 – Jason Jun 12 '17 at 20:41
  • 1
    @Jason That's strange, `locals()` and `globals()` exists in Python 2.7 as well...it works for me when I try it with version 2.7. What error are you getting? – victor Jun 12 '17 at 21:07
  • @VictorC. So I did an export abc=True but the locals().get('abc') gives me null in 2.7.9. Do you have any other imports apart from sys or os? – Jason Jun 13 '17 at 00:19
  • @VictorC. Now I understand how locals and globals work. Given that I am not sure if there is a need for locals or globals as I can just do `env.get('abc')`. But that still leaves the "True" or "False" open as any of the output here doesnt check if the variable is True/False. How do I go to else if the variable exist and is False ? – Jason Jun 13 '17 at 00:35
  • @Jason `os.environ` is still a dictionary similar to local variables. You can do the same thing you did with globals, namely `if env.get('abc')=='True'` – victor Jun 13 '17 at 17:42
  • `os.environ.get('abc') or None` will work as well – Rahul Reddy Jun 09 '20 at 13:55

4 Answers4

13

You can check to see if the variable is in the dictionaries returned by globals() and locals(). (Thank you to Aaron for reminding me to add the full code)

For a local variable:

if locals().get('abc'):
    print(abc)

For a global variable:

if globals().get('abc'):
    print(abc)

For an environment variable:

if os.environ.get('abc')=='True':
    #abc is set to True

More information here:

https://docs.python.org/3/library/functions.html#locals https://docs.python.org/3/library/functions.html#globals

victor
  • 1,573
  • 11
  • 23
  • 1
    the full answer would then look like `if locals().get('abc') == True:` (or `...globals()...`) – Aaron Jun 12 '17 at 17:55
10

You can use:

env.get("abc", False)

False is the default value if "abc" is not in env.

1

You could use a Try Except Block.

try:
    # Try calling ABC here anyway you like
    # Here I am just printing it
    print(abc)
except NameError:
    print("Variable ABC does not exist")
Tyler Bell
  • 837
  • 10
  • 30
  • 1
    If you do it this way, you should do `except NameError:` instead of blindly catching any exception – trent Jun 12 '17 at 17:47
0

It's enough get it from env, I think

env.get('abc')
vZ10
  • 2,468
  • 2
  • 23
  • 33