How to control which version of Python starts when you double-click on a file?

Is there a way to control which version of python starts when double clicking on the py file? From the command line and in environments like eclipse, I can control which version is running. But from a double click I'm not sure.

I have installed versions 2.6 and 2.7. 2.6 for some applications, and I want to do 2.7 by default. I added "C: \ Python27" to the PATH environment variable and works well on the command line. C: \ path \ to \ some \ file> python someFile.py will run the file in 2.7. But if I double-clicked the same file from Explorer, it will launch 2.6. How to make it work 2.7?

+7
source share
4 answers

OK I found a Python Launcher that does exactly what I want. Download here . Setting this option gave me the option "Python Launcher for Windows (GUI)" when changing the file association via the right-click menu.

Adding a shebang line

#!/usr/bin/python2.7 

makes the script run in 2.7.

This works great, since I can control which version of python is running, and users never need to know. No need for bat files, or drag and drop on shortcuts, etc. Nice and clean, and most importantly, there is no room for user error.

+4
source

On Windows, you must change the file associations , for example, using the right-click → Open with ...Choose default program or the control panel Settings folder. You can choose between several python installations by going to python.exe using the browse button:

enter image description here

Alternatively, you can change the communication in the shell by typing

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

Please note that this requires administrator rights. If UAC is enabled on your computer, right-click cmd in the Start menu and select Run as administrator .

On operating systems compatible with freedesktop.org, you can configure communication with xdg-mime .

In debian based distributions, you can change python by default with update-alternatives . On all systems, you can also symbolize python in your path to the correct implementation, for example:

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

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

 #!/usr/bin/env python 

To select a specific version of Python for your program only, you can run your Python program using one of the following lines:

 #!/usr/bin/env python2.7 #!/usr/bin/python2.7 
+5
source

You can use ASSOC and FTYPE

 assoc .py=pyfile ftype pyfile=c:\Python27\python.exe %1 
+3
source

All Articles