Pass in parameters to the python file through the command line (used by the argparse.ArgumentParser() module)



import argparse

def parse_args():
    parser = argparse.ArgumentParser("Evaluation Parameters")
    parser.add_argument(
        '--image_dir',
        type=str,
        default='./insects/test/images',
        help='the directory of test images')
    parser.add_argument(
        "--digit",
        type=int,
        default=123,
        help="输入数字")
    args = parser.parse_args()
    return args


args = parse_args()


print(args.image_dir)
print("..................")
print(args.digit)


The first step in using argparse is to create an ArgumentParser object. The argparse.ArgumentParser module is called in the parse_args() method defined above as follows

parser = argparse.ArgumentParser("Evaluation Parameters"),

The second step is to add the name and default value of the parameter variable under the module, as follows

parser.add_argument(
        '--image_dir',
        type=str,
        default='./insects/test/images',
        help='the directory of test images')

At this time, running directly is to print the default value, as follows,
Insert picture description here
and then we use the command line to test, enter the new parameters and pay attention to the following format (remember, enter the directory where the file is located, not too much)

python cs.py --digit 456

The result is as follows, you can see that it has been changed, the default number is
Insert picture description here
summarized! Must remember the first two-ah!!!

Guess you like

Origin blog.csdn.net/weixin_43134049/article/details/105881505