How to drop into REPL (Read, Eval, Print, Loop) from Python code How to drop into REPL (Read, Eval, Print, Loop) from Python code python python

How to drop into REPL (Read, Eval, Print, Loop) from Python code


I frequently use this:

def interact():    import code    code.InteractiveConsole(locals=globals()).interact()


You could try using the interactive option for python:

python -i program.py

This will execute the code in program.py, then go to the REPL. Anything you define or import in the top level of program.py will be available.


Here's how you should do it (IPython > v0.11):

import IPythonIPython.embed()

For IPython <= v0.11:

from IPython.Shell import IPShellEmbedipshell = IPShellEmbed()ipshell() # this call anywhere in your program will start IPython

You should use IPython, the Cadillac of Python REPLs. See http://ipython.org/ipython-doc/stable/interactive/reference.html#embedding-ipython

From the documentation:

It can also be useful in scientific computing situations where it is common to need to do some automatic, computationally intensive part and then stop to look at data, plots, etc. Opening an IPython instance will give you full access to your data and functions, and you can resume program execution once you are done with the interactive part (perhaps to stop again later, as many times as needed).