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

相关推荐
szial5 分钟前
网络爬虫与 CDP 实战(一):网页数据到底在哪?从 HTTP 到分页采集
网络·爬虫·python·网络爬虫
DanCheng-studio31 分钟前
智能科学与技术毕设易上手题目建议
python·毕业设计·毕设
醇氧34 分钟前
nohup 后台启动 uvicorn(仅测试 / 临时运行)
linux·运维·网络·python·python3.11
2601_957883841 小时前
2026年9月:专业解决雷神笔记本维修难题
python·电脑
Omics Pro2 小时前
预测性虚拟细胞中显式机理算子
大数据·数据库·人工智能·python·算法·机器学习·自然语言处理
秋名RG2 小时前
Java历代LTS版本新特性详解(代码增强版)
java·开发语言·python
27669582922 小时前
youdao/有道翻译APP 算法协议分析
开发语言·前端·python·sign·youdao·有道翻译app算法·有道app协议请求
承渊政道2 小时前
Linux系统学习【线程概念与控制核心知识详解】
linux·运维·vscode·学习·ubuntu·线程创建 终止 等待
夏天拐跑了西瓜10 小时前
一文入门LangChain:从框架认知到构建你的第一个AI Agent
python·langchain·conda·agent
魔猴疯猿10 小时前
从0到1用Python开发第一个智能体
人工智能·python·深度学习·神经网络·机器学习