Gooey使用错误TypeError: ‘required‘ is an invalid argument for positionals

编程入门 行业动态 更新时间:2024-10-24 16:22:16

Gooey使用<a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误TypeError: ‘required‘ is an invalid argument for positionals"/>

Gooey使用错误TypeError: ‘required‘ is an invalid argument for positionals

Gooey使用问题TypeError: ‘required’ is an invalid argument for positionals

TypeError: ‘required’ is an invalid argument for positionals 的解决方法
当我在使用argparse模块时,遇到了如下错误:

import argparseparser = argparse.ArgumentParser(description = 'debug_example')
parser.add_argument ('--data_root', default = 'data/path', type = str, required=False, help = 'the dataset path')
parser.add_argument ('result_root', default = 'result/path', type = str, required=False, help = 'the output path')
args = parser.parse_args()print(args.data_root)
print(args.result_root)
TypeError: 'required' is an invalid argument for positionals

解决方案:
在’result_root’前面加上- -;

parser.add_argument ('--result_root', default = 'result/path', type = str, required=True, help = 'the output path')

原因:
required = False 只能用于可选参数。 对于可选参数,应该使用 - -,如果没有 - -,python 会将其视为位置参数。 因此 add_argument 函数里的参数required参数对位置参数 ‘result_root’ 无效。

解决错误后:

import argparseparser = argparse.ArgumentParser(description = 'debug_example')
parser.add_argument ('--data_root', default = 'data/path', type = str, required=False, help = 'the dataset path')
parser.add_argument ('--result_root', default = 'result/path', type = str, required=False, help = 'the output path')
args = parser.parse_args()print(args.data_root)
print(args.result_root)

运行结果:
data/path
result/path

参考博客:
TypeError: ‘required’ is an invalid argument for positionals 的解决方法

更多推荐

Gooey使用错误TypeError: ‘required‘ is an invalid argument for positionals

本文发布于:2024-03-13 02:00:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   TypeError   Gooey   required   positionals

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!