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 function ShellExecute
which does the following:
However, it is more commonly used to launch an application that operates on a particular file. For instance, .txt files can be opened by Microsoft WordPad. The open verb for a .txt file would thus correspond to something like the following command:
"C:\Program Files\Windows NT\Accessories\Wordpad.exe" "%1"
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:
"C:\Program Files\Python\Python.exe" "D:\github\Miscellaneous-Programs\Python\check.py"
What you can do to avoid this is to create a little .bat
file named check.bat
:
python check.py %*
(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)
Putting the folder on the PATH
does not influence the way the system acts when you run some script by writing script.py -h
at the command line. What happens is the system reads the registry to find out how to run the command you gave. You can display this information by first running reg query HKCR\.py /ve
and then taking the result (which normally is Python.File
) and running reg query HKCR\Python.File\shell\open\command /ve
. The output on my system is "C:\Program Files\Python Launcher (64-bit)\py.exe" "%1" %*
. This means then when the system sees script.py -h
command it runs py.exe
program with the first parameter being the name of the script (that's what "%1"
means) and the rest of the parameters being the ones given to the script (that's what %*
) means. I guess your problem is caused by the lack of %*
part in the apropriate registry entry.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With