完成后如何以编程方式告诉 CPython 解释器进入交互模式?
如果您使用 -i 选项调用 cpython 解释器,它将在完成指定要运行的任何命令或脚本后进入交互模式。 有没有办法在程序中让解释器执行此操作,即使没有给出 -i ? 明显的用例是在发生异常情况时通过交互式检查状态来进行调试。
If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively inspecting the state when an exceptional condition has occurred.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要代码模块。
You want the code module.
设置 PYTHONINSPECT 环境变量。 这也可以在脚本本身中完成:
为了调试意外异常,您还可以使用这个不错的配方 http: //code.activestate.com/recipes/65287/
Set the PYTHONINSPECT environment variable. This can also be done in the script itself:
For debugging unexpected exceptions, you could also use this nice recipe http://code.activestate.com/recipes/65287/
使用
sys.excepthook
在另一个答案中提到的食谱,听起来像你想要的。 否则,您可以在程序退出时运行code.interact
:The recipe metioned in the other answer using
sys.excepthook
, sounds like what you want. Otherwise, you could runcode.interact
on program exit:据我所知,执行此操作的最佳方法是:
它允许访问当前范围内的变量,并为您带来 IPython 的全部功能。
The best way to do this that I know of is:
which allows access to variables in the current scope and brings you the full power of IPython.