导入错误:找不到名为gspread的模块。

10

我正在尝试使用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

当我在Python 3中运行它时,没有出现导入错误。但是有如下提示:
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 后,您是否启动了一个新的 Python 会话?还是之前安装 gpsread 前打开的终端窗口?如果是后者,您需要打开一个新的窗口。 - iammax
你使用的是哪个Python版本?是什么操作系统? - SamAtWork
1
你的电脑上安装了两个版本的Python吗?如果是这样,那么可能存在路径问题。理想情况下,你只需要在电脑上安装一个版本即可。 - sniperd
@TheOne 我使用 macOS,Python 3.6.5。 - Niels Dingsbums
@NielsDingsbums 你试过使用pip3而不是pip安装吗? - SamAtWork
4个回答

8

有可能 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"])

谢谢你的有用评论!不过,这是我的代码:import sys, subprocess subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gspread']) import gspread而这是输出结果: Requirement already satisfied: gspread in ... ModuleNotFoundError: No module named 'gspread'这不是非常奇怪吗? - undefined
这很奇怪。这个消息似乎表明pip知道有一个名为"gspread"的已安装的包,但没有名为"gspread"的模块。就像Python包"Pillow"的模块名为"PIL"一样。你可以尝试添加--force-reinstall标志来强制pip无论如何都安装gspread。 - undefined
1
非常感谢您的回答!对我来说,解决问题的方法是切换到一个新的虚拟环境,这样错误就不再出现了。不过,我对我遇到的错误仍然非常好奇,它确认了gspread已经下载,但却找不到它,哈哈。 - undefined

3

如果您正在使用 Python3,可能需要使用 pip3。最好的做法是在 virtualenv 中完成:

virtualenv --python=3.6 myvenv
source myvenv
pip install gspread
python -m stuff.py

1
如果您正在使用Python3,应使用pip3安装模块/库。并用'python3'而不是'python'解释代码。然后导入指令就会起作用。

如果你试图回答问题,但是两次正确的答案已经给出了...为什么要在Python3上添加这个废话而不提供直接的解决方案呢?请记住:程序员很懒,但能够在短短500毫秒内点击踩按钮。请更新以提高答案质量。欢迎来到SO ;-)评论结束(EoR)。 - ZF007

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
>>> 

没有错误...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接