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

相关推荐
Yan-英杰9 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
多想和从前一样5 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
小喵要摸鱼6 小时前
【Pytorch 库】自定义数据集相关的类
pytorch·python
bdawn6 小时前
深度集成DeepSeek大模型:WebSocket流式聊天实现
python·websocket·openai·api·实时聊天·deepseek大模型·流式输出
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt
鸡啄米的时光机7 小时前
vscode的一些实用操作
vscode·学习
mengyoufengyu7 小时前
算法12-贪心算法
python·算法·贪心算法