How can you tell whether python has been started with the -i flag?
According to the docs, you can check the PYTHONINSPECT variable in os.environ, which is the equivalent of -i. But apparently it doesn’t work the same way.
Works:
$ PYTHONINSPECT=1 python -c 'import os; print os.environ['PYTHONINSPECT']'
Doesn’t work:
$ python -i -c 'import os; print os.environ['PYTHONINSPECT']'
The reason I ask is because I have a script that calls sys.exit(-1) if certain conditions fail. This is good, but sometimes I want to manually debug it using -i. I suppose I can just learn to use ‘PYTHONINSPECT=1 python’ instead of ‘python -i’, but it would be nice if there were a universal way of doing this.
How to set inspect mode programmatically
The answer from the link @Jweede provided is imprecise. It should be:
How to retrieve whether interactive/inspect flags are set
Just another variant of @Brian’s answer:
See the Python’s main.c.