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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With