简体   繁体   English

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

[英]launchd executes python script, but import fails

I wrote a python script using appscript to track my currently active window. 我使用appscript编写了一个python脚本来跟踪我当前活动的窗口。 I am running it via launchd, but when I do that it can't import appscript. 我通过launchd运行它,但是当我这样做时它无法导入appscript。 I have set the PYTHONPATH in the plist for launchd, but I think launchd is not reading .pth files in site-packages. 我已将plY中的PYTHONPATH设置为launchd,但我认为launchd不是在site-packages中读取.pth文件。 Is there a way to make it do that? 有没有办法让它做到这一点?

My script is here: https://github.com/katylava/macwintracker 我的脚本在这里: https//github.com/katylava/macwintracker

This is the launchd plist: 这是推出的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. 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. 机会是系统的Python( /usr/bin/python )正在启动执行脚本而不是Python的MacPorts的( /opt/local/bin/python2.6你在哪里) appscript安装。 What should work (untested!) is to insert the MacPorts Python path as the first Program Argument , before the path to the script. 什么应该工作(未经测试!)是在脚本路径之前插入MacPorts Python路径作为第一个Program Argument And in that case you shouldn't need to specify PYTHONPATH . 在这种情况下,您不需要指定PYTHONPATH In theory, you might be able to make what you have work as long as the MacPorts Python was configured compatibly (ie similar archs, deployment targets, etc) with the system Python but you probably shouldn't want or need to go down that path. 理论上,只要MacPorts Python与系统Python兼容地配置(即类似的arch,部署目标等),你就可以完成你的工作,但你可能不应该或者需要沿着这条路走下去。

Another approach would be to change the shebang line (the first line) of the script to an explict path to the MacPorts Python: 另一种方法是将脚本的shebang行(第一行)更改为MacPorts Python的explict路径:

#!/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. 在命令行shell中工作的原因很可能是你的一个shell配置文件,比如.bash_profile ,修改了PATH环境变量,首先包含了MacPorts Python的路径( /opt/local/bin ),以便/usr/bin/env python找到MacPython python When run through launchd , the shell is not involved so that PATH manipulation does not happen; 当运行launchd ,不涉及shell,因此不会发生PATH操作; just the default paths are searched, meaning /usr/bin/env python executes /usr/bin/python . 只搜索默认路径,这意味着/usr/bin/env python执行/usr/bin/python

You can also use the full path to python in ProgramArguments. 您还可以在ProgramArguments中使用python的完整路径。 (More details here: Running Python Script with Launchd: imports not found ) (更多细节在这里: 使用Launchd运行Python脚本:未找到导入

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM