【问题标题】:ImportError only for crontab job?ImportError 仅适用于 crontab 作业?
【发布时间】:2016-10-18 13:54:18
【问题描述】:

所以我编写了一个漂亮的自动化脚本,它通过 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 来运行我的 pythonpath 明确声明,但这似乎是一个伪劣的变通办法,我已经设置了一个服务器来为我运行它,所以我'我更喜欢通过使事情正常工作来解决它 - 但我无法弄清楚为什么 cronjob run 命令似乎无法找到模块,但我可以以 root 身份使用与 crontab 中指定的相同语法运行它.我已经通过手动运行 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 找不到模块,或者手动指定 pythonpath 真的是唯一的“修复”吗?

【问题讨论】:

    标签: python import cron jira


    【解决方案1】:

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

    # 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')
    

    【讨论】:

    • 效果很好 - 它也不喜欢我的 pythonpath,非常感谢!
    猜你喜欢
    • 2022-06-16
    • 2014-08-10
    • 2020-12-08
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2011-06-20
    • 2023-03-22
    • 2014-07-07
    相关资源
    最近更新 更多