简体   繁体   English

我可以在正在运行的Python程序中放置一个断点,该程序可以放到交互式终端吗?

[英]Can I put a breakpoint in a running Python program that drops to the interactive terminal?

I'm not sure if what I'm asking is possible at all, but since python is an interpreter it might be. 我不确定我问的是否可能,但是因为python可能是一个解释器。 I'm trying to make changes in an open-source project but because there are no types in python it's difficult to know what the variables have as data and what they do. 我正在尝试在开源项目中进行更改,但因为python中没有类型,所以很难知道变量作为数据和它们的作用。 You can't just look up the documentation on the var's type since you can't be sure what type it is. 您不能只查找var类型的文档,因为您无法确定它是什么类型。 I want to drop to the terminal so I can quickly examine the types of the variables and what they do by typing help(var) or print(var). 我想放到终端,这样我就可以通过输入help(var)或print(var)来快速检查变量的类型和它们的作用。 I could do this by changing the code and then re-running the program each time but that would be much slower. 我可以通过更改代码然后每次重新运行程序来执行此操作,但这会慢得多。

Let's say I have a program: 假设我有一个程序:

def foo():
    a = 5
    my_debug_shell()
    print a

foo()

my_debug_shell is the function I'm asking about. my_debug_shell是我要问的函数。 It would drop me to the '>>>' shell of the python interpreter where I can type help(a), and it would tell me that a is an integer. 它会让我进入python解释器的'>>> shell,在那里我可以输入help(a),它会告诉我a是一个整数。 Then I type 'a=7', and some 'continue' command, and the program goes on to print 7, not 5, because I changed it. 然后我输入'a = 7'和一些'continue'命令,程序继续打印7而不是5,因为我改变了它。

Here is a solution that doesn't require code changes: 这是一个不需要更改代码的解决方案:

python -m pdb prog.py <prog_args>
(pdb) b 3
Breakpoint 1 at prog.py:3
(pdb) c
...
(pdb) p a
5
(pdb) a=7
(pdb) ...

In short: 简而言之:

  • start your program under debugger control 在调试器控制下启动程序
  • set a break point at a given line of code 在给定的代码行设置一个断点
  • let the program run up to that point 让程序运行到那一点
  • you get an interactive prompt that let's you do what you want (type 'help' for all options) 你得到一个交互式提示,让你做你想做的事情(为所有选项输入'help')

A one-line partial solution is simply to put 1/0 where you want the breakpoint: this will raise an exception, which will be caught by the debugger. 单行部分解决方案只是将1/0放在您想要断点的地方:这将引发一个异常,它将被调试器捕获。 Two advantages of this approach are: 这种方法的两个优点是:

  • Breakpoints set this way are robust against code modification (no dependence on a particular line number); 以这种方式设置的断点对于代码修改是强健的(不依赖于特定的行号);

  • One does not need to import pdb in every program to be debugged; 一个不需要在每个要调试的程序中import pdb ; one can instead directly insert "breakpoints" where needed. 可以在需要的地方直接插入“断点”。

In order to catch the exception automatically, you can simply do python -m pdb prog.py… and then type c (ontinue) in order to start the program. 为了自动捕获异常,你可以简单地执行python -m pdb prog.py…然后键入c (ontinue)以启动程序。 When the 1/0 is reached, the program exits, but variables can be inspected as usual with the pdb debugger ( p my_var ). 当达到1/0时,程序退出,但可以像往常一样使用pdb调试器( p my_var )检查变量。 Now, this does not allow you to fix things and keep running the program. 现在,这不允许您修复问题并继续运行程序。 Instead you can try to fix the bug and run the program again. 相反,您可以尝试修复错误并再次运行程序。

If you want to use the powerful IPython shell, ipython -pdb prog.py… does the same thing, but leads to IPython's better debugger interface. 如果你想使用强大的IPython shell, ipython -pdb prog.py…做同样的事情,但会导致IPython更好的调试器界面。 Alternatively, you can do everything from within the IPython shell: 或者,您可以在IPython shell中执行所有操作:

  • In IPython, set up the "debug on exception" mode of IPython ( %pdb ). 在IPython中,设置IPython的“debug on exception”模式( %pdb )。
  • Run the program from IPython with %run prog.py… . 使用%run prog.py…来自IPython的程序%run prog.py… When an exception occurs, the debugger is automatically activated and you can inspect variables, etc. 发生异常时,调试器会自动激活,您可以检查变量等。

The advantage of this latter approach is that (1) the IPython shell is almost a must; 后一种方法的优点是(1)IPython shell几乎是必须的; and (2) once it is installed, debugging can easily be done through it (instead of directly through the pdb module). (2)安装完成后,可以通过它轻松完成调试(而不是直接通过pdb模块)。 The full documentation is available on the IPython pages. IPython页面上提供了完整的文档

Python 3.7 has a new builtin way of setting breakpoints. Python 3.7有一种新的内置方法来设置断点。

breakpoint()

The implementation of breakpoint() will import pdb and call pdb.set_trace() . breakpoint()的实现将import pdb并调用pdb.set_trace()

Remember to include the braces () , since breakpoint is a function, not a keyword. 请记住包括大括号() ,因为breakpoint是一个函数,而不是关键字。

You can run the program using pdb, and add breakpoints before starting execution. 您可以使用pdb运行程序,并在开始执行之前添加断点。

In reality though, it's usually just as fast to edit the code and put in the set_trace() call, as another user stated. 实际上,正如另一个用户所说,编辑代码和放入set_trace()调用通常一样快。

Not sure what the real question is. 不确定真正的问题是什么。 Python gives you the 'pdb' debugger (google yourself) and in addition you can add logging and debug output as needed. Python为您提供了'pdb'调试器(谷歌自己),此外您还可以根据需要添加日志和调试输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM