我正在尝试使用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.
答案 0 :(得分:0)
我的最佳猜测是pip install gspread
已将gspread
安装到其他python解释器。
请尝试以下操作在您要使用的python解释器中重新安装gspread
。
import pip
pip.main(["install", "gspread"])
答案 1 :(得分:0)
如果您使用的是python3
,则可能需要使用pip3
。最佳做法是在virtualenv中进行此操作:
virtualenv --python=3.6 myvenv
source myvenv
pip install gspread
python -m stuff.py
答案 2 :(得分:0)
如果您使用的是python3,则应使用pip3安装模块/库。并使用“ python3”而不是“ python”解释代码。然后它将适用于导入说明。
答案 3 :(得分:0)
https://pypi.org/project/gspread/#files
从上面的链接下载tar文件:gspread-3.6.0.tar.gz
$ tar -xf gspread-3.6.0.tar.gz
$ cd gspread-3.6.0
$ python3.6 setup.py install
Python 3.6.12 (default, Aug 18 2020, 02:08:22)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gspread
>>>
没有错误...