如何控制双击文件时运行哪个版本的Python?
有没有办法控制双击 py 文件时运行哪个版本的 python?从命令行和 eclipse 等环境中,我可以控制运行的版本。但通过双击我不确定。
我安装了2.6和2.7。 2.6 适用于某些特定于应用程序的内容,我想将 2.7 设置为默认值。我已将“C:\Python27”添加到 PATH 环境变量中,并且在命令行中运行良好。 C:\path\to\some\file>python someFile.py 将在 2.7 中运行该文件。但如果我从资源管理器中双击同一个文件,它会运行 2.6。如何让它运行2.7?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows 上,您必须修改文件关联,例如通过右键单击 →
打开with ...
→选择默认程序
或控制面板的文件夹设置。您可以通过“浏览”按钮导航到所需的 python.exe,在多个 python 安装之间进行选择:或者 来更改关联
,您可以通过在命令 shell 中键入“注意,这需要管理员权限” 。如果您的计算机上启用了 UAC,请右键单击开始菜单中的 cmd,然后选择
以管理员身份运行
。在与 freedesktop.org 兼容的桌面上,您可以配置与
的关联xdg-mime
。在基于 debian 的发行版上,您可以使用
update-alternatives
。在所有系统上,您还可以将路径中的 python 符号链接到正确的实现,如下所示:
如果文件被标记为可执行,则也可以直接从命令行或 GUI 执行(如果它以
#!< 开头) /code> 和解释器的名称:
要为您的程序选择特定的 Python 版本,您可以使用以下行之一启动您的 Python 程序:
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
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: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:To choose a specific Python version just for your program, you can start your Python program with one of the following lines:
好的,我找到了 Python Launcher,它正是我所追求的。可以在此处找到下载。通过右键单击菜单更改文件关联时,安装此程序为我提供了“Python Launcher for Windows (GUI)”选项。
添加 shebang 行
会强制脚本在 2.7 中运行。
这非常有用,因为我可以控制正在运行的 python 版本,而用户永远不需要知道。无需bat文件,或拖动到快捷方式等。干净整洁,最重要的是,没有用户错误的余地。
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
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.
您可以使用
ASSOC
和FTYPE
You can use
ASSOC
andFTYPE
您将在以下问题的答案中找到答案:
如何在 Windows 中执行 python 脚本?
在 Windows 上安装 Python 2.x 和 3.x 时使用旧版 Python 2.x
单个 cmd.exe 会话的临时文件关联
如何编写一个批处理文件,显示可执行文件的路径和在 Windows 上处理 Python 脚本的 Python 版本?
You will find answer in answers to the following questions:
How to execute python scripts in windows?
Using older Python 2.x while having both Python 2.x and 3.x installed on Windows
Temporary file association for single cmd.exe session
How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?