Sometimes you may want to create a shortcut / double-clickable icon for a Python script and pass in some arguments. While this should work on Windows out of the box (after installing Python of course) it doesn't always work.
Make sure that you have a version of Python installed. The typical installation path for Python on Windows is directly in C: not in C:\Programm Files. If you don't have python yet, get a copy at python.org/getit.
Use Notepad to create hello.py on the Desktop, here are the contents:
#!/usr/bin/env python import time, sys if __name__ == '__main__': print "hello world", sys.argv time.sleep(10)
Right click on the icon of hello.py and check Properties->Opens with setting. To be sure, you may want to click Change...->Browse... and select the python executable. Set the checkmark Always use the selected program to open this kind of file.
Lets create a shortcut that passes some arguments to the script.
"C:\Documents and Settings\Administrator\Desktop\hello.py" --somevalue
hello world ['C:\\Documents and Settings\\Administrator\\Desktop\\hello.py", '--somevalue']If this works including the --somevalue output you are all done. Otherwise, read on to the interesting part.
C:\>assoc .py .py=Python.File C:\>ftype Python.File Python.File="C:\Python27\python.exe" "%1" %*The path to Python shown must obviously match the installation path. Important: Make sure there's an %* at the end, else arguments will not be forwarded to python. For more details see Microsoft Technet: Ftype.
HKEY_CLASSES_ROOT\Applications\python26.exe\shell\open\commandmust end with %* as in
"C:\Python27\python.exe" "%1" %*
(c) joachim(et)buechse.ch. This description is in the public domain.