pdb:完成后跳过重启

问题描述

使用

python -m pdb -c "c" script.py

发生问题时进入调试模式。从doc中,我发现选项-c "c"(Python 3.2+)使我每次在程序启动时都可以点击 c + Enter

但是,当程序正常完成时,它会输出 The program finished and will be restarted和 我仍然必须按 q + Enter 退出程序。 有没有一种方法可以跳过呢?

解决方法

您可以按顺序为-c添加多个命令。

方法1:仅在未遇到任何错误时退出

您可以只给出另一个命令q来跳出pdb模式,以防遇到错误。但是,如果遇到错误,它将进入调试模式,在该模式下,您将不得不继续按c并进入前进。

python -mpdb -c "c" -c "q" script.py

没有遇到错误(立即退出!)-

(base) $ python -mpdb -c "c" -c "q" script.py
The program finished and will be restarted
(base) $ 

遇到错误(进入调试模式!)-

(base) $ python -mpdb -c "c" -c "q" script.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/pdb.py",line 1701,in main
    pdb._runscript(mainpyfile)
  File "/anaconda3/lib/python3.7/pdb.py",line 1570,in _runscript
    self.run(statement)
  File "/anaconda3/lib/python3.7/bdb.py",line 585,in run
    exec(cmd,globals,locals)
  File "<string>",line 1,in <module>
  File "/Projects/Random/script.py",line 6,in <module>
    """
ModuleNotFoundError: No module named 'thispackagedoesntexist'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
Post mortem debugger finished. The script.py will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) 

方法2:不管是否出错都退出

您可以使用echo "q",并通过以下方式使用pdb将其传递给下一个(|)命令。这将运行第二个命令一次,并立即获取echo "q"的输出以退出-

echo "q" | python -mpdb -c "c" script.py

程序在调试模式下运行脚本后,将显示q。遇到(或未遇到)错误后,调试会自动退出。

没有遇到错误(立即退出!)-

(base) $ echo "q" | python -mpdb -c "c" script.py
The program finished and will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) (base) $ 

遇到错误(立即退出!)-

(base) $ echo "q" | python -mpdb -c "c" script.py
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/pdb.py",in <module>
  File /Projects/Random/script.py",in <module>
    """
ModuleNotFoundError: No module named 'thispackagedoesntexist'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) Post mortem debugger finished. The script.py will be restarted
> /Projects/Random/script.py(6)<module>()
-> """
(Pdb) 
(base) $ 

以下是可与pdb一起使用的命令列表-

(Pdb) help

Documented commands (type help <topic>):
========================================
EOF    c          d        h         list      q        rv       undisplay
a      cl         debug    help      ll        quit     s        unt
alias  clear      disable  ignore    longlist  r        source   until
args   commands   display  interact  n         restart  step     up
b      condition  down     j         next      return   tbreak   w
break  cont       enable   jump      p         retval   u        whatis
bt     continue   exit     l         pp        run      unalias  where
,

您可以简单地使用- python3 -mpdb -c "q" -c "c" example.py

Link

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...