Python中的argparse用法

2021-10-25 10:46:40 字數 1969 閱讀 2816

2 正文

3 結語

[1] argparse基本用法,

[2] argparse簡要用法總結,

最近在看python**的時候看到了別人使用

import argparse
import argparse

defmain()

: parser = argparse.argumentparser(

"demo of argparse"

) parser.add_argument(

'--epochs'

,type

=int

, default=50,

help

="number of epochs"

) parser.add_argument(

'--lr'

,type

=float

, default=

0.01

,help

='learn rate'

) parser.add_argument(

'--weights_path'

,type

=str

, default=

"weights/yolo.weights"

,help

="path to weights file"

) args = parser.parse_args(

)print

(args)

epochs = args.epochs

lr = args.lr

weights_path = args.weights_path

print

("your model's path of weights is {}"

.format

(weights_path)

)print

("the number of epoch is"

, epochs,

"and learning rate ="

, lr)

if __name__ ==

'__main__'

: main(

)

直接在終端執行的結果(使用預設default引數)

python test.py
輸出結果:

namespace(epochs=50, lr=0.01, weights_path=『weights/yolo.weights』)

your model』s path of weights is weights/yolo.weights

the number of epoch is 50 and learning rate = 0.01

檢視幫助:

python test.py -h
輸出結果就不放了,會顯示parser.add_argument中對於每個引數的help內容和parser = argparse.argumentparser()中的內容。

也可以更改部分預設引數,終端輸入以下內容得到更改部分引數的結果:

python test.py --epochs 200 --lr 0.5
輸出結果:

namespace(epochs=200, lr=0.5, weights_path=『weights/yolo.weights』)

your model』s path of weights is weights/yolo.weights

the number of epoch is 200 and learning rate = 0.5

如有錯誤,歡迎各位大佬進行指正!

python 命令列解析工具argparse的認識

一 介紹 argparse 是python 中用於解析命令列引數和選項的標準模組。簡單的形容就是你寫完python程式之後,在終端下 linux系統 可以用命令列直接呼叫執行,並且可以設定相應的引數等等。二 如何使用 說太多廢話無用,我們需要掌握的是如何使用,能看懂別人寫的 即可。太多的理論知識作用...

Python 命令列解析工具 Argparse介紹

最近在研究pathon的命令列解析工具,argparse,它是python標準庫中推薦使用的編寫命令列程式的工具。以前老是做ui程式,今天試了下命令列程式,感覺相當好,不用再花大把時間去研究介面問題,尤其是vc 中尤其繁瑣。現在用python來實現命令列,核心計算模組可以用c自己寫擴充套件庫,效果挺...

python中 python中的 與

這一部分首先要理解python記憶體機制,python中萬物皆物件。對於不可變物件,改變了原來的值,其別名 變數名 繫結到了新值上面,id肯定會改變 對於可變物件,操作改變了值,id肯定會變,而 是本地操作,其值原地修改 對於 號操作,可變物件和不可變物件呼叫的都是 add 操作 對於 號操作,可變...