9

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?

7
  • How do you open them? By double clicking? Are they executed right away in a console window, or got picked up by IDE (i.e. IDLE)?
    – J0HN
    Jun 12, 2013 at 19:51
  • @J0HN: I open them by double clicking and they're being opened with a console window. I've also tried right clicking and choosing "open with" with the 3.3 version.
    – snakile
    Jun 12, 2013 at 19:52
  • launch interactive interpreter by typing just python in console, than run the code from the question. If it still output 2.7.3 your path points to the wrong python interpreter. Or, even better, just type python -V in console
    – J0HN
    Jun 12, 2013 at 19:53
  • @J0HN: Thanks, both your suggestions do work and output 3.3 as desired. But even when I start the .py file using Windows command line shell it outputs 2.7.
    – snakile
    Jun 12, 2013 at 19:57
  • haven't used python in windows for years but check these env-variables: %PATH%, %PYTHONPATH%. Have a look at msconfig and autoexec.bat. Jun 12, 2013 at 19:57

5 Answers 5

4

It seems that windows is not checking the whole path of the executable! Try to rename the python.exe of version 2 (respectively 3) to some temp name. Then associate with this name. And change everything back.

FYI you could check the file association with:

C:\Users\shadowed>assoc .py
.py=Python.File

C:\Users\shadowed>ftype Python.File
Python.File="C:\Python27\python.exe" "%1" %*
1
  • this is the easiest fix and probably should be the accepted answer May 6, 2017 at 19:09
3

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.

1
  • nirsoft tools are extremely convenient to make up for global windows incompetence to provide simple mechanisms. See also "nircmd" by the same author. Oct 6, 2019 at 14:47
1

Try to add #!python3 to the very first line. Python 3.3 introduced Python Launcher for Windows that associates .py with c:\windows\py.exe. It launches the highest Python 2 by default.See https://stackoverflow.com/a/17245543/1346705

0

If you want to change the IDLE used when right clicking you can go into the registry and find the key:

HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command

You should see something like:

"C:\Python27\pythonw.exe" "C:\Python27\Lib\idlelib\idle.pyw" -e "%1"

Change all the 27 to 33 if you installed your pythons in the default area. If not just change the path to where your 3.3 implementation is stored.

To change the default command line python you should do the same with the path in your PATH environment variable.

0

Have you tried uninstalling Python 2.7 temporarily? That might fix the file association. I am uncertain, as I am not familiar with Windows 8 beyond what little I learned of it when I tried it on someone else's computer. I'm not even sure it would work on Windows 7, but it is worth a shot if you ask me.

I also recommend using the tag for the specific version of Windows you are using, rather than the standard "windows" tag. In this case, I might use both, since file associations don't change much between versions of Windows. At least use the correct tag (windows-8), at any rate. Then you will get more answers from people looking for that tag.

You must log in to answer this question.

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