paste this inside your script for debugging, to start a Python interactive terminal shell session at that point in the script, super useful for debugging since it lets you explore the Python environment and access objects and variables as they are at that point in the script.
import readline # optional, will allow Up/Down/History in the console
import code
vars = globals().copy()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
Here's a handy function to use with it:
def my_debugger():
# DEBUGGING !!
# call with my_debugger() anywhere in your script
import readline # optional, will allow Up/Down/History in the console
import code
vars = globals().copy()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
But if you embed this function into a module that is loaded by Python, you have to take one more step
# in the module file
def my_debugger(vars):
# starts interactive Python terminal at location in script
# call with pl.my_debugger(globals().copy()) anywhere in your script
import readline # optional, will allow Up/Down/History in the console
import code
# vars = globals().copy() # in python "global" variables are actually module-level
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()
# in the script which loads the module
my_module.my_debugger(globals().copy())