macos 如何在 Mac OS X 上将 py 扩展名与 python 启动器相关联?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1185817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:17:55  来源:igfitidea点击:

How to associate py extension with python launcher on Mac OS X?

pythonmacos

提问by Stephen Gross

Does anyone know how to associate the py extension with the python interpreter on Mac OS X 10.5.7? I have gotten as far as selecting the application with which to associate it (/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python), but the python executable appears as a non-selectable grayed-out item. Any ideas?

有谁知道如何将 py 扩展名与 Mac OS X 10.5.7 上的 python 解释器相关联?我已经选择了与之关联的应用程序 (/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python),但 python 可执行文件显示为不可选择的灰色项目。有任何想法吗?

回答by Ned Deily

The python.org OS X Python installers include an application called "Python Launcher.app" which does exactly what you want. It gets installed into /Applications /Python n.n/ for n.n > 2.6 or /Applications/MacPython n.n/ for 2.5 and earlier. In its preference panel, you can specify which Python executable to launch; it can be any command-line path, including the Apple-installed one at /usr/bin/python2.5. You will also need to ensure that .py is associated with "Python Launcher"; you can use the Finder's Get Info command to do that as described elsewhere. Be aware, though, that this could be a security risk if downloaded .py scripts are automatically launched by your browser(s). (Note, the Apple-supplied Python in 10.5 does not include "Python Launcher.app").

python.org OS X Python 安装程序包含一个名为“Python Launcher.app”的应用程序,它完全符合您的要求。它被安装到 /Applications /Python nn/ for nn > 2.6 或 /Applications/MacPython nn/ for 2.5 及更早版本。在其首选项面板中,您可以指定要启动的 Python 可执行文件;它可以是任何命令行路径,包括 Apple 在 /usr/bin/python2.5 安装的路径。您还需要确保 .py 与“Python Launcher”相关联;您可以使用 Finder 的“获取信息”命令执行此操作,如别处所述。但是请注意,如果您的浏览器自动启动下载的 .py 脚本,这可能会带来安全风险。(请注意,Apple 在 10.5 中提供的 Python 不包括“Python Launcher.app”)。

回答by S.Lott

The file associations are done with the "Get Info". You select your .PY file, select the File menu; Get Info menu item.

文件关联是通过“获取信息”完成的。你选择你的 .PY 文件,选择 File 菜单;获取信息菜单项。

Mid-way down the Get Info page is "Open With".

“获取信息”页面的中间是“打开方式”。

You can pick the Python Launcher. There's a Change All..button that changes the association for all .pyfiles.

您可以选择 Python Launcher。有一个Change All..按钮可以更改所有.py文件的关联。

回答by hobodave

Steve, add the following to the top of your python script:

史蒂夫,将以下内容添加到您的 Python 脚本的顶部:

#!/usr/bin/env python

It must occur as the first line of the file.

它必须出现在文件的第一行。

Then make the file executable as so:

然后使文件可执行,如下所示:

daves-macbookpro ~: chmod +x foo.py

Then all you need to do to run this is type

然后你需要做的就是运行它

./foo.py

回答by cde

The default python installation (atleast on 10.6.8) includes the Python Launcher.appin /System/Library/Frameworks/Python.framework/Resources/, which is aliased to the latest/current version of Python installed on the system. This application launches terminal and sets the right environment to run the script.

默认的 python 安装(至少在 10.6.8 上)包括Python Launcher.appin /System/Library/Frameworks/Python.framework/Resources/,它是系统上安装的最新/当前版本的 Python 的别名。此应用程序启动终端并设置正确的环境来运行脚本。