如何用VSCode debug Python文件

发布于:2024-05-24 ⋅ 阅读:(140) ⋅ 点赞:(0)

诸神缄默不语-个人CSDN博文目录

需求:我其实一般都用print大法来“调试”程序,但是有时对于机械性比较强但是又有些复杂的程序,还是debug比较方便。
debug功能我之前用过NetBeans和eclipse,应该可以明显看出来我是Java转Python党。

总体来说debug功能就是打断点,然后开始debug,debug的过程中每到断点就会停一下(也可以设置报error的时候停)。

VSCode关于debug的文档:https://code.visualstudio.com/docs/editor/debugging
这篇文档里面有很多比较复杂的内容我就没在博文里面写了,我就写了一点常用的。

1. 设置debug参数

如果直接运行Python代码的话也不用设置参数。如果需要的话,点击这个:
在这里插入图片描述
会自动生成.vscode\launch.json(也可以用别的文件):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File with Arguments",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

也可以在这里打开:
在这里插入图片描述

选择debug环境:
在这里插入图片描述

Python Debugger: Current File with Arguments是configuration标识符,如果需要设置命令行入参就增加:"args": ["-arg1","value1","-arg2","value2"](如果运行Python的命令是python example.py -arg1 value1 -arg2 value2就这么写,明白了吗?),如果需要改运行路径就增加:"cwd":"D:\\PythonCode\\lab_code\\parse_paper_to_parts\\pptp"

我是Windows所以必须要用双斜杠,因为会转义你们明白吧。当然也可以用这种platform-specific属性,看你们有没有这种需求:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/node_modules/gulp/bin/gulpfile.js",
      "args": ["myFolder/path/app.js"],
      "windows": {
        "args": ["myFolder\\path\\app.js"]
      }
    }
  ]
}

2. 设置断点

直接在Python编辑器最左边按左键就可以:
在这里插入图片描述

debug时运行会在红点位置暂停

打了哪些断点也可以在debug栏看到:
在这里插入图片描述

3. 开始debug

进入方式一:
在这里插入图片描述
在这里插入图片描述
(这里选的就是第一节设置的configuration)

进入方式二:直接选
在这里插入图片描述

4. 本文撰写过程中参考的其他网络资料

  1. vscode python debugging arguments-掘金

网站公告

今日签到

点亮在社区的每一天
去签到