Detecting the presence of IDLE / How to tell if __file__ isn't set
I had a script that need开发者_C百科ed the use of __file__
, and so I learned that IDLE doesn't set this. Is there a way from my script that I can detect the presence of IDLE?
if '__file__' not in globals():
# __file__ is not set
if you want to do something special if __file__
isn't set. Or,
try:
__file__
except NameError:
# __file__ is not set
raise
if you want to do something then raise an error anyway, or
global __file__
__file__ = globals().get('__file__', 'your_default_here')
if you want to have a default.
精彩评论