使用 Launchd 运行 Python 脚本:未找到导入 [英] Running Python Script with Launchd: imports not found

查看:93
本文介绍了使用 Launchd 运行 Python 脚本:未找到导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设置一个脚本,以使用 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 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:

  1. 它存储在哪里?我检查了 ~\.launchd.conf\etc\.launchd.conf,都不存在.
  2. 大概在我重新启动时会转储此设置.我在哪里可以更改此信息,以便 launchd 可以找到它?
  1. Where does this get stored? I checked ~\.launchd.conf and \etc\.launchd.conf, neither existed.
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆