How to make python script executable on osx?
我只想将我的脚本作为应用程序。 双击运行,而不是在终端中运行。 我以前用automator做到了,但现在在capitan上不起作用。 它只给出错误而没有解释。
当我尝试使用automator时,出现此错误:
1 | "The action"Run Shell Script" encountered an error." |
我也尝试了以下技巧,但仍然无法做到这一点。
1 2 3 | #!/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 。 之后,更改应用程序
打开终端。它为我工作。
blockquote>
我已经改变了模式
sudo chmod +x file-name.py 然后在file-name.py顶部添加以下行
#!/usr/bin/env python 然后通过运行
./file-name.py 命令运行文件,它可以正常工作。
假设安装了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:
苹果对El Capitan进行了一些更改(包括系统完整性保护),可能会因臭名昭著的"未找到要安装的软件"而导致安装失败。例如:
- http://trac.wxwidgets.org/ticket/17203
解决方法:
使用自制软件。我上面引用的在Mac OS X上安装Python正是建议的以下内容:
1
2
3
4
5
6
7 $ /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如果这不适合您,请告诉我。
快速逐步创建可点击的.app以启动python脚本。
启动Apple ScriptEditor(位于/ Applications / Utilities /中),然后在编辑器中键入以下内容:
1
2
3 tell application"Terminal"
do script with command"python /path/to/your/script.py"
end tell之后,只需单击保存并选择另存为应用程序。
附言:如果任何人读这本书都知道如何摆脱启动.app时打开的终端窗口,请告诉我。
如果您想获得更高级的知识,请查看鸭嘴兽。