How to fix “error: the following arguments are required: -i/--image”

坚强是说给别人听的谎言 提交于 2021-01-07 02:30:05

问题


I was looking at someone else's code and following python code;

import argparse

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

gives the following error on the last line;

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

How can I fix this problem? Nothing I have tried so far seems to help.


回答1:


When running sample.py, you need to specify the -i/--image argument:

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

If you want the image argument to be optional, remove required=True:

ap.add_argument("-i", "--image", help="path to the input image")


来源:https://stackoverflow.com/questions/55531306/how-to-fix-error-the-following-arguments-are-required-i-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!