mistake
When using the python script command to pass parameters to argparse, the following error was reported:
error: ununrecognized arguments: True
The script command and argparse code are as follows (for the convenience of explanation, it is simplified, and only the part where the error occurs is kept):
argparse code:
parser.add_argument('--resume', action='store_true', default=False, help='continue to train the model')
Order:
python train.py --resume True
solution
Give the solution to this error first, it is actually very simple, just remove True
python train.py --resume
At this time, the value of the parameter resume parsed in the python code is also True:
args = parser.parse_args()
print(args.resume) # True
Expansion: usage of action in argparse
The reason and principle of the problem are introduced as follows:
In the add_argument function of argparse, it contains many parameters, such as:
- default: the default value of the parameter
- type: parameter type
- help: parameter description
, etc.
In the above add_argument statement, a parameter action of the function is specified, indicating the default state of the keyword (resume). There are two default states: store_true
andstore_false
- If action is
store_true
, its value is False when the parameter resume is not specified in the command line - If action is
store_false
, its value is True when the parameter resume is not specified on the command line
Expressed with two examples:
parser.add_argument('--resume', action='store_true')
# 如果命令行语句为: python train.py -resume -> 此时resume 为True
# 如果命令行语句为: python train.py -> 此时resume 为False
parser.add_argument('--resume', action='store_false')
# 如果命令行语句为: python train.py -resume -> 此时resume 为False
# 如果命令行语句为: python train.py -> 此时resume 为True
It can be understood that the action will be triggered only if this parameter is specified on the command line. True or false is stored in the action. After the action is triggered, the stored value is assigned to resume, otherwise the value stored in the action is assigned the inverse of the value. .
If action is combined with default:
parser.add_argument('--resume', action='store_true', default='false') # 与不加default一样
# 如果命令行语句为: python train.py -resume -> 此时resume 为True
# 如果命令行语句为: python train.py -> 此时resume 为False
parser.add_argument('--resume', action='store_true', default='true') # 结果永远为True
# 如果命令行语句为: python train.py -resume -> 此时resume 为True
# 如果命令行语句为: python train.py -> 此时resume 为True
It can be understood that when an action is triggered, a value will be assigned according to the result of the trigger, but if no action is triggered, the priority of default will be greater than the inversion of the value stored in the action, and the value will be assigned according to the value of default
Extension: another way of passing parameters
In addition to using action, you can also directly pass the value of bool type, just specify the parameter type as bool in add_argument
parser.add_argument("--resume", type=bool, default=True)
The specified resume that needs to be displayed on the command line is True or False
python train.py --resume True
When getting parameters, it is the same as getting str or int types, and you can get the value of the parameters directly args.resume
by
The current new version of argparse should support the bool type. When someone used it on the Internet before, it does not support the bool type. When parsing the parameters, the value parsed by resume is "True" of the character type. If this problem occurs, you can upgrade argparse , or cast the parsed value:
bool(args.resume)
reference
https://docs.python.org/zh-cn/3/howto/argparse.html
https://wenku.baidu.com/view/5b4c94fbcd2f0066f5335a8102d276a200296020.html
https://blog.csdn.net/zeronose/article/details/122276418
https://blog.csdn.net/qq_35140742/article/details/120607823