[python] the use of argparse module, the use of argparse in Pycharm

1 Introduction

The argparse module is provided in the Python standard library as aCommand line parsing module, which allows users to input parameters in a manner similar to Unix/Linux command parameters, and parses the parameters into Python variables, so that users can process parameters more quickly.


2. Steps to use

1) Import the argparse module and create an interpreter

import argparse

# 创建解释器
parser = argparse.ArgumentParser(description="可写可不写,只是在命令行参数出现错误的时候,随着错误信息打印出来。")

2) Add the required parameters

parser.add_argument('-gf', '--girlfriend', choices=['jingjing', 'lihuan'])
# -gf 代表短选项,在命令行输入-gf和--girlfriend的效果是一样的,作用是简化参数输入
#--girlfriend 代表完整的参数名称,可以尽量做到让人见名知意,需要注意的是如果想通过解析后的参数取出该值,必须使用带--的名称
# choices 代表输入参数的只能是这个choices里面的内容,其他内容则会保错

parser.add_argument('--house', type=int, default=0)
# --house 代表参数名称
# type  代表输入的参数类型,从命令行输入的参数,默认是字符串类型
# default 代表如果该参数不输入,则会默认使用该值

parser.add_argument('food')
# 该种方式则要求必须输入该参数
# 输入该参数不需要指定参数名称,指定反而报错,解释器会自动将输入的参数赋值给food

3) Parsing parameters

# 进行参数解析
args = parser.parse_args() 
print('args-----:',args)
print('gf-------:', args.girlfriend)
print('food-----:', args.food)

The result of running in the terminal:

(base) enzo@enzo-MBP test % python main.py apple -gf jingjing      
args-----: Namespace(girlfriend='jingjing', house=0, food='apple')
gf-------: jingjing
food-----: apple

3. Description of other parameters

Parameter: action

Pass in parameters to add_argumentthe method action=‘store_true’/‘store_false’, and parse it out to be the bool type parameter True/False. The specific rules are:

  • store_true: If this parameter is not specified, its value is False by default; if this parameter is specified, this parameter is set to True
  • store_false: If this parameter is not specified, its value is True by default; if this parameter is specified, this parameter is set to False
import argparse

parser = argparse.ArgumentParser(description="description")

parser.add_argument('--pa','-a',action='store_true')
parser.add_argument('--pb','-b',action="store_false")
>> python test.py
Namespace(pa=False, pb=True)

>> python test.py -a -b
Namespace(pa=True, pb=False)

If both action and default are given for this parameter, if this parameter is not specified, the value of default shall prevail; if this parameter is specified, the value of action shall prevail.

import argparse

parser = argparse.ArgumentParser(description="description")

parser.add_argument('--pa','-a',action='store_true')
parser.add_argument('--pb','-b',action="store_true",default=True)
parser.add_argument('--pc','-c',action="store_true",default=False)

parser.add_argument('--pd','-d',action='store_false')
parser.add_argument('--pe','-e',action="store_false",default=True)
parser.add_argument('--pf','-f',action="store_false",default=False)

args = parser.parse_args()
print(args)

>> python test.py
Namespace(pa=False, pb=True, pc=False, pd=True, pe=True, pf=False)

>> python test.py -a -b -c -d -e -f
Namespace(pa=True, pb=True, pc=True, pd=False, pe=False, pf=False)

4. Use pycharm to pass parameters to argparse

In the above operation, we pass parameters to the python program through the command line, which is generally suitable for training models in GPU and other linux operating systems.
However, it is very inconvenient when we debug the algorithm. There is no way to use the compiler to debug, so the next step is to introduce how to pass parameters to argparse in Pycharm, so that pycharm can run such python programs.

1. As shown in the figure below, click Edit configurations
insert image description here

2. Add the required parameters in Parameters (only write parameters), and separate multiple parameters with spaces.
insert image description here
3. Finally, run directly. You can see that the parameters we set in Parameters will be filled during Run Complete

insert image description here

Guess you like

Origin blog.csdn.net/weixin_37804469/article/details/129160274