As seen on stackoverflow
If you place
import code code.interact(local=locals())
at any point in your script, python will instantiate a python shell at exactly that point that has access to everything in the state of the script at that point.
^D exits the shell and resumes execution past that point.
You can even modify the state at that point from the shell, call functions, etc.