Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control what version of Python is run when double clicking a file?

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?

like image 492
ollie dunn Avatar asked Jan 24 '12 11:01

ollie dunn


1 Answers

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
like image 72
phihag Avatar answered Oct 08 '22 01:10

phihag