10

Is there a way to control what version of python is run when double clicking on a py file? From the command line and in environments such as eclipse I can control what version is run. But from double clicking I am not sure.

I have 2.6 and 2.7 installed. 2.6 is for some application specific stuff and I want to make 2.7 the default. I have added "C:\Python27" to the PATH environment variable and that works well at the command line. C:\path\to\some\file>python someFile.py will run the file in 2.7. But if I double click the same file from explorer it runs 2.6. How to get it to run 2.7?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
ollie dunn
  • 305
  • 4
  • 13

4 Answers4

8

On Windows, you have to modify the file associations, for example via Right Click → Open with ...Choose default program or the Control Panel's Folder Settings. You can choose between multiple python installations by navigating to the python.exe you want via the Browse button:

enter image description here

Alternatively, you can change the association in a command shell by typing

ftype Python.File="C:\Python27\python.exe" "%1" %*

Note that this requires administrator rights. If UAC is enabled on your machine, right click cmd in the start menu and select Run as administrator.

On freedesktop.org-compatible desktops, you can configure the association with xdg-mime.

On debian-based distributions, you can change the default python with update-alternatives. On all systems, you can also symlink the python in your path to the correct implementation, like this:

$ sudo ln -sf python2.7 /usr/bin/python

If the file is marked executable, it can also be executed directly from the command line or GUI if it starts with #! and the name of the interpreter:

#!/usr/bin/env python

To choose a specific Python version just for your program, you can start your Python program with one of the following lines:

#!/usr/bin/env python2.7
#!/usr/bin/python2.7
phihag
  • 278,196
  • 72
  • 453
  • 469
  • ...And on all other O.S., just put the path to the Python interpreter after the #! characters on the very first line of the script. – jsbueno Jan 24 '12 at 12:13
  • Updated the answer for other OSs. Unfortunately, it's a little bit more complicated than adding a shebang. – phihag Jan 24 '12 at 12:28
  • 1
    Thanks for your response. Unfortunately modifying the file extension does not work as 2.6 and 2.7 have the same executable name (python.exe). It seems that windows is unable to distinguish between them. So if I modify the file association and browse to 2.7. The file still runs in 2.6 regardless. Unless I am overlooking something? – ollie dunn Jan 24 '12 at 14:00
  • @ollie They have the same executable *basename*, but not *filename* (i.e. the exes are in different directories). I added an image that shows where you can find the Browse button, and a command-line alternative. – phihag Jan 24 '12 at 17:35
  • ftype Python.File="C:\Python35\python.exe" "%1" %* set the last python version (for me: python3.6). If your last version is 3.6 but you want 3.5 just add some xxx in your folder (xxxpython36) so it will take the last recognized version which is python3.5 (after the cmd remove the xxx) – JinSnow Nov 30 '16 at 06:31
4

OK I have found the Python Launcher, which does exactly what I am after. Download can be found here. Installing this gave me the option for "Python Launcher for Windows (GUI)" when changing the file association via the right click menu.

Adding the shebang line

#!/usr/bin/python2.7

forces the script to run in 2.7.

This works great as I can control what version of python is running and users never need to know. No need for bat files, or dragging onto shortcuts etc. Nice and clean, and most importantly, no room for user error.

ollie dunn
  • 305
  • 4
  • 13
3

You can use ASSOC and FTYPE

assoc .py=pyfile
ftype pyfile=c:\Python27\python.exe %1
PabloG
  • 25,761
  • 10
  • 46
  • 59
  • Thanks for your response. I tried running these commands from the command line. ftype was indeed looking at python26. I amended it to point to 27. It does not seem to have effected double clicking in explorer though. Files still get run with 2.6. This there something specific I need to do to have it influence explorer? – ollie dunn Jan 24 '12 at 13:53
  • 1
    @olliedunn, you need to use the same file type that `assoc` returns. On my system it's `Python.File`. – Mark Ransom Jan 24 '12 at 17:40
  • I edited the example to use Python.File, since that's what the python installer uses by default – matt wilkie Apr 30 '13 at 07:23