如何使 python 脚本在 osx 上可执行?
How to make python script executable on osx?
我只是想把我的脚本做成一个应用程序。双击并在终端中 运行 而不是 运行ning。我以前用 automator 做过,但现在,在 el capitan 上它不起作用。只报错不解释。
当我尝试使用 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
在终端中。之后更改应用程序
打开终端。
对我有用。
假设安装了 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:
Apple 对 El Capitan(包括 System Integrity Protection)进行了一些更改,这些更改可能会导致安装失败并出现臭名昭著的 "no software found to install"。例如:
解决方法:
使用自制软件。这正是我上面引用的 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
然后在 文件的顶部添加了以下行-name.py
#!/usr/bin/env python
然后通过 运行ning ./file-name.py
命令 运行 文件,它工作正常。
快速 step-by-step 创建可点击的 .app 以启动您的 python 脚本。
启动 Apple ScriptEditor(位于 /Applications/Utilities/)并在编辑器中输入以下内容:
tell application "Terminal"
do script with command "python /path/to/your/script.py"
end tell
之后,只需点击保存并选择保存为应用程序。
Ps.: 如果阅读本文的人知道如何摆脱启动 .app 时打开的终端 window,请告诉我。
如果您想更高级,请查看 Platypus。
@https://whosebug.com/users/7885903/lucas-mendes-mota-da-fonseca
要隐藏终端 window,我相信你可以将 .py 重命名为 .pyw 并调用它。
我只是想把我的脚本做成一个应用程序。双击并在终端中 运行 而不是 运行ning。我以前用 automator 做过,但现在,在 el capitan 上它不起作用。只报错不解释。
当我尝试使用 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
在终端中。之后更改应用程序 打开终端。对我有用。
假设安装了 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:
Apple 对 El Capitan(包括 System Integrity Protection)进行了一些更改,这些更改可能会导致安装失败并出现臭名昭著的 "no software found to install"。例如:
解决方法:
使用自制软件。这正是我上面引用的 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
然后在 文件的顶部添加了以下行-name.py
#!/usr/bin/env python
然后通过 运行ning ./file-name.py
命令 运行 文件,它工作正常。
快速 step-by-step 创建可点击的 .app 以启动您的 python 脚本。
启动 Apple ScriptEditor(位于 /Applications/Utilities/)并在编辑器中输入以下内容:
tell application "Terminal"
do script with command "python /path/to/your/script.py"
end tell
之后,只需点击保存并选择保存为应用程序。
Ps.: 如果阅读本文的人知道如何摆脱启动 .app 时打开的终端 window,请告诉我。
如果您想更高级,请查看 Platypus。
@https://whosebug.com/users/7885903/lucas-mendes-mota-da-fonseca
要隐藏终端 window,我相信你可以将 .py 重命名为 .pyw 并调用它。