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

PythonInteractive

Python Problem Overview


Is there a way to programmatically force a Python script to drop into a REPL at an arbitrary point in its execution, even if the script was launched from the command line?

I'm writing a quick and dirty plotting program, which I want to read data from stdin or a file, plot it, and then drop into the REPL to allow for the plot to be customized.

Python Solutions


Solution 1 - Python

I frequently use this:

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

Solution 2 - Python

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.

Solution 3 - Python

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

import IPython
IPython.embed()

For IPython <= v0.11:

from IPython.Shell import IPShellEmbed

ipshell = 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).

Solution 4 - Python

You can launch the debugger:

import pdb;pdb.set_trace() 

Not sure what you want the REPL for, but the debugger is very similar.

Solution 5 - Python

To get use of iPython and functionality of debugger you should use [ipdb][1],

You can use it in the same way as pdb, with the addition of :

import ipdb
ipdb.set_trace()

[1]: http://pypi.python.org/pypi/ipdb/ "ipdb"

Solution 6 - Python

I just did this in one of my own scripts (it runs inside an automation framework that is a huge PITA to instrument):

x = 0 # exit loop counter
while x == 0:
    user_input = raw_input("Please enter a command, or press q to quit: ")
    if user_input[0] == "q":
        x = 1
    else:
        try:
            print eval(user_input)
        except:
            print "I can't do that, Dave."
            continue

Just place this wherever you want a breakpoint, and you can check the state using the same syntax as the python interpreter (although it doesn't seem to let you do module imports). It's not very elegant, but it doesn't require any other setup.

Solution 7 - Python

Great answers above, but if you would like this functionality in your IDE. Using Visual Studio Code (v1.5.*) with Python Setup:

  1. Highlight the lines you would like to run and
  • right click and select Run Selection/Line in Interactive Window from the drop down.
  • Press shift + enter on your keyboard.

enter image description here

  1. Right click on the Python file you want to execute in the file explorer and select Run Current File in Interactive Window

enter image description here

This will launch an interactive session, with linting, code completion and syntax highlighting:

enter image description here

enter image description here

Enter the code you would like to evaluate, and hit shift + enter on your keyboard to execute.

Enjoy Python!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondsimchaView Question on Stackoverflow
Solution 1 - PythonJason R. CoombsView Answer on Stackoverflow
Solution 2 - PythonAlexView Answer on Stackoverflow
Solution 3 - PythonjoeforkerView Answer on Stackoverflow
Solution 4 - PythonNed BatchelderView Answer on Stackoverflow
Solution 5 - PythonbluszczView Answer on Stackoverflow
Solution 6 - PythonrazorboyView Answer on Stackoverflow
Solution 7 - PythonTony CroninView Answer on Stackoverflow