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

相关推荐
江南十四行21 小时前
网络编程基础:TCP/IP与Socket编程详解
网络·python·http
神明93121 小时前
mysql索引排序规则设置方法_mysqlCollation对索引影响
jvm·数据库·python
神明93121 小时前
CSS如何实现打字机效果_利用animation与宽度变化
jvm·数据库·python
2303_8212873821 小时前
bootstrap如何实现平滑滚动到页面顶部
jvm·数据库·python
ftpeak21 小时前
AI开发之LangGraph教程4~记忆 (Memory)
python·ai·langchain·langgraph
2301_8125396721 小时前
Tailwind CSS如何设置不同断点的内边距_使用p-4 md-p-8类.txt
jvm·数据库·python
m0_5967490921 小时前
CSS实现动态悬浮菜单位置_JS计算配合CSS绝对定位
jvm·数据库·python
2301_8125396721 小时前
golang如何实现最小堆定时器_golang最小堆定时器实现总结
jvm·数据库·python
lyc87801 天前
【Qwen3.5-2B-Base】本地模型部署和验证联动千帆api
大数据·python
m0_690825821 天前
检测三位随机数中重复数字的Python实现方法
jvm·数据库·python