导入错误:没有名为 gspread 的模块

新手上路,请多包涵

我正在尝试使用 gspread python 库。我用 pip install gspread 安装了lib—但是当我运行代码时:

 import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://sreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('FILE_NAME.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('Changelog').sheet1
print(wks.get_all_records())

它给了我一个错误:

 File "stuff.py", line 1, in <module>
    import gspread
ImportError: No module named gspread

当我在 python3 中运行它时,它没有给我任何导入错误。但是那些:

 File "stuff.py", line 8, in <module>
    gc = gspread.authorize(credentials)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/gspread/__init__.py", line 38, in authorize
    client.login()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/gspread/client.py", line 51, in login
    self.auth.refresh(http)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/client.py", line 545, in refresh
    self._refresh(http)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/client.py", line 749, in _refresh
    self._do_refresh_request(http)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/oauth2client/client.py", line 819, in _do_refresh_request
    raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: invalid_scope: https://sreadsheets.google.com/feeds is not a valid audience string.

原文由 Niels Dingsbums 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

有可能 pip install gspread 安装了 gspread 到不同的 python 解释器。

尝试以下操作以在您要使用的 python 解释器中重新安装 gspread

 import sys, subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gspread'])


编辑:下面列出的方法已被 弃用,仅适用于 Python 2。

 import pip
pip.main(["install", "gspread"])

原文由 The Matt 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题