Argparse-系统出口:2

2024-06-28 13:08:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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笔记本

  • Python:3.6.4 64位[MSC v.1900 64位(AMD64)]
  • 伊普顿:6.2.1
  • 操作系统:Windows 10 10.0.15063 SP0
  • argparse:1.1版

Tags: thetopathimageaddtrueparseexit
2条回答

如果不共享运行文件的方式,很难回答这个问题。错误是告诉您在运行文件时未找到传入的必需参数。

由于为-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

相关问题 更多 >