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)
5
  • And your question is? Commented May 14, 2018 at 21:45
  • 1
    Possible duplicate of How to execute Python scripts in Windows? This question is about the arguments being stripped, as is yours. Commented May 14, 2018 at 21:45
  • @zwer I published before finished. My bad. Commented May 14, 2018 at 21:47
  • I cannot reproduce your issue, the presented script should work (at least it works with CPython 2.7.11). Commented May 14, 2018 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. Commented May 14, 2018 at 22:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Easy and accurate!
Followup, I tried the corresponding command in CMD (SET PATHEXT=%PATHEXT%;.py) but that doesn't seem to help. Any ideas there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.