11 Python Ipdb Debug简单使用

Python Ipdb Debug简单使用

参考链接:

https://xmfbit.github.io/2017/08/21/debugging-with-ipdb/

https://blog.csdn.net/u014015972/article/details/51705292

https://wulc.me/2018/12/21/ipdb 使用小记/

1 安装

pip install ipdb

2 使用

ipdb 的使用方法一般有两种:集成到源代码通过命令交互

1、集成到源代码:(不推荐)

import ipdb
var1 = 23
ipdb.set_trace()
var2 = 23
# some code

上面的代码会在执行完 var1 = 23 这条语句之后停止,然后进入 debug 环境

2、通过命令交互(推荐)

通过 python -m ipdb your_code.py 来运行你要调试的代码,即可进入 debug 环境。

# 如下,便是我 debug 的一个片段
ipdb> b 583                                                                                             
Breakpoint 4 at /root/FSL/DPGN/main.py:583
ipdb> c                                                                                                 
> /root/FSL/DPGN/main.py(583)<module>()
    581 
    582 if __name__ == '__main__':
4-> 583     main()

下面给大家介绍一些常用的命令:

# 1 帮助命令:键入 h 即可查看所有命令,键入 h <command> 即可查看 <command> 的作用,如下:
ipdb> h                                                                                                 

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

Miscellaneous help topics:
==========================
exec  pdb

ipdb> h p                                                                                               
p expression
        Print the value of the expression.

# 2 执行下一条语句:键入 s 即可进入函数内部(若下一条语句为调用一个函数),键入 n 即执行下一条语句

# 3 打断点:键入 b line_num 的方式即可在本文件的line_num行打上断点, 键入 b file_name:line_num 即可给指定的文件打上断点,如下:

# 4 一直执行直到遇到下一个断点:键入 c 执行代码直到遇到某个断点或程序执行完毕

# 5 打印某个变量的值:键入 p <Variables>

# 6 查看某一变量的类型: 键入 whatis <Variables>

# 7 查看调试到哪里:键入 w
    
# 8 退出:键入 q

# 9 查看所有断点:键入 b

# 10 禁用某一断点:键入 disable <breakpoint_num>(断点号)

# 11 启用某一断点:键入 enable <breakpoint_num>(断点号)

# 12 删除某一断点:键入 cl <breakpoint_num>(断点号) or cl <filename:lineno>(断点行号)

# 13 删除所有断点:键入 cl

后续会继续补充,未完,待续……(11.08)

posted @ 2021-10-29 02:29  SethDeng  阅读(172)  评论(0编辑  收藏  举报