当前位置: 首页 > 工具软件 > PySnooper > 使用案例 >

python debug 调试工具 pysnooper

乌璞瑜
2023-12-01

安装

pip install pysnooper 

定义1个函数,查看调试

import pysnooper


@pysnooper.snoop()  # 调试装饰器
def fun1(a):
    a += 1
    a += 2
    return a


fun1(0)
C:\Python38\python.exe C:/test.py
Source path:... C:/test.py
Starting var:.. a = 0
16:23:41.958115 call         5 def fun1(a):
16:23:41.959113 line         6     a += 1
Modified var:.. a = 1
16:23:41.959113 line         7     a += 2
Modified var:.. a = 3
16:23:41.959113 line         8     return a
16:23:41.959113 return       8     return a
Return value:.. 3
Elapsed time: 00:00:00.000998

把调试的内容写入debug.log文件

import pysnooper


@pysnooper.snoop(output='debug.log')  # 调试装饰器
def fun1(a):
    a += 1
    a += 2
    return a


fun1(0)

查看多个函数,有层级关系

import pysnooper


@pysnooper.snoop()  # 调试装饰器
def fun1(a):
    a += 1
    a += 2
    return a


@pysnooper.snoop()  # 调试装饰器
def fun2(a):  # 要调试的函数
    b = fun1(a)
    b += 3
    b += 4


fun2(0)  # 运行函数

输出

C:\Python38\python.exe C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
Source path:... C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
Starting var:.. a = 0
16:34:39.240023 call        12 def fun2(a):  # 要调试的函数
16:34:39.240023 line        13     b = fun1(a)
    Source path:... C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
    Starting var:.. a = 0
    16:34:39.240023 call         5 def fun1(a):
    16:34:39.240023 line         6     a += 1
    Modified var:.. a = 1
    16:34:39.240023 line         7     a += 2
    Modified var:.. a = 3
    16:34:39.240023 line         8     return a
    16:34:39.240023 return       8     return a
    Return value:.. 3
    Elapsed time: 00:00:00.000000
New var:....... b = 3
16:34:39.240023 line        14     b += 3
Modified var:.. b = 6
16:34:39.240023 line        15     b += 4
Modified var:.. b = 10
16:34:39.240023 return      15     b += 4
Return value:.. None
Elapsed time: 00:00:00.000000

Process finished with exit code 0

给debug信息添加前缀

import pysnooper


@pysnooper.snoop(prefix="fun1 ")  # 调试装饰器
def fun1(a):
    a += 1
    a += 2
    return a


@pysnooper.snoop(prefix="fun2 ")  # 调试装饰器
def fun2(a):  # 要调试的函数
    b = fun1(a)
    b += 3
    b += 4


fun2(0)  # 运行函数

输出

C:\Python38\python.exe C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
fun2 Source path:... C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
fun2 Starting var:.. a = 0
fun2 16:41:36.065410 call        12 def fun2(a):  # 要调试的函数
fun2 16:41:36.065410 line        13     b = fun1(a)
fun1     Source path:... C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
fun1     Starting var:.. a = 0
fun1     16:41:36.065410 call         5 def fun1(a):
fun1     16:41:36.065410 line         6     a += 1
fun1     Modified var:.. a = 1
fun1     16:41:36.065410 line         7     a += 2
fun1     Modified var:.. a = 3
fun1     16:41:36.065410 line         8     return a
fun1     16:41:36.065410 return       8     return a
fun1     Return value:.. 3
fun1     Elapsed time: 00:00:00.000000
fun2 New var:....... b = 3
fun2 16:41:36.065410 line        14     b += 3
fun2 Modified var:.. b = 6
fun2 16:41:36.065410 line        15     b += 4
fun2 Modified var:.. b = 10
fun2 16:41:36.065410 return      15     b += 4
fun2 Return value:.. None
fun2 Elapsed time: 00:00:00.000000

Process finished with exit code 0

设置跟踪函数的深度(默认深度为1,只调试当前函数)

import pysnooper


def fun1(a):
    a += 1
    a += 2
    return a


@pysnooper.snoop(depth=2)  # 调试深度2层
def fun2(a):
    b = fun1(a)
    b += 3
    b += 4


fun2(0)  # 运行函数
C:\Python38\python.exe C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
Source path:... C:/Users/xiahuadong/Desktop/xiahuadongCode/algorithm_AI/wav2Lip/test.py
Starting var:.. a = 0
16:48:13.839873 call        11 def fun2(a):
16:48:13.839873 line        12     b = fun1(a)
    Starting var:.. a = 0
    16:48:13.839873 call         4 def fun1(a):
    16:48:13.839873 line         5     a += 1
    Modified var:.. a = 1
    16:48:13.839873 line         6     a += 2
    Modified var:.. a = 3
    16:48:13.839873 line         7     return a
    16:48:13.839873 return       7     return a
    Return value:.. 3
New var:....... b = 3
16:48:13.840870 line        13     b += 3
Modified var:.. b = 6
16:48:13.840870 line        14     b += 4
Modified var:.. b = 10
16:48:13.840870 return      14     b += 4
Return value:.. None
Elapsed time: 00:00:00.000997

Process finished with exit code 0

设置最大的输出长度,

默认情况下,PySnooper 输出的变量和异常信息,如果超过 100 个字符,被会截断为 100 个字符。

@pysnooper.snoop(max_variable_length=200)

也可以使用max_variable_length=None它从不截断它们

支持多线程调试模式

@pysnooper.snoop(thread_info=True)
 类似资料: