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

2024-03-28 17:16:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想把我的脚本作为一个应用程序。双击并运行,而不是在终端中运行。我以前用过自动机,但现在,在埃尔卡皮坦,它不起作用。它只会给出错误而没有解释。

当我尝试使用automator时,会出现以下错误:

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

我也试过下面的把戏,还是做不到。

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

在代码开头添加#!/usr/local/bin/python。那就跑吧 chmod +x myscript.py在终端。之后更改应用程序 打开到终端。

这对我有效。


Tags: thetopyenv脚本应用程序终端bin
3条回答

快速逐步创建clickable.app以启动您的python脚本。

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

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

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

注:如果有人读到这篇文章,知道如何摆脱你启动.app时打开的终端窗口,请告诉我。

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

假设安装了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.

附录:

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.

附录2:

苹果在ElCapitan做了一些改变(包括System Integrity Protection),这可能会导致安装失败,因为臭名昭著的“找不到可安装的软件”。例如:

解决方法:

使用自制。这正是我上面引用的Installing Python on Mac OS X所建议的:

$ /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

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

我改变了模式

sudo chmod +x file-name.py

然后在文件名.py的顶部添加以下行

#!/usr/bin/env python

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

相关问题 更多 >