Python crontab?


#1

I’m trying to run a python script, and have this in my crontab:
15,30,45,0 * * * * …/twitterretriever.py
(full path not included)

This is a simple script that, as the name implies, retrieves twitter status messages.

It works fine when I run it myself, but when the cron daemon does it, I get this:

ImportError: No module named twitter

I’ve tried to export PYTHONPATH from within the program, but that makes no difference.

import os
os.environ[‘PYTHONPATH’] = ‘/home/beertje/pymodules’

I’ve tried to require python-twitter, but get told

ImportError: No module named pkg_resources

I tried placing the python-twitter egg in the same folder as the script, still no banana.

I’m pretty much groping in the dark, so if could tell me what I need to do to locate the twitter module, it would be very much appreciated.

Thanks,
beertje


#2

Looks like my PYTHONPATH was wrong… I changed to this:
import os
os.environ[‘PYTHONPATH’] = ‘/home/.niche/beertje/pymodules’

and now it works!

Edit: Looks like I jumped the gun… the thing is NOT fixed yet :frowning:


#3

Thanks for sharing that; I’m sure it will be helpful to others! :slight_smile:

–rlparker


#4

I don’t think changing the PYTHONPATH environment variable within Python will work. 'm guessing that when Python starts up PYTHONPATH is read and those paths are added, but that once that’s done the variable isn’t used. Instead, I would try changing sys.path:

import sys
sys.path.append(’/some/path’)

But it might be easier to PYTHONPATH within the crontab file:

PYTHONPATH=$HOME/some/path

Larry


#5

Fantastic! Thanks, that worked! I didn’t realize I could set the path in the crontab file…

For future reference for those who have a similar issue (and will run into the same things as me :P):

  • Be sure to put the full path into the crontab, as NO environment variables are set by default. That includes $HOME.
  • If you’re using Django, don’t forget to include that in your pythonpath

#6

The man page for the crontab file format says that several variables, including HOME, are set up automatically. I’ve used $HOME in my cron jobs to run custom scripts.

Larry