How to set file association in Windows Explorer?

2

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?

Dave

Posted 2013-11-04T00:25:21.810

Reputation: 129

Answers

1

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.

Hope

Posted 2013-11-04T00:25:21.810

Reputation: 167

It isn't clear whether this is a suggested solution or a new question. – fixer1234 – 2016-12-15T09:27:52.603

1@fixer1234, this was supposed to be a solution that worked in a similar case on my computer. – Hope – 2016-12-19T14:27:08.813

1

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.

Source: https://stackoverflow.com/questions/444388/how-can-i-pass-command-line-arguments-via-file-association-in-vista-64

K.A.Monica

Posted 2013-11-04T00:25:21.810

Reputation: 5 887

1

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 – 2013-11-04T01:59:09.573

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 – 2013-11-04T12:52:05.860

0

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.

Aanand Kainth

Posted 2013-11-04T00:25:21.810

Reputation: 1

-2

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" %*

Lou

Posted 2013-11-04T00:25:21.810

Reputation: 1

This has been tried per the initial question, please do take care to read the full question next time. – linuxdev2013 – 2017-03-23T06:13:37.110