Exiting interactive python3 session from script
Question: I'd like my program to automatically exit if it detects an error when loading
a file and parsing it (even when called from an interactive mode with -i).
I've tried variations of `exit()` and `sys.exit()`, but nothing seems to be
working. Instead of exiting the interactive session I get a stack trace. For
example, if I have the file `test.py` that is the following:

    
    
    import sys
    sys.exit(0)
    

when I run `python3 -i test.py` I get the following result:

    
    
    Traceback (most recent call last):
      File "test.py", line 2, in <module>
        sys.exit()
    SystemExit
    >>>
    

and the session continues on, until I exit myself (using those exact lines
subsequently work, just not when they're called from the script). What am I
missing?


Answer: Try calling [os._exit()](https://docs.python.org/2/library/os.html#os._exit)
to exit directly, without throwing an exception

    
    
    import os
    os._exit(1)
    

Note that this will bypass all of the python shutdown logic. Hope it helps.

