使用sys.argv argparse致电main
我有一个主函数,该功能使用argparse从sys.argv中获取ARG:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-arg1', '--argument1', type=str, required=True)
parser.add_argument('-arg2', '--argument2', type=str, required=True)
args = parser.parse_args()
主函数从另一个模块称为mod.main()。 当我打印传递给主要函数的内容时,args是:[' - arg1','val1','-arg2','val2']
,这是正确的参数列表。
但是,当我运行时,似乎Argparse的ARGS似乎未正确传递。
['-arg1', 'val1', '-arg2', 'val2']
usage: -arg1 [-h] -arg2 ARG2 -arg1 ARG1
-arg1: error: the following arguments are required: -arg1
问题是我没有运行run_module.py,该py从命令行调用module_to_to_call.main(),而是将其传递给Spark-Supparmit以在EMR上运行。
"HadoopJarStep": {
"Args": [
"spark-submit",..., "run_module.py", "module_to_call", "-arg1", "val1", "-arg2", "val2",...
I have a main function which takes args from sys.argv using argparse:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-arg1', '--argument1', type=str, required=True)
parser.add_argument('-arg2', '--argument2', type=str, required=True)
args = parser.parse_args()
The main function is called from another module as mod.main().
When i print what is passed to the main function the args are: ['-arg1', 'val1', '-arg2', 'val2']
which is the correct list of arguments.
However, when i run it it seems that the args are not passed correctly with argparse.
['-arg1', 'val1', '-arg2', 'val2']
usage: -arg1 [-h] -arg2 ARG2 -arg1 ARG1
-arg1: error: the following arguments are required: -arg1
The issue is that i am not running the run_module.py which calls the module_to_call.main() from the command line but passing it as args to spark-submit in order to run it on EMR.
"HadoopJarStep": {
"Args": [
"spark-submit",..., "run_module.py", "module_to_call", "-arg1", "val1", "-arg2", "val2",...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个最小的示例,
您可以使用它运行
并进行
或运行它,而无需参数
以获取帮助消息:
Update
从OP的评论中,我意识到
sys.argv
>在函数main()
函数的途中被某些封底的代码被弄脏,第一个参数被切断了。参数列表
[' - arg1','val1','-arg2','val2']
是不是,因为它缺少> sys.argv [0]
- 从命令行调用Python脚本的名称。由于argparse
processessys.argv [1:]
,它无法找到' - arg1'
,因此失败了错误。在这种情况下,您可以将一个虚拟元素添加到
sys.argv
以确保-arg1
将在代码处时传递到argparse
作为模块运行。您可以使用以下包裹的代码对其进行测试
,并从命令行调用它:
即使
sys.argv,结果也应与直接从命令行调用
将缺少第一个元素:argparse_example
时相同。Here is a minimal example for you
You can run it with
And get
Or run it without arguments
to get the help message:
UPDATE
From comments by the OP I realize that
sys.argv
got mangled on the way to functionmain()
by some enveloping code and the first argument got cut off.The argument list
['-arg1', 'val1', '-arg2', 'val2']
is not the correct list of arguments as it is missingsys.argv[0]
— the name of the python script being called from the command line. Asargparse
processessys.argv[1:]
, it is unable to find'-arg1'
and therefore fails with an error.In this case you can add a dummy element to
sys.argv
to make sure that-arg1
will be passed toargparse
when your code is being run as a module.You can test it with the following enveloping code
And call it from the command line like this:
The result should be the same as when calling
argparse_example
directly from the command line, even thoughsys.argv
will be missing the first element: