将参数传递给 python 应用程序?
Pass arguments to python application?
我有一个 Python3 应用程序,我想使用 Ubuntu 18.04 终端向它传递一些参数:
python3 app/app.py -af app/annotation_file.json -em app/model/ -output_dir app/new_model/ -n_iter 100
我正在尝试使用 argparse 执行此操作:
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-af", "--annotation_file",
dest="main", default=None,
help="Path to annotated file")
parser.add_argument("-em", "--existing_model_path",
dest="main", default=None,
help="Retrain existing model or train from scratch")
parser.add_argument("-output_dir", "--output_dir_to_save_model",
dest="main", default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n_iter", "--num_iter",
dest="main", default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
main(args)
输出:
Namespace(main='100')
Namespace(main='100')
但我没有得到输出。
如何将上述所有命名参数传递给我的 main()
函数?
好的,所以,您正在将参数传递给函数,但是当您的每一行解析器都将输入数据保存到:
dest="main"
你的主变量在每一个新的 parser.add_argument 行都会被覆盖。
如果您的 main() 函数有四个参数,请更改每个 parser.add_argument() 行的 dest="" 行以指向不同的变量,然后调用您的 main() 函数:
main(args.arg1, args.arg2, args.arg3, args.arg4)
其中 arg-n 是您新创建的每个不同变量。
示例,给定您的代码:
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-af", "--annotation_file",
dest="arg1", default=None,
help="Path to annotated file")
parser.add_argument("-em", "--existing_model_path",
dest="arg2", default=None,
help="Retrain existing model or train from scratch")
parser.add_argument("-output_dir", "--output_dir_to_save_model",
dest="arg3", default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n_iter", "--num_iter",
dest="arg4", default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
main(args.arg1, args.arg2, args.arg3, args.arg4)
我会清理您的解析器定义(我删除了 dest
,并修复了短选项标志):
parser = ArgumentParser()
parser.add_argument("-a", "--af", "--annotation_file",
help="Path to annotated file")
parser.add_argument("-e", "--em", "--existing_model_path",
help="Retrain existing model or train from scratch")
parser.add_argument(+-o", "--output_dir", "--output_dir_to_save_model",
default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n", "--n_iter", "--num_iter",
default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
现在您将在 namespace
中看到所有 4 个参数,并且可以通过名称访问它们
args.n_iter
args.output_dir
通常 dest
取自其中一个双破折号标志。您最终会将 args
传递给 main()
这一事实与 argparse
无关。您通过向所有 4 个参数提供相同的 dest
搞砸了解析器,结果您只看到一个的值。
如文档所述,您可以使用
轻松获得字典版本
vars(args)
https://docs.python.org/3/library/argparse.html#dest
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character.
我有一个 Python3 应用程序,我想使用 Ubuntu 18.04 终端向它传递一些参数:
python3 app/app.py -af app/annotation_file.json -em app/model/ -output_dir app/new_model/ -n_iter 100
我正在尝试使用 argparse 执行此操作:
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-af", "--annotation_file",
dest="main", default=None,
help="Path to annotated file")
parser.add_argument("-em", "--existing_model_path",
dest="main", default=None,
help="Retrain existing model or train from scratch")
parser.add_argument("-output_dir", "--output_dir_to_save_model",
dest="main", default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n_iter", "--num_iter",
dest="main", default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
main(args)
输出:
Namespace(main='100')
Namespace(main='100')
但我没有得到输出。
如何将上述所有命名参数传递给我的 main()
函数?
好的,所以,您正在将参数传递给函数,但是当您的每一行解析器都将输入数据保存到:
dest="main"
你的主变量在每一个新的 parser.add_argument 行都会被覆盖。
如果您的 main() 函数有四个参数,请更改每个 parser.add_argument() 行的 dest="" 行以指向不同的变量,然后调用您的 main() 函数:
main(args.arg1, args.arg2, args.arg3, args.arg4)
其中 arg-n 是您新创建的每个不同变量。
示例,给定您的代码:
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-af", "--annotation_file",
dest="arg1", default=None,
help="Path to annotated file")
parser.add_argument("-em", "--existing_model_path",
dest="arg2", default=None,
help="Retrain existing model or train from scratch")
parser.add_argument("-output_dir", "--output_dir_to_save_model",
dest="arg3", default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n_iter", "--num_iter",
dest="arg4", default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
main(args.arg1, args.arg2, args.arg3, args.arg4)
我会清理您的解析器定义(我删除了 dest
,并修复了短选项标志):
parser = ArgumentParser()
parser.add_argument("-a", "--af", "--annotation_file",
help="Path to annotated file")
parser.add_argument("-e", "--em", "--existing_model_path",
help="Retrain existing model or train from scratch")
parser.add_argument(+-o", "--output_dir", "--output_dir_to_save_model",
default="/app/model",
help="Path to save newly generated NER model")
parser.add_argument("-n", "--n_iter", "--num_iter",
default=100,
help="Number of iterations to train the model")
args = parser.parse_args()
print(args)
现在您将在 namespace
中看到所有 4 个参数,并且可以通过名称访问它们
args.n_iter
args.output_dir
通常 dest
取自其中一个双破折号标志。您最终会将 args
传递给 main()
这一事实与 argparse
无关。您通过向所有 4 个参数提供相同的 dest
搞砸了解析器,结果您只看到一个的值。
如文档所述,您可以使用
轻松获得字典版本vars(args)
https://docs.python.org/3/library/argparse.html#dest
For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character.