Cia*_*ran 5 python debugging console interpreter
经过Matlab多年的研究编程,我想念我可以在执行中暂停程序并检查变量,通过交互式控制台绘制,保存/修改数据等,然后恢复执行.
有没有办法在python中做同样的事情?
例如:
# ... python code ...
RunInterpreter
# Interactive console is displayed, so user can inspect local/global variables
# User types CTRL-D to exit, and script then continues to run
# ... more python code ...
Run Code Online (Sandbox Code Playgroud)
这将使调试变得更容易.建议非常感谢,谢谢!
使用该pdb
库.
我<F8>
在Vim中绑定了这一行:
import pdb; pdb.set_trace()
Run Code Online (Sandbox Code Playgroud)
这会让你进入一个pdb
控制台.
该pdb
控制台是不是很一样的标准Python控制台...但它会做大部分的同样的东西.另外,在我~/.pdbrc
,我有:
alias i from IPython.Shell import IPShellEmbed as IPSh; IPSh(argv='')()
Run Code Online (Sandbox Code Playgroud)
这样我就可以pdb
通过以下i
命令进入"真正的"iPython shell :
(pdb) i
...
In [1]:
Run Code Online (Sandbox Code Playgroud)
我发现的优秀解决方案是使用“代码”模块。我现在可以从代码中的任何位置调用“DebugKeyboard()”,并且会弹出解释器提示,让我可以检查变量并运行代码。CTRL-D 将继续该程序。
import code
import sys
def DebugKeyboard(banner="Debugger started (CTRL-D to quit)"):
# use exception trick to pick up the current frame
try:
raise None
except:
frame = sys.exc_info()[2].tb_frame.f_back
# evaluate commands in current namespace
namespace = frame.f_globals.copy()
namespace.update(frame.f_locals)
print "START DEBUG"
code.interact(banner=banner, local=namespace)
print "END DEBUG"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1702 次 |
最近记录: |