vscode

  1. {
  2. // 使用 IntelliSense 了解相关属性。
  3. // 悬停以查看现有属性的描述。
  4. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "Python: 当前文件",
  9. "type": "python",
  10. "request": "launch",
  11. "program": "${file}",
  12. "console": "integratedTerminal"
  13. },
  14. {
  15. "name": "Python: startup.py",
  16. "type": "python",
  17. "request": "launch",
  18. "program": "${workspaceFolder}/startup.py",
  19. "args" : ["--port", "1593"],#传递参数
  20. "env":{"CUDA_VISIBLE_DEVICES":"2"},# 环境变量
  21. },
  22. ]
  23. }

下面是这些字段的说明:
1.name: 当前DEBUG配置的名称。可以具体python文件名,也可以 “name”: “Python: 当前文件”,直接在调试python 文件下点击调试功能。
2.Type: 指什么语言。
3.request是最重要的参数,它能选择两种类型,一个是launch模式,一个是attach模式:
launch模式:由VS Code来启动一个独立的具有debug功能的程序。
attach模式:监听一个已启动的程序(其必须已经开启debug模式)。
大多数情况下,调试Python都是用launch模式。少数情况下,你无法通过新建独立程序来调试(如要与浏览器相结合的程序,launch模式会导致你大部分浏览器插件失效),这时候就需要attach模式。
4.program: 文件的绝对路径,一般不需要改动。
5.console: 终端的类型, integratedTerminal 指使用vscode终端。
6.”args”: [“—quiet”, “—norepeat”, “—port”, “1593”],
dict={‘quiet’,’port’:’1593’,”norepeat”}
7.python#
python 路径,可以更改绝对路径。
Full path that points to the Python interpreter to be used for debugging.
If not specified, this setting defaults to the interpreter identified in thepython.pythonPathsetting, which is equivalent to using the value${config:python.pythonPath}. To use a different interpreter, specify its path instead in thepythonproperty of a debug configuration.
Alternately, you can use a custom environment variable that’s defined on each platform to contain the full path to the Python interpreter to use, so that no additional folder paths are needed.
If you need to pass arguments to the Python interpreter, you can use thepythonArgsproperty.
8.pythonArgs#
Specifies arguments to pass to the Python interpreter using the syntax”pythonArgs”: [““, ““,…].

9.env
设置和修改系统变量或环境变量。例如:”env”:{“CUDA_VISIBLE_DEVICES”:”2”},//使用哪块显卡
Sets optional environment variables for the debugger process beyond system environment variables, which the debugger always inherits. The values for these variables must be entered as strings.
https://code.visualstudio.com/docs/python/debugging

10
“program”: “${file}”, file当前文件
“program”: “${workspaceFolder}/train.py”, # 指定运行文件 #workspaceFolder 项目路径

{
    "name": "Python: 当前文件",  # debug 命名
    "type": "python",  # 语言类型
    "request": "launch",   # debug方式
    "program": "${workspaceFolder}/train.py", # 指定运行文件
    "console": "integratedTerminal", # 控制终端
    "args":["--batch-size","2","--epochs","10","--img-size","160"], 
    # args 参数
    "env" :{"CUDA_VISIBLE_DEVICES":"1"}, # 环境变量
    "python":"/path/anaconda3/envs/th1.8/bin/python3.7", # python  脚本位置
},

pycharm

pdb

https://docs.python.org/3.8/library/pdb.html
https://www.jianshu.com/p/8e5fb5fe0931
https://www.cnblogs.com/xiaohai2003ly/p/8529472.html

方法

python -m pdb main.py ,
python main.py ,main.py ,import pdb ,pdb.set_trance()
1. 单步执行代码,通过命令 python -m pdb xxx.py 启动脚本,进入单步执行模式

pdb命令行:
1)进入命令行Debug模式,python -m pdb xxx.py
2)h:(help)帮助
3)w:(where)打印当前执行堆栈
4)d:(down)执行跳转到在当前堆栈的深一层(个人没觉得有什么用处)
5)u:(up)执行跳转到当前堆栈的上一层
6)b:(break)添加断点
b 列出当前所有断点,和断点执行到统计次数
b line_no:当前脚本的line_no行添加断点
b filename:line_no:脚本filename的line_no行添加断点
b function:在函数function的第一条可执行语句处添加断点
7)tbreak:(temporary break)临时断点
在第一次执行到这个断点之后,就自动删除这个断点,用法和b一样
8)cl:(clear)清除断点
cl 清除所有断点
cl bpnumber1 bpnumber2… 清除断点号为bpnumber1,bpnumber2…的断点
cl lineno 清除当前脚本lineno行的断点
cl filename:line_no 清除脚本filename的line_no行的断点
9)disable:停用断点,参数为bpnumber,和cl的区别是,断点依然存在,只是不启用
10)enable:激活断点,参数为bpnumber
11)s:(step)执行下一条命令
如果本句是函数调用,则s会执行到函数的第一句
12)n:(next)执行下一条语句
如果本句是函数调用,则执行函数,接着执行当前执行语句的下一条。
13)r:(return)执行当前运行函数到结束
14)c:(continue)继续执行,直到遇到下一条断点
15)l:(list)列出源码
l 列出当前执行语句周围11条代码
l first 列出first行周围11条代码
l first second 列出first—second范围的代码,如果second 16)a:(args)列出当前执行函数的函数
17)p expression:(print)输出expression的值
18)pp expression:好看一点的p expression
19)run:重新启动debug,相当于restart
20)q:(quit)退出debug
21)j lineno:(jump)设置下条执行的语句函数
只能在堆栈的最底层跳转,向后重新执行,向前可直接执行到行号
22)unt:(until)执行到下一行(跳出循环),或者当前堆栈结束
23)condition bpnumber conditon,给断点设置条件,当参数condition返回True的时候bpnumber断点有效,否则 bpnumber断点无效