Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a python script to invoke "python -i" when called normally?

I have a python script that I like to run with python -i script.py, which runs the script and then enters interactive mode so that I can play around with the results.

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

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

like image 358
Matthew Moisen Avatar asked Aug 25 '16 22:08

Matthew Moisen


People also ask

How do I run Python code interactively?

The interactive Python mode lets you run your script instantly via the command line without using any code editor or IDE. To run a Python script interactively, open up your command line and type python. Then hit Enter. You can then go ahead and write any Python code within the interactive mode.

What is Python interactive mode?

Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.

How do I run a Python script from another file?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.


3 Answers

From within script.py, set the PYTHONINSPECT environment variable to any nonempty string. Python will recheck this environment variable at the end of the program and enter interactive mode.

import os
# This can be placed at top or bottom of the script, unlike code.interact
os.environ['PYTHONINSPECT'] = 'TRUE'  
like image 178
user2357112 supports Monica Avatar answered Sep 22 '22 08:09

user2357112 supports Monica


In addition to all the above answers, you can run the script as simply ./script.py by making the file executable and setting the shebang line, e.g.

#!/usr/bin/python -i
this = "A really boring program"

If you want to use this with the env command in order to get the system default python, then you can try using a shebang like @donkopotamus suggested in the comments

#!/usr/bin/env PYTHONINSPECT=1 python

The success of this may depend on the version of env installed on your platform however.

like image 37
porglezomp Avatar answered Sep 24 '22 08:09

porglezomp


You could use an instance of code.InteractiveConsole to get this to work:

from code import InteractiveConsole
i = 20
d = 30
InteractiveConsole(locals=locals()).interact()

running this with python script.py will launch an interactive interpreter as the final statement and make the local names defined visible via locals=locals().

>>> i
20

Similarly, a convenience function named code.interact can be used:

from code import interact
i = 20
d = 30
interact(local=locals())

This creates the instance for you, with the only caveat that locals is named local instead.


In addition to this, as @Blender stated in the comments, you could also embed the IPython REPL by using:

import IPython
IPython.embed()

which has the added benefit of not requiring the namespace that has been populated in your script to be passed with locals.

like image 36
Dimitris Fasarakis Hilliard Avatar answered Sep 25 '22 08:09

Dimitris Fasarakis Hilliard