How to set file association in Windows Explorer?

I have installed the Canopy Python package, and when I double-click a python file it opens the Canopy editor. I want clicking the file to run it, not edit it. I have changed the file properties to Type of file: PY file (.py) / Opens with: python.exe but it doesn't work. Double clicking the file flashes a cmd window for a split second but does not run the file, presumably because it's starting python without giving it a filename.

Note: in a cmd window, python file.py runs the file just fine. And assoc .py says .py=Enthought.Canopy, not the expected something like .py=pyfile.

Note2: Association of Python files in Windows not working says to do a "shift right-click" on the file, but I get no better options with the shift than with just a right-click by itself.

The properties window has no obvious way to tell Windows to run the file with python.exe %s, which is what I assume is needed. How does one set a file association that will actually run the file?

New versions of Windows require Registry editing since the file association control panel does not allow you to specify parameter passing. One source suggests using this in a text file saved as "foo.reg" and opened by Windows:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Perl\shell\Open\command] @="\"C:\path\to\python.exe\" \"%1\" %*"

This should cause Windows to pass the filename to the interpreter.

• Actually the accepted answer author disagrees with newer versions of Windows taking out the file association panel. The way one has to do, however, to achieve any kind of association is non-trivial (i.e. it's the same as the registry entry you typed). For reference here is the Microsoft page on the topic. – Doktoro Reichard Nov 4 '13 at 1:59
• Thanks KA for confirming it's not just me. I have no idea why Microsoft would be so stupid as to not support setting file associations that work (without manually editing config files or the registry), so I asked the question assuming it was my own lack of knowledge. No such luck I guess. – Dave Nov 4 '13 at 12:52

On my system (Win7 x64, Anaconda2 and Anaconda3 installed), I needed to modify

[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]


by putting something like

"C:\Anaconda3\python.exe" "%1" %*


in the default entry.

By the way, %* is useful for passing optional arguments to your Python scripts.

• It isn't clear whether this is a suggested solution or a new question. – fixer1234 Dec 15 '16 at 9:27
• @fixer1234, this was supposed to be a solution that worked in a similar case on my computer. – Hope Dec 19 '16 at 14:27

If you prefer not to screw around with the registry, newer versions of Windows have an alternate:

Control Panel: Control Panel\Programs\Default Programs\Set Associations

• Programs
• Default Programs
• Set Associations

Settings: Windows 10

• System
• Default Apps
• Choose default apps by file type

In both cases, locate .PY and .PYW, and change their program to python.exe or pythonw.exe respectively (py.exe and pyw.exe).

This will automatically set the path to the executable followed by the missing in your case part: %1. This variable is replaced by the name of your py(w) file.

Launch a command prompt.

Associate the correct file group with .py scripts:

assoc .py=Python.File Redirect all Python files to the new executable:

ftype Python.File=C:\Path\to\pythonw.exe "%1" %*

• This has been tried per the initial question, please do take care to read the full question next time. – linuxdev2013 Mar 23 '17 at 6:13