【发布时间】:2021-03-20 03:45:53
【问题描述】:
我有一个 python 脚本 main.py 它做了一些事情并每天通过 crontab 运行它,我创建了以下文件(我认为它称为 bash 脚本):
#!/bin/sh
source /Users/PathToProject/venv/bin/activate
python /Users/PathToProject/main.py
一段时间以来,它每天都运行,没有任何问题。
现在我添加了一项功能,该功能可以将包含一些结果的 .CSV 文件随后通过 PyDrive2 在我的 Google 驱动器中保存到 main.py。通过命令行运行这个新脚本时,它每次都能成功运行,没有任何错误。
我以为 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 位于目录中,并且可以通过命令行运行。
【问题讨论】:
-
你有没有尝试让
chmod u+x main.sh然后./main.sh执行它? -
我编辑我的帖子以更具体。 crontab 之前运行完美,因此我认为这应该不是问题,不是吗?
标签: python-3.x cron pydrive