如何使python脚本在osx上可执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36564320/
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-08-19 18:02:20  来源:igfitidea点击:

How to make python script executable on osx?

pythonmacosshellexecutabledouble-click

提问by hzleonardo

I just want to make my script as an application. double-click and run instead of running in terminal. I have done it before with automator but now, on el capitan it doesn't work. It only gives error without explanation.

我只想将我的脚本制作为应用程序。双击运行而不是在终端中运行。我以前用 automator 完成过,但现在,在 el capitalan 上它不起作用。它只给出错误而没有解释。

When I try with automator I get this error:

当我尝试使用 automator 时,出现此错误:

"The action “Run Shell Script” encountered an error."

Also I tried the trick below, still I am not able to do this.

我也尝试了下面的技巧,但我仍然无法做到这一点。

#!/usr/bin/env python

chmod +x script.py

SOLVED:

After these two steps. I changed "Open with" to terminal for only this file and changed the #!/usr/bin/env python, it works. But it doesn't work without the two steps below, you need to follow all steps.

Add #!/usr/local/bin/pythonin the beginning of the code. Then run chmod +x myscript.pyin terminal. After that change the application open with to Terminal.

It worked for me.

解决了:

经过这两个步骤。我将“打开方式”更改为仅此文件的终端并更改了#!/usr/bin/env python,它可以工作。但是如果没有下面的两个步骤,它就不起作用,您需要按照所有步骤进行操作。

添加#!/usr/local/bin/python在代码的开头。然后chmod +x myscript.py在终端运行 。之后,将打开的应用程序更改为终端。

它对我有用。

采纳答案by paulsm4

Assuming Python is installed, this should work:

假设安装了 Python,这应该可以工作:

https://docs.python.org/2/using/mac.html

Select PythonLauncher as the default application to open your script (or any .py script) through the finder Info window and double-click it. PythonLauncher has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.

https://docs.python.org/2/using/mac.html

选择 PythonLauncher 作为默认应用程序以通过 finder 信息窗口打开您的脚本(或任何 .py 脚本)并双击它。PythonLauncher 有各种首选项来控制脚本的启动方式。选项拖动允许您为一次调用更改这些,或使用其首选项菜单全局更改内容。

ADDENDUM:

附录:

http://docs.python-guide.org/en/latest/starting/install/osx/

The latest version of Mac OS X, El Capitan, comes with Python 2.7 out of the box.

You do not need to install or configure anything else to use Python. Having said that, I would strongly recommend that you install the tools and libraries described in the next section before you start building Python applications for real-world use. In particular, you should always install Setuptools, as it makes it much easier for you to use other third-party Python libraries.

The version of Python that ships with OS X is great for learning but it's not good for development.

http://docs.python-guide.org/en/latest/starting/install/osx/

最新版本的 Mac OS X El Capitan 附带了 Python 2.7 开箱即用。

您无需安装或配置任何其他东西即可使用 Python。话虽如此,我强烈建议您在开始构建 Python 应用程序以供实际使用之前安装下一节中描述的工具和库。特别是,您应该始终安装 Setuptools,因为它可以让您更轻松地使用其他第三方 Python 库。

OS X 附带的 Python 版本非常适合学习,但不适合开发。

ADDENDUM 2:

附录 2:

Apple made some changes in El Capitan (including System Integrity Protection) that could cause installs to fail with the infamous "no software found to install". For example:

Apple 对 El Capitan(包括系统完整性保护)进行了一些更改,这可能会导致安装失败,并出现臭名昭著的“找不到要安装的软件”。例如:

WORKAROUND:

解决方法:

Use Homebrew. Which is exactly what the Installing Python on Mac OS XI cited above recommends:

使用自制软件。这正是我上面引用的在 Mac OS X 上安装 Python 的建议:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ vi ~/.profile => 
...  
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

$ brew install python

Please let me know if this doesn't work for you.

如果这对您不起作用,请告诉我。

回答by Akhter-uz-zaman

I have changed the mode by

我已经改变了模式

sudo chmod +x file-name.py

sudo chmod +x file-name.py

Then Added the following line on top of the file-name.py

然后在file-name.py顶部添加以下行

#!/usr/bin/env python

#!/usr/bin/env python

Then run the file by running ./file-name.pycommand and it works fine.

然后通过运行./file-name.py命令运行文件,它工作正常。

回答by Lucas Mendes Mota Da Fonseca

Quick step-by-step to create clickable .app to launch your python scripts.

快速一步一步地创建可点击的 .app 来启动你的 Python 脚本。

Launch the Apple ScriptEditor (located in /Applications/Utilities/) and type in the following into the editor:

启动 Apple ScriptEditor(位于 /Applications/Utilities/)并在编辑器中输入以下内容:

tell application "Terminal"
    do script with command "python /path/to/your/script.py"
end tell

After, simply hit save and choose to save as an application.

之后,只需点击保存并选择另存为应用程序。

Ps.: If anyone reading this know how to get rid of the Terminal window that opens when you launch the .app, let me know.

Ps。:如果有人阅读本文知道如何摆脱启动 .app 时打开的终端窗口,请告诉我。

If you want to get more advanced, check out Platypus.

如果您想更高级,请查看Platypus