Entering Python's Interactive Mode
December 14, 2016You can drop into Python’s interactive mode immediately after executing a script. Say you have the following script, hello.py
.
message = 'Hello, World!'
def greet():
print(message)
greet()
To execute hello.py
then immediately enter interactive mode, use the Python interpreter’s -i
option.
$ python -i hello.py
Hello, World!
>>> greet()
Hello, World!
This is a nice way to avoid repeatedly typing the same commands when developing a script or experimenting with something new. You can achieve a similar effect by setting a breakpoint in your script, though I prefer the -i
option when I don’t want to end up in the debugger.