如何从Python代码中删除REPL(读取,Eval,打印,循环)

时间:2022-09-06 13:13:34

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?

有没有办法以编程方式强制Python脚本在执行中的任意点放入REPL,即使脚本是从命令行启动的?

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.

我正在编写一个快速而肮脏的绘图程序,我想从stdin或文件中读取数据,绘制它,然后放入REPL以允许自定义绘图。

6 个解决方案

#1


83  

You could try using the interactive option for python:

您可以尝试使用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.

这将执行program.py中的代码,然后转到REPL。您在program.py*定义或导入的任何内容都将可用。

#2


107  

I frequently use this:

我经常使用这个:

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

#3


39  

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

这是你应该怎么做(IPython> v0.11):

import IPython
IPython.embed()

For IPython <= v0.11:

对于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

你应该使用IPython,Python REPLs的凯迪拉克。见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).

它在科学计算情况下也很有用,在这种情况下,通常需要执行一些自动的,计算密集的部分,然后停下来查看数据,图表等。打开IPython实例将使您可以完全访问您的数据和函数,完成交互式部分后,您可以恢复程序执行(可能会在以后再次停止,根据需要多次停止)。

#4


17  

You can launch the debugger:

您可以启动调试器:

import pdb;pdb.set_trace() 

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

不确定你想要什么REPL,但调试器非常相似。

#5


15  

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

要使用iPython和调试器的功能,你应该使用ipdb,

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

您可以像使用pdb一样使用它,并添加:

import ipdb
ipdb.set_trace()

#6


0  

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

我只是在我自己的一个脚本中执行此操作(它在自动化框架内运行,这是一个巨大的PITA仪器):

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.

只需将它放在您想要断点的任何地方,您就可以使用与python解释器相同的语法来检查状态(尽管它似乎不允许您进行模块导入)。它不是很优雅,但它不需要任何其他设置。

#1


83  

You could try using the interactive option for python:

您可以尝试使用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.

这将执行program.py中的代码,然后转到REPL。您在program.py*定义或导入的任何内容都将可用。

#2


107  

I frequently use this:

我经常使用这个:

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

#3


39  

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

这是你应该怎么做(IPython> v0.11):

import IPython
IPython.embed()

For IPython <= v0.11:

对于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

你应该使用IPython,Python REPLs的凯迪拉克。见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).

它在科学计算情况下也很有用,在这种情况下,通常需要执行一些自动的,计算密集的部分,然后停下来查看数据,图表等。打开IPython实例将使您可以完全访问您的数据和函数,完成交互式部分后,您可以恢复程序执行(可能会在以后再次停止,根据需要多次停止)。

#4


17  

You can launch the debugger:

您可以启动调试器:

import pdb;pdb.set_trace() 

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

不确定你想要什么REPL,但调试器非常相似。

#5


15  

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

要使用iPython和调试器的功能,你应该使用ipdb,

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

您可以像使用pdb一样使用它,并添加:

import ipdb
ipdb.set_trace()

#6


0  

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

我只是在我自己的一个脚本中执行此操作(它在自动化框架内运行,这是一个巨大的PITA仪器):

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.

只需将它放在您想要断点的任何地方,您就可以使用与python解释器相同的语法来检查状态(尽管它似乎不允许您进行模块导入)。它不是很优雅,但它不需要任何其他设置。