Why does argparse only work if the python interpreter is called explicitly?

戏子无情 提交于 2019-12-10 21:44:51

问题


I have the python interpreter location in my path environment variable so that I don't need to explicitly call the python interpreter from the command line. However, when I use the argparse module to read command line arguments, it only works if I explicitly call the python interpreter.

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('w')

cmd_args = parser.parse_args()

print(cmd_args.w)

when I don't explicitly call the interpreter, this occurs:

C:\Users\nheme\Desktop> command_line_parse.py test_argument
usage: command_line_parse.py [-h] w
command_line_parse.py: error: the following arguments are required: w

When I explicitly call the interpreter, the code works as expected:

C:\Users\nheme\Desktop> python command_line_parse.py test_argument
test_argument

Why do I need to explicitly call the interpreter?

来源:https://stackoverflow.com/questions/50440889/why-does-argparse-only-work-if-the-python-interpreter-is-called-explicitly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!