#if the variable is local:
if 'myVar' in locals():
# myVar exists
#if the variable is global:
if 'myVar' in globals():
# myVar exists.
# 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
# for local
if 'myVar' in locals():
# myVar exists.
# for globals
if 'myVar' in globals():
# myVar exists.
try:
myVar
except NameError:
myVar = None # or some other default value.
# Now you're free to use myVar without Python complaining.