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?

4 Answers 4

2

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.

3
  • It isn't clear whether this is a suggested solution or a new question.
    – fixer1234
    Dec 15, 2016 at 9:27
  • 1
    @fixer1234, this was supposed to be a solution that worked in a similar case on my computer.
    – Hope
    Dec 19, 2016 at 14:27
  • This seems to be the best way to get arguments passed to python in Win10. And if you use py.exe, it will autopick the right python version.
    – idbrii
    Nov 4, 2022 at 4:44
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

2
  • 2
    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. Nov 4, 2013 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, 2013 at 12:52
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.

1
  • This works to launch a python script but won't pass any arguments. If you open cmd.exe and run script.py argument then the script won't get "argument" in sys.argv.
    – idbrii
    Nov 4, 2022 at 4:46
-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" %*

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .