Adam
Adam

Reputation: 2087

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

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?

Upvotes: 4

Views: 1473

Answers (1)

agf
agf

Reputation: 176780

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.

Upvotes: 8

Related Questions