0
votes

Python file runs properly when executed from terminal.

Gives error "ImportError: No module named bs4" when run with cron.

I'm running python3.6 from /anaconda3/lib/python3.6

python3.6
>>> from crontab import CronTab
>>> cron = CronTab(user=True)
>>> job = cron.new(command='python /Users/X/Y/Z/Hello.py')
>>> job.minute.every(1)
>>> cron.write()

Hello.py calls beautifulsoup from bs4, which is in /anaconda3/lib/python3.6/site-packages/bs4/

Cron details are:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
1
How many versions of python have you got installed? Have you tried putting the full path to the python executable in the command? - Dan-Dev

1 Answers

1
votes

Try to add /anaconda3/lib/python3.6/site-packages/ path to sys.path before importing bs4:

import sys
sys.path.append('/anaconda3/lib/python3.6/site-packages/')

OR

Add PYTHONPATH to crontab environment:

$ sudo crontab -e

PYTHONPATH=$PYTHONPATH:/anaconda3/lib/python3.6/site-packages/