Python argparse fails when the script run without preceding py -2 or -3

  argparse, command-line, python, python-3.x, windows

My python code is:

# my_script.py

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
    print("verbosity turned on")

My default python is Python 3.7.4 (also python 2 is installed on my PC). When I run this script from Windows command line this way:

py -2 my_script.py

or this way:

py -3 my_script.py

The script works fine. But when I run this way:

my_script.py

I get the next error:

usage: my_script.py [-h] [-v]
my_scirpt.py: error: unrecognized arguments:

Why? I always run all my python scripts without preceding py and it works (they do not include argparse module)

Can you please help – why python script that contains arparse does not run without preceding py?

Source: Windows Questions

LEAVE A COMMENT