【问题标题】:Argparse - SystemExit:2argparse - SystemExit:2
【发布时间】:2018-08-31 08:38:55
【问题描述】:
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
  • 操作系统:Windows 10 10.0.15063 SP0
  • 参数解析:1.1

【问题讨论】:

  • 您是如何执行此操作的?您似乎不太可能使用 Jupyter 笔记本; argparse 用于命令行执行。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2019-06-03
        • 1970-01-01
        • 2022-12-19
        • 2021-05-29
        • 2020-06-03
        • 2017-07-04
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        相关资源
        最近更新 更多