0

My aim is to run a script daily that updates a google sheet with data from Google Analytics.

When I run the code on my VM instance it returns an error:

ModuleNotFoundError: No module named 'googleapiclient'

The module is installed and if I run:

$ source [project-name]/bin/activate

on the VM then call my script it works and the update is done.

I am trying to set the script to run daily, I tried to set it up using crontab: (for testing i used every 5 mins)

I tried: */5 * * * * python3 myscript.py and

*/5 * * * * $ python3 myscript.py

and */5 * * * * source [project-name]/bin/activate

*/5 * * * * python3 myscript.py

This is the first time I am trying to set up a crontab job, so any debugging suggestions are appreciated as well.

David C
  • 365
  • 2
  • 8
  • Seems as if you have the dependency installed only in a virtualenv, and therefore you get the error when trying to run the script without first activating the virtualenv. The following [post](https://stackoverflow.com/questions/3287038/cron-and-virtualenv) within the community should guide you in the right path to get your script running I'd you are interesting in maintaining the virtualenv. Another solution could be to simply install the dependencies used by your script globally (which will simply translate in running the 'pip install [DEPENDENCY]' without first activating your virtualenv. – Daniel Ocando Mar 31 '21 at 17:07
  • Thanks, I was thinking that was the problem, that I installed it in the wrong way. I tried to install it globally and it said that it was already installed. Do you think if I start a new VM instance and and install it again with pip it will work? Do I use $ pip3 install [depemdency] or do I use $ sudo apt-get ..? And thanks for the help – kitti.megyeri Apr 01 '21 at 07:07
  • I think the better thing will be to post your script with the exact dependencies you are using (you can use a pip freeze and save them on a requirements.txt file) in order to attempt a reproduction. Sharing more details about the image used on your VM will be also helpful. Taking that into consideration, you can always try to run them una fresh VM. – Daniel Ocando Apr 01 '21 at 09:52

1 Answers1

0

So, I finally solved this.

you have to use pip install when installing the libraries and once you install google-api-python-client, you don't have to do the activation to be able to call the library

use: $ pip3 install google-api-python-client

then you can schedule crontab jobs such as: /*5 * * * * python3 myscript.py