7

When I run my code it shows that in this segment, an exception has occured: SystemExit2 error at the options = parse.parse_args(). May I know what went wrong here?

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()

This is the traceback of my execution:

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 is the command line when i run the program: enter image description here

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Sarah
  • 107
  • 1
  • 6
  • Are you running this in an interactive interpreter by chance? Sounds a bit like the issue described in [this question](https://stackoverflow.com/questions/42249982/systemexit-2-error-when-calling-parse-args) – shmee Jan 08 '19 at 13:51
  • 1
    nope, i ran it in visual studio code – Sarah Jan 08 '19 at 14:00
  • 2
    Might still very well be an issue with how VSC runs Python. I'm not familiar with it and how you pass command line args in it. You could try with `options = parser.parse_args(["--frames", "24", "--output", "file.name"])` to make sure it has nothing to do with how `sys.argv` is populated and used in VCS. It would also be helpful if you could add the full traceback of the exception to your question. – shmee Jan 08 '19 at 14:14
  • okay, i added the full traceback of the exception into my question – Sarah Jan 08 '19 at 14:43
  • When i added the line i got a syntax error for opener = CameraOpener (options) – Sarah Jan 08 '19 at 14:49
  • I think the `parser` has a problem with your commandline (missing arguments most likely), issued an error message, with `usage` and tried exit. VSC has caught that exit and issued this message. – hpaulj Jan 08 '19 at 16:09
  • How are you calling this script? How do you specify the required values like `frames`, `output`? – hpaulj Jan 08 '19 at 17:31
  • i specify the required values when i run the program in the command prompt – Sarah Jan 09 '19 at 02:22
  • Put options = parser.parse_args() statement in try except and print full traceback using traceback.print_exc(). This will give you the idea about the problem. – Ajay Srivastava Jan 09 '19 at 05:03
  • Do you get the same error when you run the script from the commandline? – DisappointedByUnaccountableMod Jan 09 '19 at 08:03
  • yup, i get the same error when i run the script from the command line – Sarah Jan 10 '19 at 02:58
  • Show us the command line call. And the argparse error message. – hpaulj Jan 10 '19 at 03:47
  • i added the command line call already – Sarah Jan 10 '19 at 06:39
  • 4
    I don't see anything specifying `--frames` and `--output`. A you sure you know how to run a script with command line arguments? – hpaulj Jan 10 '19 at 06:48
  • The commandlne isn’t there – DisappointedByUnaccountableMod Jan 10 '19 at 08:48
  • Positional (non-optional) arguments normally aren’t prefixed `—` – DisappointedByUnaccountableMod Jan 10 '19 at 08:48
  • i added the whole programming code inside, the problem i had and also the sentences that pop out at the command area. When i run this program in visual code studio by pressing F5 button, i just cant seem to type the input of the frames,output,skipframes and skipmilliseconds and also it keep showing the argparse error – Sarah Jan 10 '19 at 13:53
  • @Sarah To add command line arguments when running with F5, you need to set up a [launch configuration](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations) – 0x5453 Jan 10 '19 at 13:57
  • The commandline you are using ISN'T visible - the para says: "This is the command line when i run the program: " and then no command line. – DisappointedByUnaccountableMod Jan 10 '19 at 14:01
  • Per shmee: possible duplicate of [SystemExit: 2 error when calling parse\_args()](https://stackoverflow.com/questions/42249982/systemexit-2-error-when-calling-parse-args) – Davis Herring Jan 11 '19 at 01:26
  • @barny i added a picture of the command line, is this the command line that you want to see ? – Sarah Jan 11 '19 at 02:13
  • Right, that looks as expected as you haven’t provided the —frames and —output parameters: what happens when you do? – DisappointedByUnaccountableMod Jan 11 '19 at 08:46

1 Answers1

1

You have arguments that are specified as required (required=True) which are missing! The picture you posted clearly shows this.

Hitting F5 in VSCode will run it without arguments as well. Producing the same error, which in turn causes VSCode to complain that your script crashed.

You have to call your program with the arguments --frames and --output. First try it on the commandline, then create a launch configuration for VSCode.

AndreasT
  • 9,417
  • 11
  • 46
  • 60