-1

Cron calling python script throws error of Module not found for Libraries that are installed and working fine when individually that Python script is executed in virtualenv on ssh

Python script runs in virtualend in ssh , Now trying to set cron for that script and add error log

error log

ModuleNotFoundError: No module named 'google.cloud'

Want to libraries path to be set as per new python3.6 installation instead of python2.7 default path

1 Answers1

0

Cronjobs run in a different environment from your typical bash shell. You need to activate the virtualenv before running the cronjob.

Let's say that you have a python file called '/a/b/main.py' and a virtualenv '/a/b/env'. Here's an example cronjob to run the script every night at 12am (create with crontab -e):

0 0 * * * source /a/b/env/bin/activate && python /a/b/main.py

GOTCHA: cron runs by default in the sh shell, not bash. If you want to use bash you need to explicitly specify this (e.g. /bin/bash ./something.sh) or define the SHELL env var in your crontab (include SHELL=/bin/bash at the top)

Benjamin
  • 546
  • 4
  • 12