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

时间:2021-03-28 20:48:50

I'm not sure if what I'm asking is possible at all, but since python is an interpreter it might be. 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. You can't just look up the documentation on the var's type since you can't be sure what type it is. 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). I could do this by changing the code and then re-running the program each time but that would be much slower.

我不确定我的问题是否可行,但由于python是一个解释器,它可能是。我正在尝试在开源项目中进行更改,但因为python中没有类型,所以很难知道变量作为数据和它们的作用。您不能只查找var类型的文档,因为您无法确定它的类型。我想放到终端,这样我就可以通过输入help(var)或print(var)来快速检查变量的类型和它们的作用。我可以通过更改代码然后每次重新运行程序来做到这一点,但这会慢得多。

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. 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. Then I type 'a=7', and some 'continue' command, and the program goes on to print 7, not 5, because I changed it.

my_debug_shell是我要问的函数。它会让我进入python解释器的'>>> shell,在那里我可以输入help(a),它会告诉我a是一个整数。然后我输入'a = 7'和一些'continue'命令,程序继续打印7而不是5,因为我改变了它。

5 个解决方案

#1


40  

http://docs.python.org/library/pdb.html

import pdb
pdb.set_trace()

#2


27  

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')

#3


9  

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. Two advantages of this approach are:

一行部分解决方案只是将1/0放在您想要断点的地方:这将引发一个异常,它将被调试器捕获。这种方法的两个优点是:

  • 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; one can instead directly insert "breakpoints" where needed.

    一个不需要在每个要调试的程序中导入pdb;可以在需要的地方直接插入“断点”。

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. When the 1/0 is reached, the program exits, but variables can be inspected as usual with the pdb debugger (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.

为了自动捕获异常,你可以简单地执行python -m pdb prog.py ...然后键入c(ontinue)以启动程序。当达到1/0时,程序退出,但可以像往常一样使用pdb调试器(p my_var)检查变量。现在,这不允许您修复问题并继续运行程序。相反,您可以尝试修复错误并再次运行程序。

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. Alternatively, you can do everything from within the IPython shell:

如果你想使用强大的IPython shell,ipython -pdb prog.py ...会做同样的事情,但会导致IPython更好的调试器界面。或者,您可以在IPython shell中执行所有操作:

  • In IPython, set up the "debug on exception" mode of IPython (%pdb).
  • 在IPython中,设置IPython(%pdb)的“异常调试”模式。

  • Run the program from IPython with %run prog.py…. When an exception occurs, the debugger is automatically activated and you can inspect variables, etc.
  • 使用%run prog.py运行来自IPython的程序....发生异常时,调试器会自动激活,您可以检查变量等。

The advantage of this latter approach is that (1) the IPython shell is almost a must; and (2) once it is installed, debugging can easily be done through it (instead of directly through the pdb module). The full documentation is available on the IPython pages.

后一种方法的优点是(1)IPython shell几乎是必须的; (2)安装完成后,可以通过它轻松完成调试(而不是直接通过pdb模块)。 IPython页面上提供了完整的文档。

#4


2  

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()调用通常一样快。

#5


0  

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”调试器(谷歌自己),此外您还可以根据需要添加日志记录和调试输出。

#1


40  

http://docs.python.org/library/pdb.html

import pdb
pdb.set_trace()

#2


27  

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')

#3


9  

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. Two advantages of this approach are:

一行部分解决方案只是将1/0放在您想要断点的地方:这将引发一个异常,它将被调试器捕获。这种方法的两个优点是:

  • 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; one can instead directly insert "breakpoints" where needed.

    一个不需要在每个要调试的程序中导入pdb;可以在需要的地方直接插入“断点”。

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. When the 1/0 is reached, the program exits, but variables can be inspected as usual with the pdb debugger (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.

为了自动捕获异常,你可以简单地执行python -m pdb prog.py ...然后键入c(ontinue)以启动程序。当达到1/0时,程序退出,但可以像往常一样使用pdb调试器(p my_var)检查变量。现在,这不允许您修复问题并继续运行程序。相反,您可以尝试修复错误并再次运行程序。

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. Alternatively, you can do everything from within the IPython shell:

如果你想使用强大的IPython shell,ipython -pdb prog.py ...会做同样的事情,但会导致IPython更好的调试器界面。或者,您可以在IPython shell中执行所有操作:

  • In IPython, set up the "debug on exception" mode of IPython (%pdb).
  • 在IPython中,设置IPython(%pdb)的“异常调试”模式。

  • Run the program from IPython with %run prog.py…. When an exception occurs, the debugger is automatically activated and you can inspect variables, etc.
  • 使用%run prog.py运行来自IPython的程序....发生异常时,调试器会自动激活,您可以检查变量等。

The advantage of this latter approach is that (1) the IPython shell is almost a must; and (2) once it is installed, debugging can easily be done through it (instead of directly through the pdb module). The full documentation is available on the IPython pages.

后一种方法的优点是(1)IPython shell几乎是必须的; (2)安装完成后,可以通过它轻松完成调试(而不是直接通过pdb模块)。 IPython页面上提供了完整的文档。

#4


2  

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()调用通常一样快。

#5


0  

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”调试器(谷歌自己),此外您还可以根据需要添加日志记录和调试输出。