Search code examples
pythonlinuxcronlinux-mint

Importerror on prawcore using crontab


So I've written my own wallpaper changer using python and praw to pull images down from reddit. It works great when called from the command line. My issue is with crontab. I modified the crontab using sudo crontab -e and placed this inside

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

This returns the error

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

My shebang is #!/usr/bin/python. I've tried appending the path to prawcore inside the script using

import sys
sys.path.append('/home/doubt_even/.local/lib/python2.7/site-packages/prawcore/')

I've noticed that my script does not successfully change the wallpaper if I run it using the sudo prefix from the command line. I tried putting the script in the cron.hourly and the same crontab entry into the regular crontab -e. The output file does not work when I put it into crontab -e so I'm not sure what the problem is.

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

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

Any ideas?


Solution

  • In order for the interpreter to find the module, the directory containing the module (i.e., not the module itself) should be listed on the PYTHONPATH.

    Solution 1:

    Run with root's crontab (as in sudo crontab -e), and inside your script:

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

    Solution 2:

    Set PYTHONPATH for crontab environment:

    $ 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.

    I've used this approach in the past, never saw any inconvinients. May be you can share.

    Solution 3:

    Run with doubt_even's crontab (as in crontab -e)

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

    you could solve this issue by writng to another directory where your regular user has write permissions, as in

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