I know my title is not descriptive so let me try to explain it here.
Normally I execute my python script like this:
D:\github\Miscellaneous-Programs\Python>python check.py -h
hello
['check.py', '-h']
Now what I did is added the folder D:\github\Miscellaneous-Programs\Python in my windows path environment variable. Than I tried to execute my script like this:
C:\Users\noob>check -h
hello
['D:\\github\\Miscellaneous-Programs\\Python\\check.py']
As you see it didn’t showed the -h argument I supplied to it.
My check.py
import sys
print "hello"
print sys.argv
If I remove print sys.argv from the above mentioned python script it work fine in both cases I mentioned above i.e, it prints “hello” just fine.
So, my question is how does one execute a python script that accepts some command line arguments after the script is added to environment variable.
My purpose is to execute my python script from anywhere in the windows command prompt which is somewhat similar to chmod +x check.py.
I tried the chmod option in cygwin it works fine for both cases.
Cygwin output
noob@noob-PC ~
$ chmod +x check.py
noob@noob-PC ~
$ ./check.py h
['./check.py', 'h']
Windows does not have a notion of executable script files with the interpreter given as a
#!, so what you intend to do cannot work. What Windows does is to call the WinAPI functionShellExecutewhich does the following:see MSDN
As you can see, only the first parameter is supplied to the application. In your case, this translates to something along the lines of:
What you can do to avoid this is to create a little
.batfile namedcheck.bat:(See this SO question for more details. You might also have to supply an absolute path for check.py or python if they cannot be found)