Skip to content Skip to sidebar Skip to footer

Python Sudo Cron Job Not Working Due To Import Module Error

I have a web scraper script which runs without any problem when I start via command line. I have created a crontab job to run this script periodically with: sudo crontab -e Accord

Solution 1:

for every user which you use for cron jobs, you should do pip install your-library. Because cron uses the environment whose user run the job.

Solution 2:

If it's necessary to run a Python script as sudo in crontab, you should utilize the PYTHONPATH variable (as mentioned in a comment by Sraw) to augment the default search path for module files.

First, determine the location of the module by, e.g. running your Python (where the module is imported successfully) and checking sys.path:

import sys
print(sys.path)

One of the paths listed will be the location (call it location/of/module). Find it and change your entry in sudo crontab -e to

PYTHONPATH=/location/of/module python your_script.py

Post a Comment for "Python Sudo Cron Job Not Working Due To Import Module Error"