Detect if interactive or script mode

The documentation says “These are only defined if the interpreter is in interactive mode.”

The test should be

import sys

if hasattr(sys, 'ps1'):
    print('We are in interactive mode.")

You can also combine this with looking at __name__

import sys

if __name__ == '__main__':
    print('Running as script')
    if hasattr(sys, 'ps1'):
        print('Interactive')
    else:
        print('Not interactive')
else:
    print('Loaded as a module')