Windows insists associating .py files with the wrong version of Python

pythonwindows

I have both Python 2.7 and Python 3.3 installed on my machine (I'm on Windows 8).

I would like .py files to be opened with Python 3.3 by default. The only Python path in the PATH environment variable is the 3.3 one. Still, when opening .py files I see they're being interpreted by Python 2.7, even when I explicitly open them with the Python 3.3 exe ("Open with" and choosing the 3.3 exe). I've also tried changing the Python.exe version using "Set Associations" in Windows' control panel. I'm not sure it has changed anything. No matter what I do, the output of the following program is 2.7.3:

import sys
print(sys.version)

How do I force Windows 8 to open .py files with Python 3.3?

Best Answer

Changing the shebang (#! python3) may help you, but you will have a problem: if your python script has parameters, they will be cut off when you run command

somepythonfile.py your_parameters

You will get py.exe running with just "somepythonfile.py" and NO parameters, believe me.

The method which worked for me is next: run the app http://www.nirsoft.net/utils/file_types_manager.html - this is a file association manager. Fix the association for python files to be

"C:\Windows\py.exe" "%1" %*

instead of

"C:\Windows\py.exe" "%1"

Or, alternatively, set

"C:\Python34\pythonw.exe" "%1" %*

explicitly - to match the desired version and omit the shebang line.

Related Question