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

相关推荐
紫雾凌寒29 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋10 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户10 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
一只蜗牛儿10 小时前
Sherpa-ONNX:说话人识别与语音识别自动开启(VAD)+ Python API 完整指南
人工智能·python·语音识别
结衣结衣.10 小时前
【OpenCV】入门教学
图像处理·人工智能·python·opencv