如何使python脚本在osx上可执行? [英] How to make python script executable on osx?

查看:118
本文介绍了如何使python脚本在osx上可执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将我的脚本作为应用程序.双击运行,而不是在终端中运行.我以前用automator做到了,但是现在,在capitan上它是行不通的.它只会给出错误而无需解释.

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时,出现此错误:

When I try with automator I get this error:

"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:

这两个步骤之后.我只为此将打开方式"更改为终端 文件并更改#!/usr/bin/env python,它可以工作.但是,如果没有以下两个步骤,它将无法正常工作,您需要遵循所有步骤 脚步.

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.之后,更改应用程序 打开终端.

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

对我有用.

推荐答案

假设已安装Python,则此方法应该有效:

Assuming Python is installed, this should work:

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

选择PythonLauncher作为默认应用程序以打开脚本 (或任何.py脚本)通过finder信息窗口并双击 它. PythonLauncher具有各种首选项来控制脚本的方式 启动.拖动选项可将它们更改为一个 调用,或使用其偏好设置"菜单全局更改.

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/

最新版本的Mac OS X El Capitan随附Python 2.7 的盒子.

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

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

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.

OS X附带的Python版本非常适合学习,但 这对发展不利.

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

附录2:

Apple对El Capitan进行了一些更改(包括

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:

担心:

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

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

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

这篇关于如何使python脚本在osx上可执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆