try:
from file import varName
except ImportError:
print 'var not found'
Alternatively, you can do this (if you have already imported the file):
import file
try:
v = file.varName
except AttributeError:
print 'var not found'
This will only work if var is global. If you use variables with scope, you will need to use introspection.
source
share