as far as I understood, with ConfigArgParse, I can set the very main config in a config.ini file of my program and make some of those choices available via command line. However, when I set my config.ini file as default in the constructor, I get the following error:
main.py: error: unrecognized arguments: --input_base data
where --input_base is the only configuration not included in my parser as can be seen in the following:
parser = ArgParser(default_config_files=['config.ini'])
parser.add_argument('-out', '--output_base', type=str, help='xyz')
parser.add_argument('--amount', type=int, help='xyz')
parser.add_argument('--num_jobs', help='xyz')
parser.add_argument('--batch_size', type=int, help='xyz')
parser.add_argument('--queue_size', type=int, help='xyz')
parser.add_argument('--kind', choices={'long', 'short', 'both'}, help='xyz')
parser.add_argument('--level', choices={'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}, help='xyz')
config = parser.parse_args()
Only using config.ini works fine but because of usability I have to include command line args as well.
Thanks for your help. Appreciate it!
Try change last line to:
config, unknown = parser.parse_known_args()
This will parse only known arguments (ignoring every unknown).
as in this question: Python argparse ignore unrecognised arguments
Related
I am trying to set up training arguments and parse but I got this error could anyone help please!
parser = argparse.ArgumentParser(description='Explore pre-trained AlexNet')
parser.add_argument(
'--image_path', type=str,
help='Full path to the input image to load.')
parser.add_argument(
'--use_pre_trained', type=bool, default=True,
help='Load pre-trained weights?')
args = parser.parse_args()
got this error
usage: ipykernel_launcher.py [-h] [--image_path IMAGE_PATH]
[--use_pre_trained USE_PRE_TRAINED]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-ff8e2476-e39b-4e40-b8f9-6b8113fe8f1f.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
In a Jupyter notebook cell:
import sys
sys.argv
I get get
['/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py',
'-f',
'/home/paul/.local/share/jupyter/runtime/kernel-7923bfd2-9f96-45cf-8b44-1859a2185715.json']
The Jupyter server is using the sys.argv to set up the communication channel with your kernel. argparse parses this list too.
So commandline and argparse cannot be used to provide arguments to your notebook when run this way.
How did you start this script? Did you even try to provide the commandline values that the script expected?
'--image_path'
'--use_pre_trained'
If you did, you probably would have gotten a different parser's error, about 'unexpected arguments'. That's coming from the server.
If you use a Colab may this solution help you, this solution suggest to you to write argparse in the other python file introduction-argparse-colab
%%writefile parsing.py
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
In a Jupyter notebook code works like this:
parser = argparse.ArgumentParser()
parser.add_argument('--image_folder', type=str, default='/content/image', help='path to image folder')
parser.add_argument("-f", "--file", required=False)
You need to add in the end of " parser.add_argument " line ( This is the reason you are getting 'SystemExit: 2' error ):
parser.add_argument("-f", required=False)
in your case this should work:
parser = argparse.ArgumentParser(description='Explore pre-trained AlexNet')
parser.add_argument('--image_path', type=str, default='your_path', help='Full path to the input image.')
parser.add_argument('--use_pre_trained', type=bool, default=True, help='Load pre-trained weights?')
parser.add_argument("-f", "--file", required=False)
args = parser.parse_args()
Now you can call:
image = args.image_path
Or
from PIL import Image
image = Image.open(args.image_path)
Tested in Google colab
conflict_handler(action, confl_optionals)
File "/usr/local/lib/python3.6/argparse.py", line 1510, in _handle_conflict_error
raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument -h/--height: conflicting option string: -h
The above is the error message,
here is my code,
I don't see the error:
# 1) Parse the arguments
parser = argparse.ArgumentParser(description="Description for my parser")
parser.add_argument("-v", "--velocity", action="store", required=True, help="The velocity of the object is required")
parser.add_argument("-a", "--angle", action="store", type=float, required=True, help="The angle of the object is required")
parser.add_argument("-h", "--height", required=False, default= 1.2, help="The height of the object is not required. Default is set to 1.2 meters" )
Option "-h" is by default predefined as a "help" option, which prints the description and the list of arguments. Your custom "-h --height" conflicts with this, thus causing an error.
It wouldn't be nice to overwrite the default "-h --help" option, because many users expect "-h" option to print help message. (So if I were you I would find another way to name the option.) But you can ignore it if you really need to by using add_help parameter with the constructor. Like this:
parser = argparse.ArgumentParser(description="Description for my parser", add_help=False)
If you want to keep "--help" option, you have to add another line of parser.add_argument("--help", action="help"). (Thanks to chepner)
As the error suggests you are using a param name who is conflicting with other. Particularly in this case the -h option. The lib argparse always include the -h option to print the script help, so for the height, you must use a different param than -h, for example -ht.
parser = argparse.ArgumentParser(description="Description for my parser")
parser.add_argument("-v", "--velocity", action="store", required=True, help="The velocity of the object is required")
parser.add_argument("-a", "--angle", action="store", type=float, required=True, help="The angle of the object is required")
parser.add_argument("-ht", "--height", required=False, default= 1.2, help="The height of the object is not required. Default is set to 1.2 meters" )
Here's my code:
def parse_args():
parser = argparse.ArgumentParser(description='Simple training script for object detection from a CSV file.')
parser.add_argument('csv_path', help='Path to CSV file')
parser.add_argument('--weights', help='Weights to use for initialization (defaults to ImageNet).', default='imagenet')
parser.add_argument('--batch-size', help='Size of the batches.', default=1, type=int)
return parser.parse_args()
when I run my code, I get an error:
usage: Train.py [-h] [--weights WEIGHTS] [--batch-size BATCH_SIZE] csv_path
Train.py: error: too few arguments
Any idea where I'm going wrong?
This is because you did not specify the number of arguments expected after each flag with nargs as such:
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Simple training script for object detection from a CSV file.')
parser.add_argument('csv_path', nargs="?", type=str, help='Path to CSV file')
parser.add_argument('--weights', nargs="?", help='Weights to use for initialization (defaults to ImageNet).', default='imagenet')
parser.add_argument('--batch-size', nargs="?", help='Size of the batches.', default=1, type=int)
return parser.parse_args()
parse_args()
According to the doc:
If the nargs keyword argument is not provided, the number of arguments consumed is determined by the action. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced.
'?'. One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from default will be produced. Note that for optional arguments, there is an additional case - the option string is present but not followed by a command-line argument. In this case the value from const will be produced. Some examples to illustrate this:
Details here
The first arg csv_path is required (you did not provide some default value), so you need to pass that to your command line like below:
python Train.py some_file.csv # or the path to your file if it's not in the same directory
Try this:
import argparse
import sys
import csv
parser = argparse.ArgumentParser()
parser.add_argument('--file', default='fileName.csv')
args = parser.parse_args()
csvdata = open(args.file, 'rb')
I made a script that takes a few arguments to run. Originally it could be run automatically with the argument 'auto', but I'm trying to daemonize it so it will run the run the script with the specified arguments as a daemon. The problem is that python-daemon and argparse don't seem to get along when it comes to deciding who parses what.
parser = argparse.ArgumentParser(usage='pyfilter.py <file> <options> <actions>')
parser.add_argument('file', help='blacklist file containing IPs', type=str)
subparsers = parser.add_subparsers(help='help', dest='action')
parser_update = subparsers.add_parser('update', help='update help')
parser_update.add_argument('-c', '--check', help="check IPs for abuse reports", dest="check", type=str, nargs=1)
parser_blacklist = subparsers.add_parser('blacklist', help='create iptables rules for malicious IPs specified'
'in the provided file')
parser_clear = subparsers.add_parser('clear', help='clear iptables')
parser_auto = subparsers.add_parser('auto', help='automatically run update and blacklist on a loop')
parser_auto.add_argument('-i', '--interval', help='specify the loop interval', dest='interval', type=int, nargs=1)
parser_auto.add_argument('-c', '--check', help="check IPs for abuse reports", dest="check", type=str, nargs=1)
parser.add_argument('-p', '--port', help='specify the port to block', type=int)
parser.add_argument('-v', '--verbose', help='write output to screen', nargs=1)
args = parser.parse_args()
. . .
class pyfilterDaemon():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/pyfilter.pid'
self.pidfile_timeout = 5
def run(self):
while True:
update()
blacklist()
time.sleep(interval)
. . .
def main():
#handle()
d = pyfilterDaemon()
daemon_runner = runner.DaemonRunner(d)
daemon_runner.start()
Here's the commands I've tried to make this work:
root#tfof:~# ./pyfilter.py start
ERROR: File 'start' not found # argparse parsed 'start' accidentally
root#tfof:~# ./pyfilter.py /etc/blacklist.lst -v yes auto
usage: checkfilter.py stop|restart|start # now the argparse arguments are satisfied, but python-daemon is looking for its argument
root#tfof:~# ./pyfilter.py /etc/blacklist.lst -v yes auto start
usage: pyfilter.py <file> <options> <actions>
pyfilter.py: error: unrecognized arguments: start # argparse is trying to parse 'start'
Would it be possible to pass off the 'start' argument to python-daemon or something? Or if I could just get rid of the argparsing it'd be fine, but the 'file' argument is a must.
Argparse takes arguments from sys.argv per default (see here).
It is not surprising that the behaviour you see here is happening,
as you just call the parse_args function with the default arguments.
You can just pass whatever you want to parse to it, instead of sys.argv.
See this question for an example.
So consume whatever you need for python-deamon and then parse the remaining args with argparse.
I'm running this as part of a call to the api using the url. I dont know what I'm doing wrong - the terminal keeps saying I have an Attribute error where the 'Namespace' object has no attribute to offset. I wanted to add search parameters "offset, sort and category_filter", but am not sure what I have to do to the parser.add_argument. I tried copying those that were in the sample code listed below, but it didnt seem to work. I'm a bit confused as to why that is...
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-q', '--term', dest='term', default=DEFAULT_TERM,
type=str, help='Search term (default: %(default)s)')
parser.add_argument('-l', '--location', dest='location',
default=DEFAULT_LOCATION, type=str,
help='Search location (default: %(default)s)')
parser.add_argument('--offset', dest='offset', default=DEFAULT_OFFSET,
type=int, help='Search offset (default: %(default)s)')
parser.add_argument('--sort', dest='sort', default=DEFAULT_SORT,
type=int, help='Sear sort (default:%(default)s)')
parser.add_argument('--category_filter', dest='category_filter', default=DEFAULT_CATEGORY_FILTER,
type=str, help='Search category_filter (default: %(default)s)')
input_values = parser.parse_args()
try:
query_api(input_values.term, input_values.location, input_values.offset, input_values.sort, input_values.category_filter)
except urllib2.HTTPError as error: