Python脚本直接通过命令行运行,但不通过shell/bash脚本运行



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

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

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

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

我以为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脚本并跳过上传/下载到谷歌驱动器的部分,它会很好地工作。

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

当您通过命令行运行时,它会为json文件和其他文件选择路径。克朗找不到路。在道路上要绝对,它会顺利运行。如果绝对路径不可能,请尝试相对于CRON位置路径的相对路径。

最新更新