最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中调试Python代码

Python crifan 11012浏览 0评论

通过:

初学python,pycharm和Spyder哪个好? – 知乎

推荐一下python编程软件? – 知乎

-》

Python – Visual Studio Marketplace

Python 笔记一:简单入门及点评 – 简书

现在去试试:

VSCode中去看看python代码的调试效果如何

现在去拿之前在PyCharm中调试开发的一个Python项目:

去试试,VSCode中:

如何调试,看看效果如何

之前好像是已经安装了对应的Python插件的:

vscode debug python

Debugging Python with Visual Studio Code

Python in Visual Studio Code

“activate it using the Python: Select Interpreter command”

应该先去设置Python解析器

Get Started Tutorial with Python in Visual Studio Code

点击左下角的Python 2.7.10,可以看到列出当前检测到的多个版本的Python解析器

使用VS Code调试Python代码 – PlyTools

VSCode基础使用+VSCode调试python程序入门 – CSDN博客

vscode python设置debug ? – 知乎

使用vscode调试python代码 – CNode技术社区

此处就是用的python 2,所以无须切换python解析器,就用默认检测到的:

Python 2.7.10

即可。

默认配置:

<code>{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}"
    },
    {
      "name": "Python: Attach",
      "type": "python",
      "request": "attach",
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "${workspaceFolder}",
      "port": 3000,
      "secret": "my_secret",
      "host": "localhost"
    },
    {
      "name": "Python: Terminal (integrated)",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    },
    {
      "name": "Python: Terminal (external)",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "externalTerminal"
    },
    {
      "name": "Python: Django",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/manage.py",
      "args": [
        "runserver",
        "--noreload",
        "--nothreading"
      ],
      "debugOptions": [
        "RedirectOutput",
        "Django"
      ]
    },
    {
      "name": "Python: Flask (0.11.x or later)",
      "type": "python",
      "request": "launch",
      "module": "flask",
      "env": {
        "FLASK_APP": "app.py"
      },
      "args": [
        "run",
        "--no-debugger",
        "--no-reload"
      ]
    },
    {
      "name": "Python: Module",
      "type": "python",
      "request": "launch",
      "module": "module.name"
    },
    {
      "name": "Python: Pyramid",
      "type": "python",
      "request": "launch",
      "args": [
        "${workspaceFolder}/development.ini"
      ],
      "debugOptions": [
        "RedirectOutput",
        "Pyramid"
      ]
    },
    {
      "name": "Python: Watson",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/console.py",
      "args": [
        "dev",
        "runserver",
        "--noreload=True"
      ]
    },
    {
      "name": "Python: All debug Options",
      "type": "python",
      "request": "launch",
      "pythonPath": "${config:python.pythonPath}",
      "program": "${file}",
      "module": "module.name",
      "env": {
        "VAR1": "1",
        "VAR2": "2"
      },
      "envFile": "${workspaceFolder}/.env",
      "args": [
        "arg1",
        "arg2"
      ],
      "debugOptions": [
        "RedirectOutput"
      ]
    }
  ]
}
</code>

就只是生成了和刚才看到的一样的配置:

<code>    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}"
    },
</code>

选择配置:

直接点击运行,结果terminal输出log:

不是debug。

右键:

也没看到如何添加断点的。

也点击了行首,但是无法点击断点:

点击Breakpoint的+去新增断点:

但是好像要自己输入函数?

很麻烦啊。

先后两次点击运行后的log是:

<code>➜  saveLocalData git:(master) ✗ &gt;....honTools/visualstudio_py_launcher.py /Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData 56456 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData/saveDataToMongodb.py
crifanLib: Can not found lib chardet
crifanLib: Can not found lib BeautifulSoup
Imported: crifanLib,    v5.5
2018/06/12 05:59:34 LINE 1163 INFO    wbOut=&lt;openpyxl.workbook.workbook.Workbook object at 0x104020e10&gt;
2018/06/12 05:59:34 LINE 1168 INFO    wsOutStorybook=&lt;Worksheet "\u7ed8\u672c"&gt;, wsOutSong=&lt;Worksheet "\u513f\u6b4c"&gt;
2018/06/12 05:59:34 LINE 359  INFO    initOutputExcelHeaderStyle: wbOut=&lt;openpyxl.workbook.workbook.Workbook object at 0x104020e10&gt;
2018/06/12 05:59:34 LINE 405  INFO    initOutputExcelHeaders: [绘本] ws=&lt;Worksheet "\u7ed8\u672c"&gt;
2018/06/12 05:59:34 LINE 405  INFO    initOutputExcelHeaders: [儿歌] ws=&lt;Worksheet "\u513f\u6b4c"&gt;
Traceback (most recent call last):
  File "/Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData/saveDataToMongodb.py", line 1176, in &lt;module&gt;
    wbIn = load_workbook(ExcelFullFilename)
  File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/openpyxl/reader/excel.py", line 174, in load_workbook
    archive = _validate_archive(filename)
  File "/Users/crifan/Library/Python/2.7/lib/python/site-packages/openpyxl/reader/excel.py", line 121, in _validate_archive
    archive = ZipFile(filename, 'r', ZIP_DEFLATED)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 756, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: u'/Users/crifan/dev/dev_root/xxx/\u6570\u636e/FromMaggie/\u82f1\u8bed\u7ed8\u672c\u8d44\u6e902018.4.26.xlsx'
➜  saveLocalData git:(master) ✗ cd /Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" python /Users/crifan/.vscode/extensions/ms-python.python-2018.5.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData 57402 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData/.vscode/launch.json
  File "/Users/crifan/dev/dev_root/xxx/projects/mongodb/saveLocalData/.vscode/launch.json", line 2
    // Use IntelliSense to learn about possible attributes.
     ^
SyntaxError: invalid syntax
</code>

关于添加断点,参考:

https://code.visualstudio.com/docs/languages/python

的Debugging的视频,才看到,原来是行首的空白处,鼠标移动山去可以看到红色点,点击后,即可添加断点:

然后清空了log后,点击debug去调试看看:

注意到,底部有个:

自动附加 默认是 关

看起来,很是不错啊

简单易用的感觉。

然后单步试试

试了试 但不执行

(结果Mac中F11是隐藏所有窗口,所以不得不点击按钮试了试)

结果发现此处的F11,是进入函数内部的单步执行

即:Step Into的效果

注意到:

鼠标移动上去后,显示:

自动附加

的含义了:在调试模式下自动附加到Node.js进程

此处调试Python的话,鼠标移动到对应变量或函数,显示对应的信息:

继续单步:

不过通过鼠标移动到调试按钮栏中发现了:

  • F5:Continue 继续运行 -》直到运行完毕或者遇到断点停下来

  • F10:Step Over

  • F11:Step Into

  • Shift+F11:Step Out

  • Shift+Command+F5:Restart

  • Shift+F5:Stop

总体来说,效果还不错的。

【总结】

VSCode中调试Python的话:

1.先打开当前Python文件

2.左下角选择Python版本(Python解析器)

3.左边切换到(和项目文件列表同级)调试tab

4.点击左上角的调试右边的绿色箭头,在下来列表中选择:添加设置

5.会自动打开launch.json文件,并添加出一个:

Python Current File

之类的配置,配置内容是:

<code>    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}"
    },
</code>

6.保存后,关闭launch.json,打开要调试的Python文件,然后去调试:

F5 或 调试->启动调试(F5)

启动调试即可

7.可以根据需要去添加断点

点击每行之前的line number的左边的那一列,即可出现红色点点,左下角的Breakpoints也会出现对应的新的断点。

转载请注明:在路上 » 【已解决】VSCode中调试Python代码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.183 seconds, using 22.15MB memory