I am using crontab to run a python script that requires the module MySQLdb. When I run this script from the command line everything works fine. However, trying to run it using crontab elicits this error.
Traceback (most recent call last):
File "clickout.py", line 3, in <module>
import MySQLdb
ImportError: No module named MySQLdb
I did a google search and added this to the top of my script #!/usr/bin/python
. However, this didn't do anything and I am still getting the same error. What am I doing wrong?
Define PYTHONPATH at the top of your crontab. Defining all these environment variables (below) can help you avoid some common cron problems relating to the lack of environment variables:
USER=...
HOME=/home/...
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$HOME/bin
PYTHONPATH=...
DISPLAY=:0.0
MAILTO=...
LANG=en_US.UTF-8
To find out the path to MySQLdb, open a python shell and type:
>>> import MySQLdb
>>> MySQLdb.__file__
'/usr/lib/pymodules/python2.7/MySQLdb/__init__.pyc'
Your path my differ. In the example above, the appropriate dir to add to PYTHONPATH
would be /usr/lib/pymodules/python2.7
(though you should not have to add this particular path since your python executable should have this path in its sys.path automatically).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With