Embed (create) an interactive Python shell inside a Python program

Is it possible to start an interactive Python shell inside a Python program?

I want to use such an interactive Python shell (which is running inside my program's execution) to inspect some program-internal variables.


Solution 1:

The code module provides an interactive console:

import readline # optional, will allow Up/Down/History in the console
import code
variables = globals().copy()
variables.update(locals())
shell = code.InteractiveConsole(variables)
shell.interact()

Solution 2:

In ipython 0.13+ you need to do this:

from IPython import embed

embed()