Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an interpreter for Python similar to Pry for Ruby? [duplicate]

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.

like image 355
dsimcha Avatar asked Mar 27 '26 06:03

dsimcha


2 Answers

I frequently use this:

def interact():
    import code
    code.InteractiveConsole(locals=globals()).interact()
like image 102
Jason R. Coombs Avatar answered Mar 29 '26 18:03

Jason R. Coombs


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.

like image 21
Alex Avatar answered Mar 29 '26 20:03

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!