Search code examples
pythoncron

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

According to syslog message script started but there was no expected output, so I created a log file for stdout and stderr messages and there seems to be a problem with BeautifulSoup module import.

import BeautifulSoup
ImportError: No module named bs4

Do you have any idea what might be the root cause and how I can solve it?

EDIT: My problem was related with my crontabs run by different user (root). So as suggested in the answer, when I changed it to own user, problem solved!


Solution

  • the sudo infront of crontab means that root user runs the script. Beautiful soup is not installed for root user so it cannot find the module.

    Instead remove the statement from "sudo crontab -e" and then run and just "crontab -e". Placing the script in there will run it from your user account and should have access to the beautiful soup module