Problem passing arguments to Python scripts on Windows
December 14th, 2010 at 8:32 pmThis is a problem I encountered more than once, and today I finally found a "solution". I place "solution" in quotes because it’s more of a workaround for something that seems to be a problem of the Python Windows installer.
I ran into the problem on a Windows 7 box running ActivePython 2.6, but according to this Python issue, others have encountered the problem with Windows XP and Python 3.x as well.
The problem manifests itself as follows. Prepare a simple script containing:
import sys
print sys.argv
Execute it from the command-line:
C:\>python z.py 1 2 3
['z.py', '1', '2', '3']
This looks right. But now execute it without prepending python:
C:\>z.py 1 2 3
['C:\\z.py']
What gives? Doesn’t the installer configure .py files to be run by the Python executable correctly, passing arguments to it as one would expect?
Now, I found a couple of non-solutions. The most popular was to setup the association with the assoc and ftype commands, as follows:
C:\>ftype Python26.File="C:\Python26\python.exe" "%1" %*
C:\>assoc .py=Python26.File
But at no avail, the problem persisted.
What eventually solved it is fixing the relevant registry keys for Python. I set the HKEY_CLASSES_ROOT\Applications\python26.exe\shell\open\command key to:
"C:\Python26\python26.exe" "%1" %*
Previously, %* was missing. Similarly, I set HKEY_CLASSES_ROOT\py_auto_file\shell\open\command to the same value. You can also set it accordingly for python26w.exe (the no-shell version of Python on Windows).
This worked, and now I got:
C:\>z.py 1 2 3
['C:\\z.py', '1', '2', '3']
What causes the problem? In all likeness the ActivePython 2.6 installer, which doesn’t set up the registry keys correctly. Now, this may not always happen, and some discussions point to it being dependent on other factors. For instance, I had another version of Python already installed on the machine when I executed the installer – this may have confused it.
Related posts:

December 14th, 2010 at 21:13
Just as a data point, I use Python 2.6 under Windows 7 but I install from Python.org, not ActiveState, and mine works correctly.
December 15th, 2010 at 08:09
@Dave, thanks for the input. It seems to depend on many factors – I also have another (win XP) machine where it’s OK. The important point, in any case, is that the wrong invocation line somehow got into the registry, and I wasn’t the one who put it there
December 15th, 2010 at 09:09
I’m not sure if it’s the ActiveState installer that’s at fault here. I’ve got 2.6 running under XP & 7 and both pass arguments from the comline as expected.
December 15th, 2010 at 12:05
Same here. I’ve never tried ActivePython so my experience is on the original installer. I’ve never had any problems calling a python-file directly with args. That’s a way i use python in many environments from Win2000prof / Server up to Win7-46 / Server 2008.
January 2nd, 2011 at 11:24
Same as guys have already said. I’ve XP under Python 2.6.6 from python.org and everything works correctly.
January 4th, 2011 at 17:38
Thanks. This was driving me nuts.
March 25th, 2011 at 14:29
Thanks a lot. It was most helpful.
May 16th, 2012 at 01:15
So deleting the python.exe key works too… then the Python.File ftype seems to take back over.
I think the stuff in this Applications key is created when you choose to “open with” for a particular extension, and choose “always open with this program”. At least, there is some interaction between them… that is when my problems started.
February 14th, 2013 at 10:28
Thanks.
March 2nd, 2013 at 07:13
Cheers, this worked perfect.