python argparse issues with the help argument (TypeError: %o format: a number is required, not dict)

Even though Python has a great documentation, once in a while you get stuck on a single problem more than expected.
It happenned to me today with the argparse module, which I thought I knew enough to quickly code a user interface application.
Here is an example of what I was trying to do (in a more complex code but we will get the idea).

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--elitism', type=int, default=5, dest="elitism", 
help='should be an integer less than popsize. Ideally, about 10%')
args = parser.parse_args()

I got, this kind of error

TypeError: %o format: a number is required, not dict

and the reason is that in the help string there is a % sign, which is not recognised!!!! As simple as that…. The error message being misleading and the code used was embedded in more complex code, so this was not easy to track done… a bit frustrating

And the solution is to double the percent character

help="about 10%%"
Please follow and like us:
This entry was posted in Python and tagged . Bookmark the permalink.

32 Responses to python argparse issues with the help argument (TypeError: %o format: a number is required, not dict)

Leave a Reply

Your email address will not be published.