Problem passing arguments to Python scripts on Windows

December 14th, 2010 at 8:32 pm

This 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:

  1. Listing all serial ports on Windows with Python
  2. Passing extra arguments to Qt slots
  3. Local execution of Python CGI scripts
  4. Passing extra arguments to PyQt slots
  5. matplotlib – plotting with Python

14 Responses to “Problem passing arguments to Python scripts on Windows”

  1. DaveNo Gravatar Says:

    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.

  2. elibenNo Gravatar Says:

    @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 :-)

  3. alex danteNo Gravatar Says:

    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.

  4. M. BajohrNo Gravatar Says:

    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.

  5. AdilNo Gravatar Says:

    Same as guys have already said. I’ve XP under Python 2.6.6 from python.org and everything works correctly.

  6. JohnnyNo Gravatar Says:

    Thanks. This was driving me nuts.

  7. BibekNo Gravatar Says:

    Thanks a lot. It was most helpful.

  8. GlennNo Gravatar Says:

    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.

  9. ZeNo Gravatar Says:

    Thanks.

  10. SamNo Gravatar Says:

    Cheers, this worked perfect.

  11. Roy F.No Gravatar Says:

    Many thanks to Eli and all who replied. I ran into this problem after double clicking a .py file and browsing to the python.exe that I had copied to the system without running the installer. So I ran the installer and the problem was still there. Glenn’s reply is right on the mark! That’s why for some of us, everything is fine and for others, well, we have this problem. If you have already used “open with” on a .py file, you get .py associated with py_auto_file, which the python installers know nothing about. If .py is already associated, the installer does not override the existing association, and since the Windows “open with” feature does not put the %* in its command strings, the command arguments don’t get passed to the script.

  12. Edgar KnappNo Gravatar Says:

    Thank you. You saved me a lot of time and effort.

  13. AlexMNo Gravatar Says:

    thanks so much for the great post. like others have said this problem was really nagging at me.
    I did have to reboot to get the registry changes to take affect — perhaps that is the normal expected behaviour (?) as I do not normally edit the registry.

    After the reboot all was working as expected.

    again thanks

  14. MikeNo Gravatar Says:

    I know this post is old but I just stumbled upon this problem and I was going mental about what’s causing it. The ‘registry fix’ worked flawlessly, thanks!

    For the record, I’m using Spyder on Win7x64.

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)