环境配置
配置 python 环境
准备一段 python 代码
from __future__ import print_function def sum_nums(n): s=0 for i in range(n): s += i print(s) if __name__ == '__main__': sum_nums(5)
然后在左侧运行和调试按钮中,点击“创建launch.json”文件,选择 python文件(如果没有的话需要先安装 python 扩展,在应用中搜索 python 第一个安装了最多的即可)
选择 python 文件
生成默认的 launch 文件如下
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}这里我们需要自定义指定一下用到的 python 版本,需要添加 “pythonPath” 选项
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"pythonPath": "/home/lthpc/anaconda3/bin/python3.7",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}这样的话就可以使用指定的 python 运行代码了
如果说用到了 conda 虚拟环境,则需要找到虚拟环境对应的 python 路径,可以使用 whereis python 查看
调试代码
配置好调试环境后,在代码中打上断点,然后点击运行调试和执行按钮,即可进入调试页面









