141

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.

8 Answers 8

157

I frequently use this:

def interact():
    import code
    code.InteractiveConsole(locals=globals()).interact()
2
  • 58
    You can do it even simpler than that: import code; code.interact(local=locals()) Sep 9, 2009 at 4:17
  • 5
    Within pdb, you can use interact.
    – gerrit
    Aug 19, 2014 at 23:05
139

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.

1
  • 27
    When you're ready to switch to the dark side, ipython -i program.py is there for you.
    – joeforker
    Sep 9, 2009 at 20:26
52

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

4
  • 4
    IPython is great, but if the OP wants a solution that only uses built-in Python, Jason's code.InteractiveConsole() solution is how you "should" do it. :-)
    – Ben Hoyt
    Sep 8, 2009 at 22:56
  • This is the only method that could actually read local variables when placed in arbitrary locations in the code for me
    – corazza
    Feb 7, 2021 at 19:21
  • Is there a way to stop the script from the ipython repl? I've put the call to embed() in a tight loop!
    – kristianp
    Mar 21, 2023 at 0:42
  • What difference or benefit does IPython provide over the code module?
    – Erich
    Apr 4, 2023 at 22:29
20

You can launch the debugger:

import pdb;pdb.set_trace() 

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

5
  • 4
    I would suspect that he would like to make live changes to a running process, à la Lisp.
    – Pinochle
    Sep 8, 2009 at 19:59
  • 1
    Is there any way to resume executing the script that launched you into pdb? Dec 11, 2013 at 20:37
  • 1
    I tend to write code snippet in VIM instaed of Python save it and then do a !python -i % for example . Or pdb . Thats a good use case.
    – Nishant
    Jan 2, 2014 at 14:03
  • 2
    Since Python 3.7 you can call the breakpoint() built-in function to achieve the same effect.
    – Jasha
    Oct 23, 2020 at 23:49
  • @JeffWelling The pdb documentation provides a more thorough answer, but continue can be used to resume. c is a shorthand alternative.
    – Erich
    Apr 4, 2023 at 22:29
18

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

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

import ipdb
ipdb.set_trace()
1

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.

0

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!

1
  • 1
    This is beautiful tear drops. If there's anything more useful than an IPython REPL, is a Jupyter Notebook. You can execute your script in parts if you select and "shift+enter" each. Visual Vim selection works a threat if you don't wanna use the mouse. I did had to negate "-python.execSelectionInTerminal" in my keybindings and copy it with "jupyter.execSelectionInteractive" instead for the code to run in the notebook instead of a terminal.
    – Chema
    Apr 25, 2023 at 2:05
0

This function does code.interact with the locals and globals of the caller:

def repl():
    """This starts a REPL with the callers globals and locals available

    Raises:
        RuntimeError: Is raised when the callers frame is not available
    """
    import code
    import inspect

    frame = inspect.currentframe()
    if not frame:
        raise RuntimeError('No caller frame')

    code.interact(local=dict(frame.f_back.f_globals, **frame.f_back.f_locals))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.