使用 Launchd 运行 Python 脚本:未找到导入 [英] Running Python Script with Launchd: imports not found
问题描述
我一直在尝试设置一个脚本,以使用 launchd
定期运行 Python 程序.python 程序失败是因为它找不到我的导入——我知道这是因为我在日志文件中发现了错误.为了解决这个问题,我创建了 ~\.MacOSX\environment.plist
,并在那里复制了我的 PYTHONPATH
,注销并再次登录.这似乎不足以解决问题,我不知道还能尝试什么.
I've been trying to set up a script to run a python program at regular intervals using launchd
. The python program fails because it can't find my imports---I know this because I caught the errors in a log file. To fix this problem, I created ~\.MacOSX\environment.plist
, and duplicated my PYTHONPATH
there, logged out and logged in again. This seems to be insufficient to solve the problem, and I'm at a loss as to what else to try.
我运行的是 OSX 10.8.3.
I'm running OSX, 10.8.3.
相关主题:
- launchd 执行 python 脚本,但导入失败(不适用--我的脚本指向正确的python实例)
- launchd executes python script, but import fails (not applicable--my script points to the right python instance)
更新:
看来我可以运行以下命令:
It appears that I can run the following command:
launchctl setenv PYTHONPATH $PYTHONPATH
并且脚本将成功执行.所以,修改我的问题:
and the script will execute successfully. So, to modify my question:
- 它存储在哪里?我检查了
~\.launchd.conf
和\etc\.launchd.conf
,都不存在. - 大概在我重新启动时会转储此设置.我在哪里可以更改此信息,以便
launchd
可以找到它?
- Where does this get stored? I checked
~\.launchd.conf
and\etc\.launchd.conf
, neither existed. - Presumably this setting is dumped when I reboot. Where can I change this information so that
launchd
will find it?
推荐答案
要设置特定作业的环境,您应该使用作业定义本身中的 EnvironmentVariables
键:
To set the environment of a specific job you should use the EnvironmentVariables
key in the job definition itself:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.app</string>
<key>Program</key>
<string>/path/to/your/script</string>
<key>EnvironmentVariables</key>
<dict>
<key>PYTHONPATH</key>
<string>/your/python/path</string>
</dict>
</dict>
</plist>
您可以通过编辑守护进程的 /etc/launchd.conf
或 /etc/launchd-user 来为
代理.后者有效,但没有记录.当前记录(但不受支持)的每用户配置文件是 launchd(8)
服务定义默认环境变量.conf$HOME/.launchd.conf
.
You may define default environment variables for launchd(8)
services by editing /etc/launchd.conf
for daemons or /etc/launchd-user.conf
for agents. The latter one works but is not documented. The currently documented (but unsupported) per-user config file is $HOME/.launchd.conf
.
这些配置文件包含一系列 launchctl(1)
子命令.你想要的是:
These config files contain a list of launchctl(1)
subcommands. The one you want is:
setenv PYTHONPATH /your/python/path
更新: /etc/launchd.conf
在 Mac OS X 10.10 及更高版本中不受支持.在这些系统上,您必须基于每个作业定义环境变量.
Update: /etc/launchd.conf
is not supported in Mac OS X 10.10 and higher. On these systems you'll have to define environment variables on a per-job-basis.
这篇关于使用 Launchd 运行 Python 脚本:未找到导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!