launchd执行python脚本,但导入失败

发布于 2024-10-31 00:53:50 字数 1868 浏览 2 评论 0原文

我使用 appscript 编写了一个 python 脚本来跟踪我当前活动的窗口。我通过 launchd 运行它,但是当我这样做时,它无法导入 appscript。我已经在 launchd 的 plist 中设置了 PYTHONPATH,但我认为 launchd 没有读取站点包中的 .pth 文件。有没有办法让它做到这一点?

我的脚本在这里: https://github.com/katylava/macwintracker

这是启动的 plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>label</key>
    <string>com.katylavallee.wintracker</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py</string>
        <string>1</string>
        <string>1</string>
    </array>
    <key>Environment Variables</key>
    <dict>
      <key>PYTHONPATH</key>
      <string>/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages</string>
    </dict>
    <key>StandardErrorPath</key>
    <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker_err.log</string>
    <key>StandardOutPath</key>
    <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker.log</string>
    <key>StartInterval</key>
    <integer>3</integer>
  </dict>
</plist>

:错误:

Traceback (most recent call last):
  File "/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py", line 5, in <module>
    from appscript import app, its
ImportError: No module named appscript

Python 脚本在命令行中工作正常。

I wrote a python script using appscript to track my currently active window. I am running it via launchd, but when I do that it can't import appscript. I have set the PYTHONPATH in the plist for launchd, but I think launchd is not reading .pth files in site-packages. Is there a way to make it do that?

My script is here: https://github.com/katylava/macwintracker

This is the launchd plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>label</key>
    <string>com.katylavallee.wintracker</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py</string>
        <string>1</string>
        <string>1</string>
    </array>
    <key>Environment Variables</key>
    <dict>
      <key>PYTHONPATH</key>
      <string>/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages</string>
    </dict>
    <key>StandardErrorPath</key>
    <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker_err.log</string>
    <key>StandardOutPath</key>
    <string>/Users/kyl/Library/Logs/com.katylavallee.wintracker/wintracker.log</string>
    <key>StartInterval</key>
    <integer>3</integer>
  </dict>
</plist>

And the error:

Traceback (most recent call last):
  File "/Users/kyl/Library/Application Support/com.katylavallee.wintracker/wintracker.py", line 5, in <module>
    from appscript import app, its
ImportError: No module named appscript

The python script works fine from the command line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甩你一脸翔 2024-11-07 00:53:50

有可能是系统 Python (/usr/bin/python) 正在启动来执行您的脚本,而不是 MacPorts Python (/opt/local/bin/python2.6) >) 您安装了 appscript 的位置。应该有效的(未经测试!)是将 MacPorts Python 路径作为第一个程序参数插入到脚本路径之前。在这种情况下,您不需要指定 PYTHONPATH。理论上,只要 MacPorts Python 配置与系统 Python 兼容(即类似的架构、部署目标等),您就可以使您所拥有的功能正常工作,但您可能不希望或不需要走这条路。

另一种方法是将脚本的 shebang 行(第一行)更改为 MacPorts Python 的显式路径:

#!/opt/local/bin/python2.6

这在命令行 shell 中起作用的原因可能是您的 shell 配置文件之一,例如 . bash_profile,首先修改 PATH 环境变量以包含 MacPorts Python 的路径 (/opt/local/bin),以便 /usr/ bin/env python 首先找到 MacPython python。当通过 launchd 运行时,不涉及 shell,因此不会发生 PATH 操作;仅搜索默认路径,这意味着 /usr/bin/env python 执行 /usr/bin/python

Chances are that the system Python (/usr/bin/python) is being launched to execute your script rather than the MacPorts Python (/opt/local/bin/python2.6) where you have appscript installed. What should work (untested!) is to insert the MacPorts Python path as the first Program Argument, before the path to the script. And in that case you shouldn't need to specify PYTHONPATH. In theory, you might be able to make what you have work as long as the MacPorts Python was configured compatibly (i.e. similar archs, deployment targets, etc) with the system Python but you probably shouldn't want or need to go down that path.

Another approach would be to change the shebang line (the first line) of the script to an explict path to the MacPorts Python:

#!/opt/local/bin/python2.6

The reason this works in a command line shell is likely that one of your shell profile files, say .bash_profile, modifies the PATH environment variable to include the path to the MacPorts Python (/opt/local/bin) first so that /usr/bin/env python finds the MacPython python first. When run through launchd, the shell is not involved so that PATH manipulation does not happen; just the default paths are searched, meaning /usr/bin/env python executes /usr/bin/python.

水水月牙 2024-11-07 00:53:50

您还可以在 ProgramArguments 中使用 python 的完整路径。 (更多详细信息请参见:使用 Launchd 运行 Python 脚本:导入未找到

   <key>ProgramArguments</key>
    <string>/path/to/your/python</string>
    <string>/path/to/your/script</string>

You can also use the full path to python in ProgramArguments. (More details here: Running Python Script with Launchd: imports not found)

   <key>ProgramArguments</key>
    <string>/path/to/your/python</string>
    <string>/path/to/your/script</string>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文