VSCode-Python传参数进行Debug

新建demo.py

import argparse
def parse_args():

    description = "debug example"                   
    parser = argparse.ArgumentParser(description=description)        

    help = "The path of address"
    parser.add_argument('--host',help = help)  
    parser.add_argument('--port',help = help)  
    parser.add_argument('--user',help = help)  
    args = parser.parse_args()                                                
    return args

if __name__ == '__main__':
    args = parse_args()

    host = args.host
    port = args.port
    user = args.user
    print([host, port, user])   

    '''
    python demo.py --host 127.0.0.1 --port 22 --user root
    '''

命令行运行 python demo.py --host 127.0.0.1 --port 22 --user root 可以看到输出结果

在vscode点击debug的图标-->create a launch.json file--->python File

初始的json文件如下:

{
    // 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}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Ctrl+shif+p切换python环境并更改./vscode/launch.json如下

{
    // 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}",
            "console": "integratedTerminal",
            "justMyCode": true,

            "env": {
                "CUDA_VISIBLE_DEVICES":"4"  // 设置cuda
            },

            // 添加参数
            "args":[
                "--host", "127.0.0.1",
                "--port", "22",
                "--user","root"
            ]
        }
    ]
}

之后打断点按F5或者Run-->Start Debugging 就可以了

相关推荐
Python之栈5 分钟前
Python if 语句优化技巧
python·算法
姑苏老陈26 分钟前
【Python基础】Python文件处理
开发语言·python·python文件操作
yukai0800834 分钟前
Python 全栈系列271 微服务踩坑记
python·微服务·php
学步_技术1 小时前
Python编码系列—Python工厂方法模式:构建灵活对象的秘诀
开发语言·python·工厂方法模式
秋秋秋叶1 小时前
Python学习——【2.3】for循环
python·学习
会发paper的学渣1 小时前
python 单例模式实现
开发语言·python·单例模式
学步_技术1 小时前
Python编码系列—Python桥接模式:连接抽象与实现的桥梁
开发语言·python·桥接模式
柴华松2 小时前
GPU训练代码
开发语言·python
Echo_Lee02 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#
python之行2 小时前
python 环境问题
开发语言·python