如何在程序中启动python控制台(以便于调试)?

时间:2021-11-12 02:38:17

After years of research programming in Matlab, I miss the way I could pause a program mid-execution and inspect the variables, do plotting, save/modify data, etc. via the interactive console, and then resume execution.

经过Matlab多年的研究编程,我想念我可以在执行中暂停程序并检查变量,通过交互式控制台绘制,保存/修改数据等,然后恢复执行。

Is there a way to do the same thing in python?

有没有办法在python中做同样的事情?

For example:


   # ... 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 ...

This would make debugging a lot easier. Suggestions much appreciated, thanks!

这将使调试变得更容易。建议非常感谢,谢谢!

5 个解决方案

#1


6  

Use the pdb library.

使用pdb库。

I have this line bound to <F8> in Vim:

我在Vim中将此行绑定到 :

import pdb; pdb.set_trace()

That will drop you into a pdb console.

这会让你进入一个pdb控制台。

The pdb console isn't quite the same as the standard Python console… But it will do most of the same stuff. Also, in my ~/.pdbrc, I've got:

pdb控制台与标准的Python控制台并不完全相同......但它会做大部分相同的事情。另外,在我的〜/ .pdbrc中,我得到了:

alias i from IPython.Shell import IPShellEmbed as IPSh; IPSh(argv='')()

So that I can get into a "real" iPython shell from pdb with the i command:

这样我就可以使用i命令从pdb进入一个“真正的”iPython shell:

(pdb) i
...
In [1]:

#2


4  

The excellent solution I found was to use the 'code' module. I can now call 'DebugKeyboard()' from anywhere in my code and the interpreter prompt will pop-up, allowing me to examine variables and run code. CTRL-D will continue the program.

我发现的优秀解决方案是使用“代码”模块。我现在可以从我的代码中的任何地方调用'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"

#3


3  

The code module contains classes for bringing up a REPL.

代码模块包含用于启动REPL的类。

#4


2  

Check out the Python debugger. In short, you can insert

查看Python调试器。简而言之,您可以插入

import pdb; pdb.set_trace()

at any point in your program that you want to debug. (Note that you should remove these in release versions!)

在程序中您想要调试的任何位置。 (请注意,您应该在发行版中删除这些!)

#5


1  

pdb is what you're looking for - just put a call to pdb.set_trace() wherever you want to drop into an debugger.

pdb是你正在寻找的 - 只需要调用pdb.set_trace(),无论你想放入调试器。

#1


6  

Use the pdb library.

使用pdb库。

I have this line bound to <F8> in Vim:

我在Vim中将此行绑定到 :

import pdb; pdb.set_trace()

That will drop you into a pdb console.

这会让你进入一个pdb控制台。

The pdb console isn't quite the same as the standard Python console… But it will do most of the same stuff. Also, in my ~/.pdbrc, I've got:

pdb控制台与标准的Python控制台并不完全相同......但它会做大部分相同的事情。另外,在我的〜/ .pdbrc中,我得到了:

alias i from IPython.Shell import IPShellEmbed as IPSh; IPSh(argv='')()

So that I can get into a "real" iPython shell from pdb with the i command:

这样我就可以使用i命令从pdb进入一个“真正的”iPython shell:

(pdb) i
...
In [1]:

#2


4  

The excellent solution I found was to use the 'code' module. I can now call 'DebugKeyboard()' from anywhere in my code and the interpreter prompt will pop-up, allowing me to examine variables and run code. CTRL-D will continue the program.

我发现的优秀解决方案是使用“代码”模块。我现在可以从我的代码中的任何地方调用'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"

#3


3  

The code module contains classes for bringing up a REPL.

代码模块包含用于启动REPL的类。

#4


2  

Check out the Python debugger. In short, you can insert

查看Python调试器。简而言之,您可以插入

import pdb; pdb.set_trace()

at any point in your program that you want to debug. (Note that you should remove these in release versions!)

在程序中您想要调试的任何位置。 (请注意,您应该在发行版中删除这些!)

#5


1  

pdb is what you're looking for - just put a call to pdb.set_trace() wherever you want to drop into an debugger.

pdb是你正在寻找的 - 只需要调用pdb.set_trace(),无论你想放入调试器。