Search code examples
python-3.xcronpydrive

Python script runs directly via command line but does not run via shell/bash script


I had a python script main.py it did something and to run it via crontab on a daily basis I created the following file (I think it's called bash script):

#!/bin/sh
source /Users/PathToProject/venv/bin/activate
python /Users/PathToProject/main.py

For some time now it ran daily without any problems.

Now I added a feature that saves a .CSV file containing some results to my google drive via PyDrive2 afterward in the main.py. When running this new script via command line it runs successfully without any errors - every time.

I assumed that the crontab would run as well, but now I get the Traceback below.

/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access mycreds.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 121, in _loadfile
    with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 431, in LoadClientConfigFile
    client_type, client_info = clientsecrets.loadfile(
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 124, in _loadfile
    raise InvalidClientSecretsError('Error opening file', exc.filename,
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/PathToProject/main.py", line 5, in <module>
    main()
  File "/Users/PathToProject/version2.py", line 20, in main
    PYD.download_file(data_file)
  File "/Users/PathToProject/PyDrive_Modul.py", line 58, in download_file
    file_ID = get_ID_of_title(filename)
  File "/Users/PathToProject/PyDrive_Modul.py", line 47, in get_ID_of_title
    drive = google_drive_auth()
  File "/Users/PathToProject/PyDrive_Modul.py", line 11, in google_drive_auth
    gauth.LocalWebserverAuth()
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 123, in _decorated
    self.GetFlow()
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 507, in GetFlow
    self.LoadClientConfig()
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 411, in LoadClientConfig
    self.LoadClientConfigFile()
  File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 435, in LoadClientConfigFile
    raise InvalidConfigError("Invalid client secrets file %s" % error)
pydrive2.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

If I edit the python script and skip the part of up/downloading to google drive it works fine.

Now I don't know why this error occurs and how I can solve this problem. The error message seems to be misleading because the client_secrets.json is in the directory and it works via the command line.


Solution

  • When you run via command line it picks path for json file and others.Cron could not find path.Be absolute in path, It will run smoothly. If absolute path not possible, try relative path with respect to CRON location path.