Python crontab?

I’m trying to run a python script, and have this in my crontab:
15,30,45,0 * * * * …/
(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.


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:

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


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

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



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

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.