Python script losing arguments when run from PATH on Windows

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.

Tags:

Python

Windows