将Pydrive接入Python项目时遇client_secrets.json未找到错误求助
解决PyDrive找不到client_secrets.json的问题
错误信息
Traceback (most recent call last): File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\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 "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 386, in LoadClientConfigFile client_type, client_info = clientsecrets.loadfile(client_config_file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile return _loadfile(filename) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\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 "c:\Users\LENOVO\OneDrive\Desktop\Computational Neuroscience\Data\Data Science.py", line 4, in <module> gauth.LocalWebserverAuth(client_secrets='client_secrets.json') File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 113, in _decorated self.GetFlow() File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 443, in GetFlow self.LoadClientConfig() File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 366, in LoadClientConfig self.LoadClientConfigFile() File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python311\Lib\site-packages\pydrive\auth.py", line 388, in LoadClientConfigFile raise InvalidConfigError('Invalid client secrets file %s' % error) pydrive.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
原代码
from pydrive.auth import GoogleAuth gauth = GoogleAuth() gauth.LocalWebserverAuth(client_secrets='client_secrets.json')
解决方案
使用绝对路径定位文件:
Python默认从当前工作目录查找相对路径文件,而非脚本所在目录。通过以下代码自动获取脚本所在目录的绝对路径,确保文件能被找到:from pydrive.auth import GoogleAuth import os # 获取当前脚本的绝对路径所在目录 script_directory = os.path.dirname(os.path.abspath(__file__)) # 拼接得到client_secrets.json的绝对路径 secrets_file_path = os.path.join(script_directory, 'client_secrets.json') gauth = GoogleAuth() gauth.LocalWebserverAuth(client_secrets=secrets_file_path)检查IDE工作目录设置:
若使用VS Code、PyCharm等IDE,确认工作目录设置为脚本所在的文件夹。例如VS Code可在终端查看当前路径,或在launch.json中配置"cwd": "${fileDirname}";PyCharm可在运行配置中设置工作目录为脚本所在目录。验证文件名称与格式:
确认文件确实命名为client_secrets.json,无隐藏后缀(Windows系统默认可能隐藏扩展名,需开启"显示文件扩展名"选项,避免实际是client_secrets.json.txt这类伪装文件)。
内容的提问来源于stack exchange,提问作者Shi0oo
相关产品推荐
相关产品推荐

