PDA

View Full Version : Cron w/ Python 2.7 Not Detecting Modules



alexleavitt
07-11-2012, 12:34 PM
I'm running a small Python script to dump data into a MySQL database. Recently I installed Python 2.7 and set all my PATHs so that when I SSH into my home directory and run


python /scripts/script.py

it populates the database perfectly.

However, when I set up a cron job with the same script, it throws this error:


Traceback (most recent call last):
File "scripts/database.py", line 6, in <module>
import requests
ImportError: No module named requests

I have no clue what's wrong; I'm pretty new to programming. A bunch of my more-knowledgable programmer acquaintances suggested cron may be under a different user that doen't have the same PATH.

Any suggestions?

alexleavitt
07-11-2012, 12:37 PM
EDIT: I do have requests installed, but it's under Python 2.7's site packages.

cade
07-17-2012, 01:58 AM
Your programmer friends were on the right path, but not quite there. Cron jobs run as your user but don't source the same dotfiles. Depending on where you set up your path that is likely the case. The easiest fix would be to simply specify the full path to your local Python install in the cron. Something like:


/home/<username>/additional_path/python /scripts/script.py