【问题标题】:Python script runs directly via command line but does not run via shell/bash scriptPython 脚本直接通过命令行运行,但不通过 shell/bash 脚本运行
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 你的意思是我需要在执行之前添加cd PathToProject
  • 修改/Users/PathToProject/main.py,使用绝对路径或变量作为路径,阅读与python中路径制作相关的文档。使用python函数生成路径
猜你喜欢
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多