Python:error: the following arguments are required: action
python parse_args() 报错 xxx.py: error: the following arguments are required: xxx报错形如:usage: test_1.py [-h] aaatest_1.py: error: the following arguments are required: aaa原因:args分为可选参数和必选参数。–指定可选参数,不加–指定
python parse_args() 报错 xxx.py: error: the following arguments are required: xxx
报错形如:
usage: test_1.py [-h] aaa
test_1.py: error: the following arguments are required: aaa
原因:
args分为可选参数和必选参数。–指定可选参数,不加–指定的是必选参数。
上述报错的原因是:定义参数aaa时,没有采用 – 的方式,导致相应的参数为必须手动指定的参数。此时即使通过default设置默认参数,也还是会报错
报错的代码:
import argparse
parser = argparse.ArgumentParser(description=“ddd”)
parser.add_argument(“aaa”, type=str, default=‘bbb’, help=‘ccc’) # 参数前面没有加–表示是必须手动指定值的参数,并且不能使用默认值
args = parser.parse_args()
print(args.aaa)
正确的代码:把其中一句改为:
parser.add_argument("–aaa", type=str, default=‘bbb’, help=‘ccc’) # 加–,此时可以使用默认值bbb
正确的输出:
bbb
Process finished with exit code 0
其他相关内容:argparse.ArgumentParser args AttributeError: ‘Namespace‘ object has no attribute ‘xxx‘
参考:
https://docs.python.org/zh-cn/3/howto/argparse.html
dest 指定API名称,即args.xxx中的xxx (注意:必选参数不要再指定dest,否则会提示重复指定
更多推荐
所有评论(0)