• 技术文章 >后端开发 >Python教程

    vscode如何远程调试python代码

    WBOYWBOY2023-04-30 21:13:05转载38

    环境配置

    配置 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.com/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.com/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 查看

    调试代码

    配置好调试环境后,在代码中打上断点,然后点击运行调试和执行按钮,即可进入调试页面

    以上就是vscode如何远程调试python代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除
    专题推荐:Python VSCode
    上一篇:python在linux哪个文件夹中 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • Python中loguru日志库如何使用• python怎么使用openai生成图像• conda如何配置python虚拟环境• python linecache读取行更新如何实现• Python中的Array模块怎么使用
    1/1

    PHP中文网