The python's argparse errors
12,928
Solution 1
Your issue has to do with this line:
(options, args) = parse_args()
Which seems to be an idiom from the deprecated "optparse".
Use the argparse idiom without "options":
import argparse
parser = argparse.ArgumentParser(description='Do Stuff')
parser.add_argument('--verbosity')
args = parser.parse_args()
Solution 2
Try:
args = parse_args()
print args
Results:
$ python x.py -b B -aa
Namespace(aa=True, b='B', c=None)
Solution 3
The error is in that parse_argv option is not required or used, only argv is passed.
Insted of:
(options, args) = parse_args()
You need to pass
args = parse_args()
And the rest remains same. For calling any method just make sure of using argv instead of option.
For example:
a = argv.b
Solution 4
It's exactly like the error message says: parser.parse_args()
returns a Namespace
object, which is not iterable
. Only iterable things can be 'unpacked' like options, args = ...
.
Though I have no idea what you were expecting options
and args
, respectively, to end up as in your example.
Author by
Paul
Updated on July 12, 2022Comments
-
Paul almost 2 years
where report this error : TypeError: 'Namespace' object is not iterable
import argparse def parse_args(): parser = argparse.ArgumentParser(add_help=True) parser.add_argument('-a', '--aa', action="store_true", default=False) parser.add_argument('-b', action="store", dest="b") parser.add_argument('-c', action="store", dest="c", type=int) return parser.parse_args() def main(): (options, args) = parse_args() if __name__ == '__main__': main()