Command line arguments are not working in python - python

--A_Z_Handwritten Data.csv: The path to the Kaggle A-Z dataset (Lines 3 and 4)
--HandWritingRecognition: The path to output the trained handwriting recognition model (Lines 5 and 6)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-a", "--A_Z_Handwritten Data.csv", required=True,
help="path to A-Z dataset")
ap.add_argument("-m", "--HandWritingRecognition", type=str, required=True,
help="path to output trained handwriting recognition mode")
# ap.add_argument("-p", "--plot", type=str, default="plot.png",
# help="/home/gaurav/Desktop/HandWritingRecognition/")
args = vars(ap.parse_args())
This is the error...!
usage: ipykernel_launcher.py [-h] -a A_Z_HANDWRITTEN DATA.CSV -m
HANDWRITINGRECOGNITION
ipykernel_launcher.py: error: the following arguments are required: -a/--A_Z_Handwritten Data.csv, -m/--HandWritingRecognition
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2

Related

An exception has occurred, use %tb to see the full traceback. SystemExit: 2

can anyone tell me how to make this code works in jupyter or any notebook
Code
import argparse
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.2, help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
Error
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
I've tried some solutions but none work

How to implement parse_args() in pytorch?

I am trying to code a module for my deep learning model. I wish to store these arguments using argeparse. There is some problem occuring in args = parser.parse_args()! Also What are the benefits of using the argparse library?
import numpy as np
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str,
help='Dataset')
parser.add_argument('--epoch', type=int, default=40,
help='Training Epochs')
parser.add_argument('--node_dim', type=int, default=64,
help='Node dimension')
parser.add_argument('--num_channels', type=int, default=2,
help='number of channels')
parser.add_argument('--lr', type=float, default=0.005,
help='learning rate')
parser.add_argument('--weight_decay', type=float, default=0.001,
help='l2 reg')
parser.add_argument('--num_layers', type=int, default=2,
help='number of layer')
parser.add_argument('--norm', type=str, default='true',
help='normalization')
parser.add_argument('--adaptive_lr', type=str, default='false',
help='adaptive learning rate')
args = parser.parse_args()
print(args)
The above code is a part of full code, as given in the link below
Error:
usage: ipykernel_launcher.py [-h] [--dataset DATASET] [--epoch EPOCH]
[--node_dim NODE_DIM]
[--num_channels NUM_CHANNELS] [--lr LR]
[--weight_decay WEIGHT_DECAY]
[--num_layers NUM_LAYERS] [--norm NORM]
[--adaptive_lr ADAPTIVE_LR]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/anshumansinha/Library/Jupyter/runtime/kernel-1e4c0f41-a4d7-4388-be24-640dd3411f56.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
The above code is a part of a code, the full code can be seen on the following github link : link
You called ipykernel_launcher.py with the argument -f. But -f is not in your list of arguments.
Is -f the argument, which you want to use for the input file? Then you should add something like this to your code:
parser.add_argument('--file', '-f', type=str)
The benefit of argparse is that you don't have to write by hand the code for parsing the arguments. You can try this. It is more code than one normally thinks. Especially if you have positional arguments.

Error during constructing the argument parse and parse the arguments

Why my argument parse has an error although I have already defined the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-w", "--width", type=float, required=True,
help="width of the left-most object in the image (in inches)")
args = vars(ap.parse_args())
and the error says that
usage: main.py [-h] -i IMAGE -w WIDTH
main.py: error: the following arguments are required: -i/--image, -w/--width

How to run my Python code using Spyder, without the error "the following arguments are required: -p/--shape-predictor"?

How can I run this?
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-a", "--alarm", type=str, default="",
help="path alarm .WAV file")
ap.add_argument("-w", "--webcam", type=int, default=0,
help="index of webcam on system")
args = vars(ap.parse_args())
While I run this on Spyder it gives
usage: [-h] -p SHAPE_PREDICTOR [-a ALARM] [-w WEBCAM]
: error: the following arguments are required: -p/--shape-predictor
An exception has occurred, use %tb to see the full traceback.
How can I solve this issue?
From #hiroprotagonist's comment posted above:
In spyder ctrl+F6 should allow you to pass command-line args when you execute your script. see here https://stackoverflow.com/a/26766414/4954037.

Argparse - SystemExit:2

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())
I'm running a face recognition example through OpenCV.
I use 'argparse' at this point, and get this error.
args = vars(ap.parse_args())
from this code.
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)
How can I solve it?
This is my computer environment and use the Jupyter-notebook
Python: 3.6.4 64bit [MSC v.1900 64 bit (AMD64)]
IPython: 6.2.1
OS: Windows 10 10.0.15063 SP0
argparse: 1.1
This is hard to answer without you sharing how you try to run the file. The error is telling you it did not find the required arguments passed in when you ran the file.
Since you specified required = True for the -i, -p, and -m arguments you must always pass them in or make them optional if they are not needed to run your program.
In an ipython session:
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)
I can run this parser by modifying 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')
or with
In [45]: ap.parse_args('-i image -p proto -m amodel'.split())
Out[45]: Namespace(confidence=0.5, image='image', model='amodel', prototxt='proto')
I often use this method to test a parser.
If this parser was in a script, and I ran it from command line without the arguments, it would print the usage and then exit. That exit is what ipython is catching and displaying as SystemExit: 2.
I think you should set required=True to False

Categories

Resources