我正在尝试使用python中的gspread库。我用pip install gspread安装了库,但是当我运行代码时:
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.发布于 2018-07-12 04:33:06
可能是pip install gspread将gspread安装到了不同的python解释器。
尝试执行以下操作,在要使用的python解释器中重新安装gspread。
import sys, subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gspread'])编辑:下面列出的方法是deprecated,并且只适用于Python2。
import pip
pip.main(["install", "gspread"])发布于 2018-07-12 04:42:32
如果你使用的是python3,你可能需要使用pip3。最佳实践是在虚拟环境中执行此操作:
virtualenv --python=3.6 myvenv
source myvenv
pip install gspread
python -m stuff.py发布于 2020-10-26 19:19:19
如果你使用的是python3,你应该用pip3安装模块/库。并使用“python3”而不是“python”来解释代码。然后它将对导入指令起作用。
https://stackoverflow.com/questions/51292611
复制相似问题