python在命令行调试的两种简单方法
下载debugpy包
pip install debuugpy
添加launch.json
一般都直接生成后改改就可以用
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python 调试程序: 当前文件",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 5678
}
// "program": "${file}",
// "console": "integratedTerminal"
}
]
}
设置断点
在需要调试的位置添加断点,也就是小红点
方法一
直接在源代码添加以下代码
import debugpy
debugpy.listen(("localhost", 5678))
print("Waiting for debugger attach...")
debugpy.wait_for_client()
# debugpy.breakpoint()
print("Debugger attached.")
# 你的程序代码
然后正常该怎么用命令行就怎么用命令行,比如以下:python test.py
方法二
修改命令行,不修改源代码,直接把命令行变为:python -m debugpy --listen 5678 --wait-for-client test.py
就可以了。
正常两种都能用。推荐不知道怎么改命令行的可以直接用第一种办法(可以ai),保持端口一致即可。
这里仅仅是学习、记录、分享,欢迎相互交流。