Entering Python’s Interactive Mode

You can drop into Python’s interactive mode immediately after executing a script. Say you have the following script, foo.py.

message = 'Hello, World!'

To execute foo.py then immediately enter interactive mode, use the Python interpreter’s -i option.

$ python -i foo.py
>>> message
'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.

Was this helpful? Buy me a coffee!