Did you try printing out the sys.path at the top of your script, so
see if the PYTHONPATH is picked up correctly from the cron environment
settings? I'd guess it should, but it may go wrong there (I'm no cron
expert).
Top of script (possibly just below the she-bang):
import sys
print sys.path
Again: did you print the sys.path?
The 'cd' is not going to work, unless your modules are relative to
the /path/to/script/
I may have a solution, but I first want to know if it's a path problem.
You can try using django-cron, which is a django app that can run
tasks on a specified schedule and since it runs from within the django
web framework it does not have the environment and path issues that
setting up an actual cron job does
You're not reading: I wrote:
"
Top of script (possibly just below the she-bang):
import sys
print sys.path
"
and
"
1 * * * * /usr/bin/python /path/to/script/update_feeds.py > /path/to/
script/update_feeds.log 2>&1
"
Then check your update_feeds.log. That gets the output generated from
update_feeds.py when run from cron.
(although I note that the web archive doesn't agree with my sent
emails; hmm...)
I believe that, because your PYTHONPATH will be set correctly from the
command line; but possibly not from a default (basic) shell that cron
uses.
So, again, try putting those two statements at the top of your script,
-before- the 'import feedparser' statement, and let cron do its job.
You may need to redirect the output, eg
1 * * * * /usr/bin/python /path/to/script/update_feeds.py > /path/to/
script/update_feeds.log 2>&1
to get that output (I'm not sure how you got the error from the cron
job; through email?)
Check the log file: before the traceback, you should see the result of
'print sys.path'. Check if that includes the correct directories.
No, the problem is that the Python path is incorrectly set and so you
script cannot import the feedparser! You have been receiving help on how
to print out what your script thinks the Python path is set to so that
you can see that it is mosty likely incorrect and then go about fixing
it. You are still at the "debugging what is wrong" phase, not at the
"fixing it" phase.
Work out what the Python path is set to (by printing it out, for
example) and then you can set an environment variable to set it
correctly.
Malcolm
--
I don't have a solution, but I admire your problem.
http://www.pointy-stick.com/blog/
Your script did run (you thought it completely failed), but -only- up
the 'import feedparser' part. Anything before that ('print sys.path')
actually worked fine.
Printing the sys.path is often quite useful to debug things like this.
> I see the paths specified don't contain the path to the feedparser
> module, which explains the error. My question is how do I add this
> path in the cron, especially since the script runs from the command
> line?
Presumably the environment variables you can set in your cron file are
limited (to, eg HOME and EMAIL), which is why things don't work.
Assuming this cron works the same as other ones I know, you can set
the variables on the command line itself; that may work.
Try this entry in your cron (and remove the top part with the
environment variable settings):
1 * * * * PYTHONPATH=/path/to/python2.3/site-packages:/path/to/django/
app DJANGO_SETTINGS_MODULE=myproject.settings /usr/bin/python /path/
to/script/update_feeds.py > /path/to/script/update_feeds.log 2>1
(in case mailers mess up things: there are no line breaks in that line)
The log file part is still there to check if things go wrong, but if
it works fine, remove that part ('> /path/to/script/update_feeds.log
2>1')
If this seems a bit messy, you can alter the sys.path at the top of
your script, and set the DJANGO_SETTINGS_MODULE from there as well:
import sys
sys.path.extend(['/path/to/python2.3/site-packages', '/path/to/django/
app')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
.
.
.
import feedparser
<etc>
I prefer the first, since then the script itself is more portable (no
hardcoded paths etc).
I also came across this thread: http://groups.google.com/group/django-users/browse_thread/thread/d77412f3bbbd1d1a/64d567aadbf4b3f9?hl=en&lnk=gst&q=cron#64d567aadbf4b3f9
which uses something slightly different (solution at the bottom). But
since this involves settings everything in your .bashrc (including
possibly silly greetings like 'echo "How are you today"), that may be
a bit overkill.
Let me know if it works.
(the difference between what the web archive shows and what my emails
shows, makes it somewhat hard to keep track of which messages you've
already seen, and where we exactly are in the problem solving, but
here goes anyway):
You initially had your PYTHONPATH set differently:
export PYTHONPATH=/path/to/python2.3/site-packages:/path/to/django/app
which is not the same as above. So which one should it be? What does
your regular shell have ('printenv PYTHONPATH')?
In the off-chance you've already got my other email, did you try that
(with the correct PATH settings, whatever those may be)? Ie, setting
the variables on the command line, not in the header of the crontab
file.
OK, write a small script that runs you python job.
Try running the job from the shell:
/usr/bin/python -v /path/to/script/update_feeds.py
This will list all the import statements, so find your imports that
fail in the cron job ...
And then your script would be:
#!/bin/sh
PYTHONPATH= ......
export PYTHONPATH
/usr/bin/python /path/to/script/update_feeds.py
=============================
Andrew Holt
Managing Director
4A Solutions Ltd
M: +44(0) 7841 340608
P: +44(0)1257 268351
Email: andre...@4asolutions.co.uk
Illegitimati non su carborundum !
=============================