Python SystemExit:调用 parse_args() 时出现 2 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42249982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SystemExit: 2 error when calling parse_args()
提问by Haik
I'm learning basics of Python and got already stuck at the beginning of Argparse tutorial. I'm getting the following error:
我正在学习 Python 的基础知识,并且在 Argparse 教程的开头就陷入了困境。我收到以下错误:
import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
usage: __main__.py [-h] echo
__main__.py: error: unrecognized arguments: -f
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
a %tb command gives the following output:
%tb 命令提供以下输出:
SystemExit Traceback (most recent call last)
<ipython-input-16-843cc484f12f> in <module>()
----> 1 args = parser.parse_args()
C:\Users\Haik\Anaconda2\lib\argparse.pyc in parse_args(self, args, namespace)
1702 if argv:
1703 msg = _('unrecognized arguments: %s')
-> 1704 self.error(msg % ' '.join(argv))
1705 return args
1706
C:\Users\Haik\Anaconda2\lib\argparse.pyc in error(self, message)
2372 """
2373 self.print_usage(_sys.stderr)
-> 2374 self.exit(2, _('%s: error: %s\n') % (self.prog, message))
C:\Users\Haik\Anaconda2\lib\argparse.pyc in exit(self, status, message)
2360 if message:
2361 self._print_message(message, _sys.stderr)
-> 2362 _sys.exit(status)
2363
2364 def error(self, message):
SystemExit: 2
How could I fix this problem? Thanks
我该如何解决这个问题?谢谢
回答by daphtdazz
argparse
is a module designed to parse the arguments passed from the command line, so for example if you type the following at a command prompt:
argparse
是一个旨在解析从命令行传递的参数的模块,例如,如果您在命令提示符下键入以下内容:
$ python my_programme.py --arg1=5 --arg2=7
You can use argparse
to interpret the --arg1=5 --arg2=7
part. If argparse
thinks the arguments are invalid, it exits, which in general is done in python by calling sys.exit()
which raises the SystemExit
error, which is what you're seeing.
您可以使用argparse
来解释--arg1=5 --arg2=7
零件。如果argparse
认为参数无效,它会退出,这通常是在 python 中通过调用sys.exit()
which 引发SystemExit
错误来完成的,这就是你所看到的。
So the problem is you're trying to use argparse
from an interactive interpreter (looks like ipython), and at this point the programme has already started, so the args should have already been parsed.
所以问题是你试图argparse
从交互式解释器(看起来像 ipython)使用,此时程序已经启动,所以应该已经解析了 args。
To try it properly create a separate python file such as my_programme.py
and run it using python
from a command line, as I illustrated.
要尝试正确地创建一个单独的 python 文件,例如my_programme.py
并从命令行运行它python
,如我所示。
回答by ?ukasz Rogalski
parse_args
method, when it's called without arguments, attempts to parse content of sys.argv
. Your interpreter process had filled sys.argv
with values that does not match with arguments supported by your parser
instance, that's why parsing fails.
parse_args
方法,当它被无参数调用时,会尝试解析sys.argv
. 您的解释器进程填充了sys.argv
与您的parser
实例支持的参数不匹配的值,这就是解析失败的原因。
Try printing sys.argv
to check what arguments was passed to your interpreter process.
尝试打印sys.argv
以检查传递给解释器进程的参数。
Try calling parser.parse_args(['my', 'list', 'of', 'strings'])
to see how parser will work for interpreter launched with different cmdline arguments.
尝试调用parser.parse_args(['my', 'list', 'of', 'strings'])
以查看解析器如何为使用不同 cmdline 参数启动的解释器工作。
回答by johninvirtual
[quick solution] Add an dummy parser argument in the code
[快速解决方案] 在代码中添加一个虚拟解析器参数
parser.add_argument('-f')