为啥我的 argparse 抛出 SystemExit 2 错误?
Posted
技术标签:
【中文标题】为啥我的 argparse 抛出 SystemExit 2 错误?【英文标题】:Why is my argparse throwing a SystemExit 2 error?为什么我的 argparse 抛出 SystemExit 2 错误? 【发布时间】:2019-06-03 05:17:35 【问题描述】:当我运行我的代码时,它显示在此段中发生了异常:SystemExit2 error at the options = parse.parse_args()
。我可以知道这里出了什么问题吗?
import argparse
import queue
import roypy
from sample_camera_info import print_camera_info
from roypy_sample_utils import CameraOpener, add_camera_opener_options
from roypy_platform_utils import PlatformHelper
class MyListener (roypy.IRecordStopListener):
"""A simple listener, in which waitForStop() blocks until onRecordingStopped has been called."""
def __init__ (self):
super (MyListener, self).__init__()
self.queue = queue.Queue()
def onRecordingStopped (self, frameCount):
self.queue.put (frameCount)
def waitForStop (self):
frameCount = self.queue.get()
print ("Stopped after capturing frameCount frames".format (frameCount=frameCount))
def main ():
platformhelper = PlatformHelper()
parser = argparse.ArgumentParser (usage = __doc__)
add_camera_opener_options (parser)
parser.add_argument ("--frames", type=int, required=True, help="duration to capture data (number of frames)")
parser.add_argument ("--output", type=str, required=True, help="filename to record to")
parser.add_argument ("--skipFrames", type=int, default=0, help="frameSkip argument for the API method")
parser.add_argument ("--skipMilliseconds", type=int, default=0, help="msSkip argument for the API method")
options = parser.parse_args()
opener = CameraOpener (options)
cam = opener.open_camera ()
print_camera_info (cam)
l = MyListener()
cam.registerRecordListener(l)
cam.startCapture()
cam.startRecording (options.output, options.frames, options.skipFrames, options.skipMilliseconds)
seconds = options.frames * (options.skipFrames + 1) / cam.getFrameRate()
if options.skipMilliseconds:
timeForSkipping = options.frames * options.skipMilliseconds / 1000
seconds = int (max (seconds, timeForSkipping))
print ("Capturing with the camera running at rate frames per second".format (rate=cam.getFrameRate()))
print ("This is expected to take around seconds seconds".format (seconds=seconds))
l.waitForStop()
cam.stopCapture()
if (__name__ == "__main__"):
main()
这是我执行的回溯:
Exception has occurred: SystemExit
2
File "C:\Users\NPStudent\Desktop\Python Code\sample_record_rrf.py", line 44, in main
options = parser.parse_args()
File "C:\Users\NPStudent\Desktop\Python Code\sample_record_rrf.py", line 69, in <module>
main()
这是我运行程序时的命令行:
【问题讨论】:
您是否偶然在交互式解释器中运行此程序?听起来有点像this question 中描述的问题 不,我在 Visual Studio 代码中运行它 可能仍然是 VSC 如何运行 Python 的问题。我不熟悉它以及如何在其中传递命令行参数。您可以尝试使用options = parser.parse_args(["--frames", "24", "--output", "file.name"])
以确保它与sys.argv
在VCS 中的填充和使用方式无关。如果您可以将异常的完整回溯添加到您的问题中,这也会很有帮助。
好的,我将异常的完整回溯添加到我的问题中
我没有看到任何指定 --frames
和 --output
的内容。你确定你知道如何使用命令行参数运行脚本吗?
【参考方案1】:
您有一些按要求指定的参数 (required=True
) 丢失了!您发布的图片清楚地表明了这一点。
在 VSCode 中按 F5 也会不带参数地运行它。产生同样的错误,进而导致 VSCode 抱怨你的脚本崩溃了。
您必须使用参数--frames
和--output
调用您的程序。
先在命令行上试试,然后为 VSCode 创建一个launch configuration。
【讨论】:
以上是关于为啥我的 argparse 抛出 SystemExit 2 错误?的主要内容,如果未能解决你的问题,请参考以下文章
如何测试我的代码是不是引发了适当的 argparse 异常?