xxxxxxxxxx
# If the variable exists at all:
if "myVar" in locals() or globals():
# `myVar` exists
# If the variable is local:
if "myVar" in locals():
# `myVar` is local
# If the variable is global:
if "myVar" in globals():
# `myVar` is global
xxxxxxxxxx
#if the variable is local:
if 'myVar' in locals():
# myVar exists
#if the variable is global:
if 'myVar' in globals():
# myVar exists.
xxxxxxxxxx
# three liner
try: x
except NameError: return False
else: return True
# or, for local vars
return 'x' in locals()
# or, for global vars
return 'x' in globals()
xxxxxxxxxx
# for local
if 'myVar' in locals():
# myVar exists.
# for globals
if 'myVar' in globals():
# myVar exists.
xxxxxxxxxx
try:
myVar
except NameError:
myVar = None # or some other default value.
# Now you're free to use myVar without Python complaining.