如何控制双击文件时运行哪个版本的Python?

发布于 2024-12-28 18:39:16 字数 274 浏览 5 评论 0原文

有没有办法控制双击 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

≈。彩虹 2025-01-04 18:39:16

在 Windows 上,您必须修改文件关联,例如通过右键单击 → 打开with ...选择默认程序或控制面板的文件夹设置。您可以通过“浏览”按钮导航到所需的 python.exe,在多个 python 安装之间进行选择:

在此处输入图像描述

或者 来更改关联

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

,您可以通过在命令 shell 中键入“注意,这需要管理员权限” 。如果您的计算机上启用了 UAC,请右键单击开始菜单中的 cmd,然后选择以管理员身份运行

在与 freedesktop.org 兼容的桌面上,您可以配置与 的关联xdg-mime

在基于 debian 的发行版上,您可以使用 update-alternatives。在所有系统上,您还可以将路径中的 python 符号链接到正确的实现,如下所示:

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

如果文件被标记为可执行,则也可以直接从命令行或 GUI 执行(如果它以 #!< 开头) /code> 和解释器的名称:

#!/usr/bin/env python

要为您的程序选择特定的 Python 版本,您可以使用以下行之一启动您的 Python 程序:

#!/usr/bin/env python2.7
#!/usr/bin/python2.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:

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
你怎么敢 2025-01-04 18:39:16

好的,我找到了 Python Launcher,它正是我所追求的。可以在此处找到下载。通过右键单击菜单更改文件关联时,安装此程序为我提供了“Python Launcher for Windows (GUI)”选项。

添加 shebang 行

#!/usr/bin/python2.7

会强制脚本在 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

#!/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.

用心笑 2025-01-04 18:39:16

您可以使用 ASSOCFTYPE

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

You can use ASSOC and FTYPE

assoc .py=pyfile
ftype pyfile=c:\Python27\python.exe %1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文