问题
I had a script that needed 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?
回答1:
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.
来源:https://stackoverflow.com/questions/7049481/detecting-the-presence-of-idle-how-to-tell-if-file-isnt-set