0

NOT like this question: Calling a python script from command line without typing "python" first

I have a Python script called argparse_test.py. I put it in my system path on my Windows 10 machine so I can call it from any folder by just typing "arparse_test.py" followed by arguments.

The argument works if I call

python argparse_test.py -w

But it does not recognize arguments when I call

argparse_test.py -w

Is there a way to make this work?

Script here:

import argparse
import time
if __name__=='__main__':
    # Command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", "--work", action="store_true", help="Make it work")
    args = parser.parse_args()
    if args.work:
        print "It works"
    else:
        print "It doesn't work"
    time.sleep(5)
hnefatl
  • 5,860
  • 2
  • 27
  • 49
minterm
  • 259
  • 3
  • 13
  • And your question is? – zwer May 14 '18 at 21:45
  • 1
    Possible duplicate of [How to execute Python scripts in Windows?](https://stackoverflow.com/questions/1934675/how-to-execute-python-scripts-in-windows) This question is about the arguments being stripped, as is yours. – hnefatl May 14 '18 at 21:45
  • @zwer I published before finished. My bad. – minterm May 14 '18 at 21:47
  • I cannot reproduce your issue, the presented script should work (at least it works with CPython 2.7.11). – zwer May 14 '18 at 21:56
  • I think the issue is permissions and some weird stuff with file associations. I'm not admin on this computer. The "possible duplicate" might have helpful answers. – minterm May 14 '18 at 22:07

1 Answers1

2

You need to add .py to the PATHEXT environment variable. In PowerShell, do: $ENV:PATHEXT += ";.py"

Bruce Payette
  • 2,511
  • 10
  • 8