如何修复“错误:需要以下参数:-i / - image”

问题描述 投票:0回答:1

我正在查看其他人的代码并遵循python代码;

import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the input image")
args = vars(ap.parse_args())

在最后一行给出以下错误;

usage: sample.py [-h] -i IMAGE
sample.py: error: the following arguments are required: -i/--image

我该如何解决这个问题?到目前为止,我没有尝试过什么似乎有所帮助。

python-3.x argparse
1个回答
2
投票

运行sample.py时,需要指定-i / --image参数:

python sample.py --image image/cat.png

如果您希望image参数是可选的,请删除required=True

ap.add_argument("-i", "--image", help="path to the input image")
© www.soinside.com 2019 - 2024. All rights reserved.