How to get a python script to invoke "python -i" when called normally?
<p>I have a python script that I like to run with <code>python -i script.py</code>, which runs the script and then enters interactive mode so that I can play around with the results.</p>

<p>Is it possible to have the script itself invoke this option, such that I can just run <code>python script.py</code> and the script will enter interactive mode after running?</p>

<p>Of course, I can simply add the <code>-i</code>, or if that is too much effort, I can write a shell script to invoke this.</p>

<p>From within <code>script.py</code>, set the <a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONINSPECT"><code>PYTHONINSPECT</code></a> environment variable to any nonempty string. Python will recheck this environment variable at the end of the program and enter interactive mode.</p>

<pre><code>import os
# This can be placed at top or bottom of the script, unlike code.interact
os.environ['PYTHONINSPECT'] = 'TRUE'  
</code></pre>

python python-interactive