How to get Python interactive console in current namespace?

user553702 picture user553702 · Aug 23, 2011 · Viewed 9.3k times · Source

I would like to have my Python code start a Python interactive console (REPL) in the middle of running code using something like code.interact(). But the console that code.interact() starts doesn't see the variables in the current namespace. How do I do something like:

mystring="hello"

code.interact()

... and then in the interactive console that starts, I should be able to type mystring and get "hello". Is this possible? Do I need to set the "local" argument of code.interact() to something? What would this be set to? How should it be called?

Answer

morsik picture morsik · Aug 23, 2011

Try:

code.interact(local=locals())