Detecting the presence of IDLE / How to tell if __file__ isn't set

南笙酒味 提交于 2020-01-24 16:26:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!