简体   繁体   中英

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?

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:

在此输入图像描述

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

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.

You can use ASSOC and FTYPE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM