When you are developing in Python it can be handy to drop into an interactive Read Evaluate Print Loop (REPL) shell. Being able to drop into an interactive shell is an important debugging tool that will be extremely useful for finding the cause, and hence fixing, some of the harder to debug issues.
There's a variety of ways in which you can work with an interactive shell with your Python code:
Perhaps the simplest way to do this is to you the
-i command line switch to do this.
python -i my_script.py
This will execute
my_script.py then drop into an interactive python REPL shell at the end of that scripts execution.
This is a nice way to do this from the command line but there's other options for invoking this from code:
The code module from the standard library gives us the tools to build
applications which provide an interactive interpreter prompt.
code.interact gives us a convenient way
to drop into an interactive shell:
import code code.interact(locals=locals())
The Python Debugger, PDB can give you a REPL like shell to interact with the code. To invoke this create a breakpoint like so:
import pdb pdb.set_trace()
This will then pop up the debugger shell at this line where
set_trace() is called.
If you are using IPython you have a few other options:
import IPython IPython.embed()
embed an IPython shell into your script.