EZGmail无法找到credentials.json文件

2024-09-24 04:32:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在关注“用Gmail API发送和接收电子邮件”这一无聊的部分,但我无法设置ezgmail API。如果有什么不同的话,我会用Jupyter笔记本。 我已经下载了gmail API,并且运行了他们的快速启动,没有任何问题,但是当我试图运行书中的代码时,我得到一个错误,系统找不到.json文件。这是我的代码,与书中的代码完全相同(除了文件路径,我已将其更改为“C:\path\to”,仅此而已):

import ezgmail, os
os.chdir(r'C:\path\to\PythonScripts\credentials_json_file') #I have correct file path in my code
ezgmail.init()

我得到了一个错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-13-ed031accfb24> in <module>
      1 import ezgmail, os
----> 2 os.chdir(r'C:\path\to\PythonScripts\credentials_json_file')
      3 ezgmail.init()

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\path\to\\PythonScripts\\credentials_json_file'

我用os.listdir仔细检查了凭证文件是否在我当前的工作目录中,并且该文件显示在输出中。我还尝试将代码中的“credentials\u json\u file”部分更改为“credentials.json\u file”,删除单词file、json等,同时将json文件重命名为credentials-gmail.json(正如一些相关帖子所建议的),但没有任何效果

我还尝试在没有os.chdir行的情况下运行代码,但这只会导致以下异常:

usage: ipykernel_launcher.py [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver]
                             [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                             [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
ipykernel_launcher.py: error: unrecognized arguments: -f C:\path\to\AppData\Roaming\jupyter\runtime\kernel-5b1dd877-fb2d-4b67-8b7e-88a3d365dc4a.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

任何帮助都会很好。对于这样一段简单的介绍性代码,我已经花了太长时间来处理这个问题


Tags: 文件topath代码authapijsonhost