Skip to content Skip to sidebar Skip to footer

Argparse Tutorial Example Doesn't Work In Windows Command Line

So I'm trying to teach myself how to use the python library argparse via the tutorial here. The example is given by the following piece of code which is saved as tut.py. import arg

Solution 1:

At the beginning I didn't think that it was important to specify that I use the anaconda distribution for python, however it turns out that using the anaconda command prompt instead of the windos 10 one solves the problem.

Output

(base) C:\Users\Admin\Desktop\HiWi\Codect>python tut.py 1 2 3 4
4

(base) C:\Users\Admin\Desktop\HiWi\Codect>python tut.py 1 2 3 4 --sum
10

(base) C:\Users\Admin\Desktop\HiWi\Codect>

Solution 2:

Use this code to get some of n passed arguments : tut.py

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                const=sum, default=max,
                help='sum the integers (default: find the max)')

args = parser.parse_args()
print(sum(args.integers))

OUTPUT

python tut.py 1 2 3 4   
10

Note: when I run your code it runs perfectly


Post a Comment for "Argparse Tutorial Example Doesn't Work In Windows Command Line"