[英]How to make python script executable on osx?
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.
我以前用 automator 完成过,但现在,在 el capitalan 上它不起作用。 It only gives error without explanation.
它只给出错误而没有解释。
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.我将“打开方式”更改为仅此文件的终端并更改了
#!/usr/bin/env python
,它可以工作。 But it doesn't work without the two steps below, you need to follow all steps.但是如果没有下面的两个步骤,它就不起作用,您需要按照所有步骤进行操作。
Add
#!/usr/local/bin/python
in the beginning of the code.在代码开头添加
#!/usr/local/bin/python
。 Then runchmod +x myscript.py
in terminal.然后在终端中运行
chmod +x myscript.py
。 After that change the application open with to Terminal.之后,将打开的应用程序更改为终端。
It worked for me.
它对我有用。
I have changed the mode by我已经改变了模式
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
Then run the file by running ./file-name.py
command and it works fine.然后通过运行
./file-name.py
命令运行文件,它工作正常。
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 。
Assuming Python is installed, this should work:假设安装了 Python,这应该可以工作:
https://docs.python.org/2/using/mac.html
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 作为默认应用程序以通过 finder 信息窗口打开您的脚本(或任何 .py 脚本)并双击它。 PythonLauncher has various preferences to control how your script is launched.
PythonLauncher 有各种首选项来控制脚本的启动方式。 Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.
选项拖动允许您为一次调用更改这些,或使用其首选项菜单全局更改内容。
ADDENDUM:附录:
http://docs.python-guide.org/en/latest/starting/install/osx/
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.
最新版本的 Mac OS X El Capitan 附带了 Python 2.7 开箱即用。
You do not need to install or configure anything else to use Python.
您无需安装或配置任何其他东西即可使用 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.
话虽如此,我强烈建议您在开始构建实际使用的 Python 应用程序之前安装下一节中描述的工具和库。 In particular, you should always install Setuptools, as it makes it much easier for you to use other third-party Python libraries.
特别是,您应该始终安装 Setuptools,因为它可以让您更轻松地使用其他第三方 Python 库。
The version of Python that ships with OS X is great for learning but it's not good for development.
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". Apple 对 El Capitan(包括系统完整性保护)进行了一些更改,这可能会导致安装失败,并出现臭名昭著的“找不到要安装的软件”。 For example:
例如:
WORKAROUND:解决方法:
Use Homebrew.使用自制软件。 Which is exactly what the Installing Python on Mac OS X I 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.如果这对您不起作用,请告诉我。
@https://stackoverflow.com/users/7885903/lucas-mendes-mota-da-fonseca @https://stackoverflow.com/users/7885903/lucas-mendes-mota-da-fonseca
To hide the terminal window, I believe you could rename the .py to .pyw and call that.要隐藏终端窗口,我相信您可以将 .py 重命名为 .pyw 并调用它。
https://stackoverflow.com/a/34739687/6713477 https://stackoverflow.com/a/34739687/6713477
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.