ImportError 仅适用于 crontab 作业?

ImportError only for crontab job?

所以我写了一个漂亮的自动化脚本,它通过 jira 模块 + 方便的花花公子逻辑为我完成了一些工作。它 运行 完全来自命令行:

me@local] ~/Documents/auto_updater > python /Users/me/Documents/auto_updater/jira_updater.py 
Missing info starting
  checking out:  ES-20157
    No array outage
    Already has sev1_missing_outage label.
***snip***

在没有 python 调用的情况下使用完整路径 运行 也可以正常工作:

me@local] ~/Documents/auto_updater > /Users/me/Documents/auto_updater/jira_updater.py 
Missing info starting
  checking out:  ES-20157
    No array outage
    Already has sev1_missing_outage label.

现在 - 假设它工作意味着生活美好,我决定在 crontab 上设置它 30 分钟,并且每次似乎找不到 jira 模块时我都会收到垃圾邮件失败:

From me@me-mbp  Mon Oct 17 19:30:04 2016
Return-Path: <me@me-mbp>
X-Original-To: me
Delivered-To: me@me-mbp
Received: by me-mbp (Postfix, from userid 502)
    id 514D0203328A; Mon, 17 Oct 2016 19:30:00 -0600 (MDT)
From: me@me-mbp (Cron Daemon)
To: me@me-mbp
Subject: Cron <me@local> python /Users/me/Documents/auto_updater/jira_updater.py >> /Users/me/Documents/auto_updater/updated_log.txt
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=me>
X-Cron-Env: <USER=me>
X-Cron-Env: <HOME=/Users/me>
Message-Id: <20161018013004.514D0203328A@micheal.taylor-mbp>
Date: Mon, 17 Oct 2016 19:30:00 -0600 (MDT)

Traceback (most recent call last):
  File "/Users/me/Documents/auto_updater/jira_updater.py", line 3, in <module>
    from jira.client import JIRA
ImportError: No module named jira.client

本来#!/usr/bin/python路径有问题,所以改成#!/usr/bin/env python:

[me@local] ~/Documents/auto_updater > head jira_updater.py 
#!/usr/bin/env python

from jira.client import JIRA
import random

# Here's some responses that we can randomize from so it doesn't feel quite so botty.
FIRST_RESPONSES = ['- Do you need help moving this forward?',
                  '- Can I help you get traction on this?',

我看到了一些解决方法,指出我只需要我的 cronjob 到 运行 并明确声明了我的 python 路径,但这似乎是一个劣质的解决方法,我已经设置了运行 这对我来说是一个服务器,所以我更愿意通过让事情正常工作来解决它 - 但我不明白的是为什么 cronjob 运行 命令似乎找不到模块,但我可以作为 root 运行 使用 crontab 中指定的相同语法。我已经通过手动 运行ning 在 crontab 中指定的相同命令验证了这一点:

me@local] ~/Documents/auto_updater > crontab -l
*/30 * * * * python /Users/me/Documents/auto_updater/jira_updater.py >> /Users/me/Documents/auto_updater/updated_log.txt

任何人都知道为什么 cronjob 找不到模块,或者手动指定 python 路径真的是唯一的 "fix"?

可能在 jira_updater.py 中显式附加 jira 的库路径。

# added code below before import jira
# append path where jira lib located, for example in /usr/bin/lib
import sys
sys.path.append('/usr/bin/lib')

# if yout don't know where jira located, use code below to get jira path first
# then put the path found into code sys.path.append above
import imp;imp.find_module('jira')