【问题标题】:Using Third-Party Modules with Python in an Automator Service在 Automator 服务中使用 Python 的第三方模块
【发布时间】:2011-01-06 19:42:57
【问题描述】:

我已经在我的机器上安装了 Py-Appscript,它可以与/Library/Frameworks/Python.framework/Versions/Current/bin/python 的 Python 安装一起使用。

我正在尝试将此安装的 Py-Appscript 与 Automator 服务一起使用。为此,我使用 Run Shell Script 操作,然后将 Shell 设置为 usr/bin/python(不幸的是,这是我对 Python 的唯一选择)。

usr/bin/python似乎无法访问我的第三方模块并在线崩溃:

from appscript import *

有没有办法让usr/bin/python 访问我的第三方模块? 要么 有没有办法告诉 Automator 改用/Library/Frameworks/Python.framework/Versions/Current/bin/python

我需要 Automator 直接从 Run Shell Script 操作运行 Python。任何调用 Automator 外部的 Python 脚本的操作(例如,通过 bin/bash)的执行速度都不够快,无法发挥作用。

【问题讨论】:

    标签: python automator


    【解决方案1】:

    好的,我能够使用在 How do I execute a PHP shell script as an Automator action on Mac OS X 找到的 hack 让它工作。

    Run Shell Script 操作中,我使用了 /bin/sh/ shell 和 <<EOF ... EOF 来正确安装 Python。

    例如,输入

    /Library/Frameworks/Python.framework/Versions/Current/bin/python <<EOF
    from appscript import *
    Numbers = app('Numbers')
    EOF
    

    进入Run Shell Script动作的代码部分将起作用。因此可以调用正确的安装(上面的/Library/Frameworks/Python.framework/Versions/Current/bin/python)并将他们的程序放在&lt;&lt;EOF ... EOF 分隔符之间。

    阿尔弗雷德

    这个技巧也适用于 Alfred。如果您想将 appscript 与 Alfred 一起使用,只需确保将 {query} 传递给上面的 python 版本,如下所示:

    /Library/Frameworks/Python.framework/Versions/Current/bin/python script.py {query}
    

    【讨论】:

    • 如果你打算这样做,那么你应该在脚本的 shebang 行中指定 Python 可执行文件,然后 chmod 脚本可执行文件。
    • 我实际上已经尝试过了,它的运行速度比上述方法慢。我认为原因是因为这两种方法都需要从硬盘驱动器访问Python.framework 安装,但上述方法至少不必从与 Automator 操作不同的硬盘驱动器位置访问代码。所以我认为上述方法需要 2 个硬盘检索(python 和 Automator),而您建议的方法需要 3 个(python、可执行 .py 文件和 Automator)。
    【解决方案2】:

    安装模块时,通常会为每个 Python 实例安装它们。因此,在这种情况下,您已在 /Library/Frameworks/Python.framework/Versions/Current/bin/python 中为 Python 安装了它们,然后它将仅适用于该 Python。 /usr/bin/python 显然是另一个 Python 安装(我不是 OS X 专家)。

    要使其可用于 /usr/bin/python 安装,请为 /usr/bin/python 安装它。

    【讨论】:

    • 感谢您的建议。不幸的是,我在终端中使用 sudo easy_install appscript 安装了 appscript,但我不知道如何将其重定向到为 /usr/bin/python 安装。
    • 好的,我可以尝试使用:sudo /usr/bin/python -m easy_install appscript/usr/bin/python 安装 appscript,但是,这遇到了错误:unable to execute gcc-4.2: No such file or directory 不幸的是,让我回到第一方。鉴于此,看起来我最好的选择是以某种方式让 Automator 使用 /Library/Frameworks/Python.framework/Versions/Current/bin/python
    • 我实际上已经安装了 Xcode。我是一名 iPhone 开发人员。
    • 普通命令是sudo /usr/bin/easy_install appscript,但你的也应该可以。也许您的 shell 配置文件有问题,或者您更改了系统中的某些内容?内置的 Python 需要 gcc-4.2 来构建扩展,它是 Xcode3.2 附带的。如果您安装了较旧的 Xcode 或更改了 gcc 设置,那么这可能是它找不到 [大概] gcc-4.2 的原因。
    猜你喜欢
    • 2021-07-24
    • 2021-01-10
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多