-2

Here the code I'm using; I run it with Python 3.6 in Anaconda:

from imutils.perspective import four_point_transform
from imutils import contours

import numpy as np
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, 
  help = 'path to the input image')
args = vars(ap.parse_args())]

and I get an error of:

runfile('/Users/suryavamsi/untitled5.py', wdir='/Users/suryavamsi')
usage: untitled5.py [-h] -1 IMAGE
untitled5.py: error: the following arguments are required: -1/--image
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/Users/suryavamsi/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2870: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

You are running a command-line script in your IDE, with the runfile() command and no command-line arguments.

argparse is working correctly. A required argument is missing (because sys.argv is not set when using runfile()), and so raises the SystemExit exception with the correct error code to exit the command-line script.

You appear to be using Spyder, in which case you'd have to set the command-line options with the Run > Configuration per file option and add your command line options to the Command line options box. See Not sure how to use argv with Spyder.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343