# 添加函数
print # 打印
# 断言:用于判断一个表达式,在表达式条件为 false 的时候触发异常
assert expression [, arguments]
# 上下两种表达式相互等价
if not expression:
raise AssertionError(arguments)
一、添加函数
二、vscode调试介绍链接
利用launch.json文件带参调试Debugging configurations for Python apps in Visual Studio Code
三、bito ai 插件
四、Logging中的日志
Python实用教程系列——Logging日志模块 - 知乎 (zhihu.com)
五、pdb:python debugger
(一)事前调试
python 调试工具 pdb 的基本用法(Python Debugger)_python pdb-CSDN博客
1、非侵入式调试
终端输入:python -m pdb filename.py
输出了当前代码运行的位置,同时等待下一步指令
(Pdb) help
Documented commands (type help <topic>):
========================================
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
Miscellaneous help topics:
==========================
exec pdb
(Pdb)
2、侵入式调试
import pdb
pdb.set_trace()
3、在指定文件的指定位置,break命令设置断点
(1)只有一个py文件:
首先在前面的任意一行设置 pdb.set_trace() ,使得程序停下来。
终端(Pdb)后面输入break line(行数例27)
(2)多个py文件
初始的 pdb.set_trace() 设置在main.py中。
(Pdb)break filename:line
如果初始断点与目标断点不在同一个目录下的文件中,也可以通过相对路径下的文件名设置断点
(二)事后调试
终端python -i test.py -i 选项可以让程序结束后打开一个交互式shell
程序结束后出现了 >>> 符号,即python调试器
输入import pdb pdb.pm()
#其中 pdb.pm() 用于程序发生异常导致奔溃后的事后调试,可以跟踪异常程序最后的堆在信息。可以追踪到导致程序奔溃的语句。