16

I have a bunch of scripts written in Python. I run them from a Windows command prompt like

c:> my_script.py arg1 arg2 arg3

This works in every computer and every Windows version since many years ago. Just now it this has broken on my Windows 7 system. The script is loaded and executed. But none of the arguments are passed into the script.

To illustrate this, I have a script named py_echo.py:

from pprint import pprint as pp
import sys

if __name__ =='__main__':
    pp(sys.argv)

Then I execute it with the argument a, b, c. None of them are passed.

c:\Python27\Lib\site-packages>py_echo.py a b c
['C:\\0\\usr\\bin\\py_echo.py']

If I run python.exe explicitly, the arguments are passed correctly

c:\Python27\Lib\site-packages>python.exe c:\0\usr\bin\py_echo.py a b c
['c:\\0\\usr\\bin\\py_echo.py', 'a', 'b', 'c']

It was working before. It only start to break down after I uninstalled a bunch old version Python interpreter and modules from my PC. Reinstalling Python does not help. I wonder what can I do to fix this??

I have become very dependent on my scripts I built over the years. I feel very handicapped when they breaks :(

Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47
  • 1
    Have you tried this: http://stackoverflow.com/questions/2640971/windows-is-not-passing-command-line-arguments-to-python-programs-executed-from-t – Chris Laplante Oct 22 '11 at 16:30
  • Thank you all. I found another thread on Perl points to the same solution. http://stackoverflow.com/questions/444388/how-can-i-pass-command-line-arguments-via-file-association-in-vista-64 – Wai Yip Tung Oct 22 '11 at 18:25
  • Same here for Python: http://stackoverflow.com/questions/2437167/issues-running-python-scripts-in-command-prompt-specifically-with-command-line. (Solution did not work on Windows 7, though). – bavaza Apr 20 '12 at 08:52

2 Answers2

9

I had the same problem with Windows 7 / Python, and eventualy found that I had to set up correct file associations AND update two registry entries through regedit.

It is all described in this excelent article:

http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

Jon H
  • 91
  • 1
  • 3
6

To move the answer to SO (rather than the link in Jon's answer):

Modifying the following two registries so that the arguments are passed along to Python:

HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

Add %* to the existing "C:\PythonXX\python.exe" "%1", so that the key now looks like: "C:\PythonXX\python.exe" "%1" %*.

Source: http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

Community
  • 1
  • 1
David C
  • 7,204
  • 5
  • 46
  • 65