It gets better
See this answer
Instead of doing it with globals, you could just make your ipdb
session interactive by typing interact
at the start of your session.
Now you can continue just the way you were and everything should work.
Better solution
Pull the module into the global scope
ipdb> import ast
ipdb> global ast # <-- This
ipdb> [t.unparse() if isinstance(t, ast.AST) else t for t in tree]
Previous answer
You could also just create a global variable that refers to AST
ipdb> import ast
ipdb> AST=ast.AST
ipdb> [t.unparse() if isinstance(t, AST) else t for t in tree]