Argparse - SystemExit:2

时间:2018-03-22 02:41:47

标签: python python-3.x jupyter-notebook argparse

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 64bit [MSC v.1900 64 bit(AMD64)]
  • IPython:6.2.1
  • 操作系统:Windows 10 10.0.15063 SP0
  • argparse:1.1

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

如果您不分享尝试运行文件的方式,这很难回答。该错误告诉您在运行该文件时未找到传入的必需参数。

由于您为-i,-p和-m参数指定了required = True,因此如果不需要它们来运行程序,则必须始终将它们传入或使其成为可选项。

答案 2 :(得分:-1)

我认为您应该将required = True设置为False