简体   繁体   English

使用Launchd运行Python脚本:未找到导入

[英]Running Python Script with Launchd: imports not found

I've been trying to set up a script to run a python program at regular intervals using launchd . 我一直在尝试设置一个脚本,使用launchd定期运行python程序。 The python program fails because it can't find my imports---I know this because I caught the errors in a log file. python程序失败,因为它无法找到我的导入---我知道这是因为我在日志文件中捕获了错误。 To fix this problem, I created ~\\.MacOSX\\environment.plist , and duplicated my PYTHONPATH there, logged out and logged in again. 为了解决这个问题,我创建了~\\.MacOSX\\environment.plist ,并在那里复制了我的PYTHONPATH ,注销并再次登录。 This seems to be insufficient to solve the problem, and I'm at a loss as to what else to try. 这似乎不足以解决问题,我不知道还有什么可以尝试。

I'm running OSX, 10.8.3. 我正在运行OSX,10.8.3。

Related threads: 相关主题:

UPDATE: 更新:

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. Where does this get stored? 这存储在哪里? I checked ~\\.launchd.conf and \\etc\\.launchd.conf , neither existed. 我查了~\\.launchd.conf\\etc\\.launchd.conf ,都不存在。
  2. Presumably this setting is dumped when I reboot. 据推测,重新启动时会转储此设置。 Where can I change this information so that launchd will find it? 我在哪里可以更改此信息,以便launchd能够找到它?

To set the environment of a specific job you should use the EnvironmentVariables key in the job definition itself: 要设置特定作业的环境,您应该在作业定义本身中使用EnvironmentVariables键:

<?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>

You may define default environment variables for launchd(8) services by editing /etc/launchd.conf for daemons or /etc/launchd-user.conf for agents. 您可以通过编辑用于守护进程的/etc/launchd-user.conf或代理的/etc/launchd.conf来为launchd(8)服务定义默认环境变量。 The latter one works but is not documented. 后者可以工作,但没有记录。 The currently documented (but unsupported) per-user config file is $HOME/.launchd.conf . 当前记录的(但不受支持的)每用户配置文件是$HOME/.launchd.conf

These config files contain a list of launchctl(1) subcommands. 这些配置文件包含launchctl(1)子命令的列表。 The one you want is: 你想要的是:

setenv PYTHONPATH /your/python/path

Update: /etc/launchd.conf is not supported in Mac OS X 10.10 and higher. 更新: Mac OS X 10.10及更高版本不支持/etc/launchd.conf On these systems you'll have to define environment variables on a per-job-basis. 在这些系统上,您必须基于每个作业定义环境变量。

None of the above actually worked for me (OS X 10.11.3). 以上都不适合我(OS X 10.11.3)。 The breakthrough was reading this script runs fine in terminal but not from launchd and realizing belatedly that one can write the absolute full path to the version of python with the right modules. 突破是读取这个脚本在终端运行良好,但不是从launchd运行并且意识到迟到的人可以使用正确的模块编写python版本的绝对完整路径。 D'oh. D'哦。

<?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>ProgramArguments</key>
    <string>/path/to/your/python</string>
    <string>/path/to/your/script</string>
</dict>
</plist>

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

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