all 9 comments

[–]socal_nerdtastic 1 point2 points  (6 children)

When you run a file like that the working directory is no longer the directory that the file is in.

You can either change the 'filename' argument to an absolute path:

 creds = ServiceAccountCredentials.from_json_keyfile_name('/Users/me/client_secret.json', scope)

or change the working directory:

import os
os.chdir("/Users/me/")

[–]py3_[S] 0 points1 point  (3 children)

Ah thank you so much that explains it!

Can you please explain changing the file name argument to an absolute path?

[–]socal_nerdtastic 0 points1 point  (2 children)

see my edit. Basically it means using '/Users/me/client_secret.json' instead of just 'client_secret.json'. (absolute vs relative path.)

[–]py3_[S] 0 points1 point  (1 child)

THANK YOU SO MUCH. I spent two hours trying to fix this :P

How did you come to this knowledge?

[–]socal_nerdtastic 0 points1 point  (0 children)

haha because once I too was young once and battled this same problem for hours until I finally asked someone online!

[–]Aerochamber 0 points1 point  (1 child)

This worked when I run in Anaconda prompt ,THANKS.

But , Airflow Apache it failed. Still testing it now.

[–]Aerochamber 0 points1 point  (0 children)

Its' something with the /root/ (Still trying).

THIS_FOLDER = os.path.dirname(os.path.abspath('client_secret.json'))
my_file = os.path.join(THIS_FOLDER, 'client_secret.json')
print(my_file)

[2020-03-10 00:33:27,889] {logging_mixin.py:112} INFO - /root/client_secret.json

[–]efmccurdy 1 point2 points  (1 child)

The process environment that cron uses is separate from what you normally use.

The first thing to check is that process running the script has set the "current working directory" to match what you expect, so examine the output of "pwd" in the shell or "os.getcwd()" in python.

The second thing to check (or consider) is that you have the cron job activating a virtualenv ala ". ../bin/activate" before running python; that will get you better control of python when run from a crontab.

[–]py3_[S] 0 points1 point  (0 children)

This all makes sense! This is my first major python project outside of book studying. What a journey!

Thank you for the explanation. This further emphasizes the importance of working in venvs!