使用 crontab 在 prawcore 上导入错误

Importerror on prawcore using crontab

所以我使用 python 和 praw 编写了我自己的墙纸更换器,以从 reddit 上拉下图像。从命令行调用时效果很好。我的问题是 crontab。我使用 sudo crontab -e 修改了 crontab 并将其放在

* * * * * /usr/bin/python /home/doubt_even/RWPC >> /var/log/wallpaper_output.log 2>&1

这个returns错误

Traceback (most recent call last): 
  File "/home/doubt_even/RWPC", line 9, in <module> 
    import praw
  File "/usr/local/lib/python2.7/dist-packages/praw/__init__.py", line 14, in <module>
    from .reddit import Reddit  # NOQA
  File "/usr/local/lib/python2.7/dist-packages/praw/reddit.py", line 11, in <module>
    from prawcore import (Authorizer, DeviceIDAuthorizer, ReadOnlyAuthorizer,
ImportError: No module named prawcore

我的 shebang 是#!/usr/bin/python。我尝试使用

在脚本中附加到 prawcore 的路径
import sys
sys.path.append('/home/doubt_even/.local/lib/python2.7/site-packages/prawcore/')

我注意到,如果我 运行 在命令行中使用 sudo 前缀,我的脚本无法成功更改墙纸。我尝试将脚本放入 cron.hourly 并将相同的 crontab 条目放入常规 crontab -e。当我将输出文件放入 crontab -e 时,输出文件不起作用,所以我不确定是什么问题。

doubt_even@mint ~ $ sudo /usr/bin/python -c "import sys; print sys.path"
['', '/usr/local/lib/python2.7/dist-packages/pyPdf-1.13-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pygeoip-0.3.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/beautifulsoup4-4.5.3-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/doubt_even/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

doubt_even@mint~ $/usr/bin/python -c "import sys; print sys.path"
['', '/usr/local/lib/python2.7/dist-packages/pyPdf-1.13-py2.7.egg','/usr/local/lib/python2.7/dist-packages/pygeoip-0.3.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/beautifulsoup4-4.5.3-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/doubt_even/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

doubt_even@mint ~ $ /usr/bin/python -c "import prawcore; print prawcore.__file__"
/home/doubt_even/.local/lib/python2.7/site-packages/prawcore/__init__.pyc

我也尝试过明确说明 PYTHONPATH,但我了解到这不是最佳解决方案。

有什么想法吗?

为了让解释器找到模块,目录 包含 模块(即,不是模块本身)应该列在 PYTHONPATH 上。

解决方案 1:

运行 和 root 的 crontab(如 sudo crontab -e),在你的脚本中:

# let the interpreter find prawcore
import sys
sys.path.append('/home/doubt_even/.local/lib/python2.7/site-packages/')

解决方案 2:

为 crontab 环境设置 PYTHONPATH:

$ sudo crontab -e

PYTHONPATH=$PYTHONPATH:/home/doubt_even/.local/lib/python2.7/site-packages/

* * * * * /usr/bin/python /home/doubt_even/RWPC  >> /var/log/wallpaper_output.log 2>&1

I also tried a explicitly stating the PYTHONPATH, but I've read that's not the best solution.

我过去使用过这种方法,从未发现任何不便之处。也许你可以分享。

解决方案 3:

运行 与 doubt_even 的 crontab(如 crontab -e

...the output file does not work when I put it into crontab -e

您可以通过写入普通用户具有写入权限的另一个目录来解决此问题,如

* * * * * /usr/bin/python /home/doubt_even/RWPC >> /home/doubt_even/.wallpaper_output.log 2>&1