我喜欢使用以下方法检查Python脚本中的错误:
$ python3 -m pdb my_script.py
这会将我放入pdb提示符中,在那里我可以c
继续执行,当它遇到错误时,我可以检查变量,然后q
退出脚本执行以返回shell。
我对iPython调试器模块也做了同样的尝试,因为它更加丰富多彩:
$ python3 -m ipdb my_script.py
但是,一旦检查完错误,我就无法退出调试器。使用q
quit命令只是在重新执行脚本和死后模式之间不断切换:
$ python3 -m ipdb my_script.py
ipdb> c
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> Inspect some variables at this point
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
如何退出此调试器?
使用ctrl+z或打开第二个终端,然后查找进程(
ps -ax | grep python
)并终止进程。一步一步:
访问终端:
ssh server
(使用选项B或C,以便可以打开第二个连接来执行命令)查找进程
ps -ax | grep python
的相应pythonPID
。例如,我的进程(python my_stucked_process.py
)的进程id将是112923
:kill -9 112923
@tutuDajuju建议使用ctrl+z,但他们的建议只会将进程发送到后台(它仍然存在,消耗内存)。你需要做以上这些才能真正扼杀这个过程
正如用户@ffeast所评论的,这里有an open ipdb issue,并建议了一些解决方法。对我来说,这些很管用:
kill %1
(或任何工号)ipdb> import os; os._exit(1)
这是IPython 5.1中的一个错误。它已在this pull request中修复,不再是IPython 5.2及以后版本的问题。现在可以使用
q
、quit()
,或Ctrl+d退出调试器。相关问题 更多 >
编程相关推荐