Vscode lanuch.json

Intro

使用launch.json

能够方便的运行需要传很多参数的代码文件

如下:

import math
import argparse  # 1、导入argpase包


def parse_args():
    parse = argparse.ArgumentParser(description='Calculate cylinder volume')  # 2、创建参数对象
    parse.add_argument('--radius', default=2, type=int, help='Radius of Cylinder')  # 3、往参数对象添加参数
    parse.add_argument('--height', default=4, type=int, help='height of Cylinder')
    args = parse.parse_args()  # 4、解析参数对象获得解析对象
    return args


def cal_vol(radius, height):
    vol = math.pi * pow(radius, 2) * height
    return vol


if __name__ == '__main__':
    args = parse_args()
    print(cal_vol(args.radius, args.height))  # 5、使用解析对象.参数获取使用命令行参数

运行上述代码的命令是

test1.py --radius 2 --height 3

创建json

那如何写在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 Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "/home/hu/Projects/test",
            "args": [
                "--radius",
                "2",
                "--height",
                "2",

            ]
        }
    ]
}

然后

这样运行即可

具体的创建步骤如下:

在debugger这里

点击create a launch json file即可

创建好之后

对应输入参数

和路径

运行

然后就可以运行了

action

传入参数就会被赋值为True

不传入就是False 

store参数的作用是

可以直接args.pa调用传入的参数

Debug

改为false:

然后就可以进入非本项目的代码(pip 库等)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值