Get locals from calling namespace in Python

If you’re writing a debugger, you’ll want to make heavy use of the inspect module:

def show_callers_locals():
    """Print the local variables in the caller's frame."""
    import inspect
    frame = inspect.currentframe()
    try:
        print(frame.f_back.f_locals)
    finally:
        del frame

Leave a Comment