Python 脚本 运行 直接通过命令行而不是 运行 通过 shell/bash 脚本

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

我有一个 python 脚本 main.py 它做了一些事情并且每天通过 crontab 运行 它我创建了以下文件(我认为它叫做 bash 脚本):

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

一段时间以来,它每天 运行 都没有任何问题。

现在我添加了一项功能,可以在 main.py 中通过 PyDrive2 将包含一些结果的 .CSV 文件保存到我的 google 驱动器。当通过命令行 运行 连接这个新脚本时,它 运行 每次都成功且没有任何错误。

我假设 crontab 也会 运行,但现在我得到下面的 Traceback。

/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)

如果我编辑 python 脚本并跳过 up/downloading 到 google 驱动器的部分,它工作正常。

现在我不知道为什么会出现这个错误,也不知道如何解决这个问题。该错误消息似乎具有误导性,因为 client_secrets.json 在目录中并且它通过命令行工作。

当您 运行 通过命令行选择 json 文件的路径并且 others.Cron 在路径中找不到 path.Be 绝对路径时,它将 运行顺利。如果绝对路径不可能,请尝试相对于 CRON 位置路径的相对路径。