Python 如何退出 pdb 并允许程序继续?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17820618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:15:36  来源:igfitidea点击:

How to exit pdb and allow program to continue?

pythonpdb

提问by turtle

I'm using the pdb module to debug a program. I'd like to understand how I can exit pdb and allow the program to continue onward to completion. The program is computationally expensive to run, so I don't want to exit without the script attempting to complete. continuedoesn't seems to work. How can I exit pdb and continue with my program?

我正在使用 pdb 模块来调试程序。我想了解如何退出 pdb 并允许程序继续完成。该程序的运行计算成本很高,因此我不想在脚本未尝试完成的情况下退出。continue似乎不起作用。如何退出 pdb 并继续我的程序?

采纳答案by voithos

continueshould "Continue execution, only stop when a breakpoint is encountered", so you've got a breakpoint set somewhere. To remove the breakpoint (if you inserted it manually):

continue应该“继续执行,只在遇到断点时停止”,所以你已经在某处设置了一个断点。要删除断点(如果您手动插入):

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /path/to/test.py:5
(Pdb) clear 1
Deleted breakpoint 1
(Pdb) continue

Or, if you're using pdb.set_trace(), you can try this (although if you're using pdb in more fancy ways, this may break things...)

或者,如果你正在使用pdb.set_trace(),你可以试试这个(尽管如果你以更奇特的方式使用 pdb,这可能会破坏事情......)

(Pdb) pdb.set_trace = lambda: None  # This replaces the set_trace() function!
(Pdb) continue
# No more breaks!

回答by Steve Barnes

If you really wish to exit the debugger then you need to run something like WinPdbwhich allows you to detach from the process and then exit the debugger, (N.B. It is multi-platform).

如果你真的想退出调试器,那么你需要运行类似WinPdb 的东西,它允许你从进程中分离出来,然后退出调试器,(注意它是多平台的)。

If you would like to continue debugging but no longer stop at a given breakpoint then you need to:

如果您想继续调试但不再在给定的断点处停止,那么您需要:

  1. Make a note of the breakpoint number, (or the file and line number),
  2. Either cl bp_numberorclear file:lineto permanently remove the breakpoint ordisable pb_numberto toggle it off but be able to toggle it back.
  3. Then continueand your program run until then next differentbreakpoint is hit.
  1. 记下断点号(或文件和行号),
  2. 无论是cl bp_numberclear file:line永久删除断点disable pb_number切换它关闭,但可以切换回来。
  3. 然后continue你的程序运行直到下一个不同的断点被命中。

For more detail on the above see the manual.

有关上述内容的更多详细信息,请参阅手册

回答by Minh Triet

A simple Ctrl-Dwill break out of pdb. If you want to continue rather than breaking, just press crather than the whole continuecommand

一个简单的Ctrl-D将突破 pdb。如果你想继续而不是中断,只需按下c而不是整个continue命令

回答by Adam Spiers

The answerfrom @voithos is correct, so I'll just add one alternative in the case where you are using set_trace. Yes, the pdb.set_trace = lambda: Nonehack works OK, but not if you have other breakpoints set and want to reenable it later on. To me this points to the fact that unfortunately pdbis missing a bunch of functionality (even basic stuff like display lists), and this is another case.

@voithos的答案是正确的,所以我只会在您使用set_trace. 是的,pdb.set_trace = lambda: Nonehack 可以正常工作,但如果您设置了其他断点并希望稍后重新启用它,则不行。对我来说,这表明不幸的pdb是缺少一堆功能(甚至是显示列表之类的基本内容),这是另一种情况。

The good news is that pdb++is a great drop-in replacement for pdb, and one of the things it solves is exactly the problem of disabling set_trace. So you can simply do:

好消息是这pdb++是一个很好的替代品pdb,它解决的问题之一就是禁用set_trace. 所以你可以简单地做:

pip install pdbpp

and then at the (Pdb++)prompt, type:

然后在(Pdb++)提示符下键入:

pdb.disable()

If you want to reenable later, unsurprisingly this works:

如果你想稍后重新启用,不出所料,这有效:

pdb.enable()

Easy! And you will get lots of other useful goodies on top of that.

简单!除此之外,您还将获得许多其他有用的好东西。

回答by BrainAtom

find new way to exit the pdb without install anything: - when the program starts to run, press ctrl+c, then switch the window to another(any window), then the original shell with pdb running should show something like: (pdb) ..... - switch back to pdb, then press Enter, now you are all set, pdb command shell reappear again

找到无需安装任何东西即可退出 pdb 的新方法: - 当程序开始运行时,按 ctrl+c,然后将窗口切换到另一个(任何窗口),然后运行 ​​pdb 的原始 shell 应显示如下内容:(pdb) ..... - 切换回pdb,然后回车,现在你都设置好了,pdb命令shell再次出现