如何从 Python 代码放入 REPL(读取、评估、打印、循环)
有没有办法以编程方式强制 Python 脚本在执行过程中的任意点放入 REPL,即使该脚本是从命令行启动的?
我正在编写一个快速而肮脏的绘图程序,我想从标准输入或文件中读取数据,绘制它,然后放入 REPL 中以允许自定义绘图。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我经常使用这个:
I frequently use this:
您可以尝试使用Python的交互式选项:
这将执行program.py中的代码,然后转到REPL。您在program.py 顶层定义或导入的任何内容都将可用。
You could try using the interactive option for python:
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.
下面是你应该如何做的(IPython > v0.11):
对于 IPython <= v0.11:
你应该使用 IPython,Python REPL 中的凯迪拉克。请参阅http://ipython.org/ipython-doc/stable/ Interactive/reference.html#embedding-ipython
来自文档:
Here's how you should do it (IPython > v0.11):
For IPython <= v0.11:
You should use IPython, the Cadillac of Python REPLs. See http://ipython.org/ipython-doc/stable/interactive/reference.html#embedding-ipython
From the documentation:
您可以启动调试器:
不确定您想要 REPL 的用途,但调试器非常相似。
You can launch the debugger:
Not sure what you want the REPL for, but the debugger is very similar.
要使用 iPython 和调试器的功能,您应该使用 ipdb,
您可以以与 pdb 相同的方式使用它,但添加:
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 :
我只是在我自己的一个脚本中执行了此操作(它在一个自动化框架内运行,该框架是一个巨大的 PITA 工具):
只需将其放置在您想要断点的任何位置,您就可以使用与 python 解释器相同的语法检查状态(尽管它似乎不允许您进行模块导入)。
它不是很优雅,但不需要任何其他设置。
I just did this in one of my own scripts (it runs inside an automation framework that is a huge PITA to instrument):
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.
上面的答案很好,但是如果您希望在 IDE 中使用此功能。将 Visual Studio Code (v1.5.*) 与 Python 设置:
右键单击
并选择运行选择/行从下拉列表中的交互窗口
中。shift + Enter
。Python
文件,然后选择在交互窗口中运行当前文件
这将启动一个交互式会话,包括 linting、代码完成和语法突出显示:
输入您要评估的代码,然后按
shift +在键盘上输入
来执行。享受Python!
Great answers above, but if you would like this functionality in your IDE. Using Visual Studio Code (v1.5.*) with Python Setup:
right click
and selectRun Selection/Line in Interactive Window
from the drop down.shift + enter
on your keyboard.Python
file you want to execute in the file explorer and selectRun Current File in Interactive Window
This will launch an interactive session, with linting, code completion and syntax highlighting:
Enter the code you would like to evaluate, and hit
shift + enter
on your keyboard to execute.Enjoy Python!
该函数与调用者的局部变量和全局变量进行
code.interact
:This function does
code.interact
with the locals and globals of the caller: