Argparse-SystemExit:2
Argparse - SystemExit:2
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-p", "--prototxt", required=True,
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
我是运行一个通过OpenCV进行人脸识别的例子。
我此时使用 'argparse',并得到这个错误。
args = vars(ap.parse_args())
来自这段代码。
usage: ipykernel_launcher.py [-h] -i IMAGE -p PROTOTXT -m MODEL
[-c CONFIDENCE]
ipykernel_launcher.py: error: the following arguments are required: -i/--
image, -p/--prototxt, -m/--model
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\Users\user\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
我该如何解决?
这是我的电脑环境,使用的是Jupyter-notebook
- Python: 3.6.4 64 位 [MSC v.1900 64 位 (AMD64)]
- IPython: 6.2.1
- OS: Windows 10 10.0.15063 SP0
- argparse: 1.1
如果不分享您尝试 运行 文件的方式,这很难回答。该错误告诉您它没有找到您 运行 文件时传入的必需参数。
由于您为 -i、-p 和 -m 参数指定了 required = True
,因此您必须始终传递它们,或者如果它们不需要 运行 您的程序,则将它们设为可选。
在 ipython
会话中:
In [36]: import argparse
In [37]: # construct the argument parse and parse the arguments
...: ap = argparse.ArgumentParser()
...: ap.add_argument("-i", "--image", required=True,
...: help="path to input image")
...: ap.add_argument("-p", "--prototxt", required=True,
...: help="path to Caffe 'deploy' prototxt file")
...: ap.add_argument("-m", "--model", required=True,
...: help="path to Caffe pre-trained model")
...: ap.add_argument("-c", "--confidence", type=float, default=0.5,
...: help="minimum probability to filter weak detections")
...: args = vars(ap.parse_args())
...:
usage: ipython3 [-h] -i IMAGE -p PROTOTXT -m MODEL [-c CONFIDENCE]
ipython3: error: the following arguments are required: -i/--image, -p/--prototxt, -m/--model
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
我可以 运行 这个解析器通过修改 sys.argv
:
In [39]: import sys
In [40]: sys.argv[1:]
Out[40]:
['--pylab',
'--nosep',
'--term-title',
'--InteractiveShellApp.pylab_import_all=False']
In [41]: sys.argv[1:] = '-i image -p proto -m amodel'.split()
In [42]: args = ap.parse_args()
In [43]: args
Out[43]: Namespace(confidence=0.5, image='image', model='amodel', prototxt='proto')
或与
In [45]: ap.parse_args('-i image -p proto -m amodel'.split())
Out[45]: Namespace(confidence=0.5, image='image', model='amodel', prototxt='proto')
我经常使用这种方法来测试解析器。
如果这个解析器在脚本中,并且我从命令行 运行 它没有参数,它会打印 usage
然后退出。 ipython
捕获并显示为 SystemExit: 2
.
的那个出口
我认为你应该将 required=True 设置为 False
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-p", "--prototxt", required=True,
help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
我是运行一个通过OpenCV进行人脸识别的例子。 我此时使用 'argparse',并得到这个错误。
args = vars(ap.parse_args())
来自这段代码。
usage: ipykernel_launcher.py [-h] -i IMAGE -p PROTOTXT -m MODEL
[-c CONFIDENCE]
ipykernel_launcher.py: error: the following arguments are required: -i/--
image, -p/--prototxt, -m/--model
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\Users\user\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
我该如何解决?
这是我的电脑环境,使用的是Jupyter-notebook
- Python: 3.6.4 64 位 [MSC v.1900 64 位 (AMD64)]
- IPython: 6.2.1
- OS: Windows 10 10.0.15063 SP0
- argparse: 1.1
如果不分享您尝试 运行 文件的方式,这很难回答。该错误告诉您它没有找到您 运行 文件时传入的必需参数。
由于您为 -i、-p 和 -m 参数指定了 required = True
,因此您必须始终传递它们,或者如果它们不需要 运行 您的程序,则将它们设为可选。
在 ipython
会话中:
In [36]: import argparse
In [37]: # construct the argument parse and parse the arguments
...: ap = argparse.ArgumentParser()
...: ap.add_argument("-i", "--image", required=True,
...: help="path to input image")
...: ap.add_argument("-p", "--prototxt", required=True,
...: help="path to Caffe 'deploy' prototxt file")
...: ap.add_argument("-m", "--model", required=True,
...: help="path to Caffe pre-trained model")
...: ap.add_argument("-c", "--confidence", type=float, default=0.5,
...: help="minimum probability to filter weak detections")
...: args = vars(ap.parse_args())
...:
usage: ipython3 [-h] -i IMAGE -p PROTOTXT -m MODEL [-c CONFIDENCE]
ipython3: error: the following arguments are required: -i/--image, -p/--prototxt, -m/--model
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
我可以 运行 这个解析器通过修改 sys.argv
:
In [39]: import sys
In [40]: sys.argv[1:]
Out[40]:
['--pylab',
'--nosep',
'--term-title',
'--InteractiveShellApp.pylab_import_all=False']
In [41]: sys.argv[1:] = '-i image -p proto -m amodel'.split()
In [42]: args = ap.parse_args()
In [43]: args
Out[43]: Namespace(confidence=0.5, image='image', model='amodel', prototxt='proto')
或与
In [45]: ap.parse_args('-i image -p proto -m amodel'.split())
Out[45]: Namespace(confidence=0.5, image='image', model='amodel', prototxt='proto')
我经常使用这种方法来测试解析器。
如果这个解析器在脚本中,并且我从命令行 运行 它没有参数,它会打印 usage
然后退出。 ipython
捕获并显示为 SystemExit: 2
.
我认为你应该将 required=True 设置为 False