views:

311

answers:

3

I'm having trouble getting command line arguments passed to Python programs if I try to execute them directly as executable commands from a Windows command shell. For example, if I have this program (test.py):

import sys
print "Args: %r" % sys.argv[1:]

And execute:

>test foo
Args: []

as compared to:

>python test.py foo
Args: ['foo']

My configuration has:

PATH=...;C:\python25;...
PATHEXT=...;.PY;....

>assoc .py
.py=Python.File

>ftype | grep Python
Python.CompiledFile="C:\Python25\python.exe" "%1" %*
Python.File="C:\Python25\python.exe" "%1" %*
Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*
A: 

Interesting. Works here using python 2.6 and Windows XP (5.1.2600):

C:\Documents and Settings\hbrown>python test.py foo
['test.py', 'foo']

C:\Documents and Settings\hbrown>test.py foo
['C:\\Documents and Settings\\hbrown\\test.py', 'foo']

C:\Documents and Settings\hbrown>test foo
['C:\\Documents and Settings\\hbrown\\test.py', 'foo']

C:\Documents and Settings\hbrown>type test.py
import sys
print sys.argv 

C:\Documents and Settings\hbrown>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY 

C:\Documents and Settings\hbrown>assoc .py
.py=Python.File
hughdbrown
+4  A: 

I think I solved this. For some reason there is a SECOND place in the registry (besides that shown by the file associations stored in HKEY_CLASSES_ROOT\Python.File\shell\open\command):

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Python25\\python.exe\" \"%1\" %*"

This seems to be the controlling setting on my system. The registry setting above adds the "%*" to pass all arguments to python.exe (it was missing in my registry for some reason).

mckoss
This didn't work for me on Windows 7. What OS did you test this in?
Acorn
Tested on Vista.
mckoss
My key is located here on Windows 7:HKEY_USERS\S-1-5-21-2829634124-923609355-2255922086-1001\Software\Classes\Applications\python.exe\shell\open\command
Steven
With the value:C:\Python26\python.exe %1 %*
Steven
A: 

My setting was under yet another registry key, HKEY_CLASSES_ROOT\py_auto_file. The other keys mentioned also existed but Windows was using this one for some reason.

bainorama