python - 导入错误 : No module named gspread

标签 python python-3.x python-2.7 gspread oauth2client

我正在尝试使用 python 中的 gspread 库。我使用 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.

最佳答案

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

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

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

编辑:下面列出的方法是deprecated并且只适用于 Python 2。

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

关于python - 导入错误 : No module named gspread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51292611/

相关文章:

python - pdf2image 不适用于gunicorn/flask

python - 连接/组合 MX1 numpy 数组与 MXN numpy 数组

python - 在交叉验证期间将参数传递给 scikit 中的 Score_func

python - 编辑两个 Pandas 列之间的距离

Python 返回无效语法错误

python - 在字典中查找具有最多值的键

python - 导入 python 模块 - ImageChops

python - 将带有未定义字符的字节文字写入 CSV 文件(Python 3)

python - 无法从 django 与 MySQL 通信,但可以直接在 python 中

python - 仅迭代序列的一部分后释放生成器资源