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 就可以了

相关推荐
GEO_ai_zhijian6 分钟前
饮料生产线质量检测系统哪家好
大数据·人工智能·python
尼恩久36 分钟前
记录xls表格提取信息
python
工艺资源1 小时前
外贸工艺品设计参考平台众多,哪家专业值得了解
人工智能·python
无糖冰可乐211 小时前
安装wsl2,并链接Windows上的vscode运行项目
pytorch·vscode·python·pip
汤米粥1 小时前
Python爬虫中Xpath用法之——normalize-space()
开发语言·python
想会飞的蒲公英1 小时前
回归模型怎样评估:MAE、MSE、RMSE 和 R²
人工智能·python·机器学习·数据分析·回归
henrylin99992 小时前
Quant Agent 完整系统规格
python·量化
三川6982 小时前
Tkinter库的学习记录10-Message与Messagebox
python