launchd执行python脚本,但导入失败
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能是系统 Python (
/usr/bin/python
) 正在启动来执行您的脚本,而不是 MacPorts Python (/opt/local/bin/python2.6
) >) 您安装了appscript
的位置。应该有效的(未经测试!)是将 MacPorts Python 路径作为第一个程序参数
插入到脚本路径之前。在这种情况下,您不需要指定PYTHONPATH
。理论上,只要 MacPorts Python 配置与系统 Python 兼容(即类似的架构、部署目标等),您就可以使您所拥有的功能正常工作,但您可能不希望或不需要走这条路。另一种方法是将脚本的 shebang 行(第一行)更改为 MacPorts Python 的显式路径:
这在命令行 shell 中起作用的原因可能是您的 shell 配置文件之一,例如
. bash_profile
,首先修改PATH
环境变量以包含 MacPorts Python 的路径 (/opt/local/bin
),以便/usr/ bin/env python
首先找到 MacPythonpython
。当通过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 haveappscript
installed. What should work (untested!) is to insert the MacPorts Python path as the firstProgram Argument
, before the path to the script. And in that case you shouldn't need to specifyPYTHONPATH
. 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:
The reason this works in a command line shell is likely that one of your shell profile files, say
.bash_profile
, modifies thePATH
environment variable to include the path to the MacPorts Python (/opt/local/bin
) first so that/usr/bin/env python
finds the MacPythonpython
first. When run throughlaunchd
, the shell is not involved so thatPATH
manipulation does not happen; just the default paths are searched, meaning/usr/bin/env python
executes/usr/bin/python
.您还可以在 ProgramArguments 中使用 python 的完整路径。 (更多详细信息请参见:使用 Launchd 运行 Python 脚本:导入未找到)
You can also use the full path to python in ProgramArguments. (More details here: Running Python Script with Launchd: imports not found)