ImportError: No module named gspread
Asked Answered
A

4

10

I'm trying to work with the gspread library in python. i installed the lib with pip install gspread but when I run the code:

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

it gives me an error:

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

When I run it in python3 it gives me no import error. But those:

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.
Abash answered 11/7, 2018 at 19:7 Comment(5)
after you installed it with pip, did you start a new python session? or is it a terminal window from before you installed gpsread? if so, you'll need a new oneChangchun
which python version are you using? what OS?Decency
Do you have both versions of Python installed on the same machine? If so then there is probably a pathing issue. Ideally you only want 1 install on your box.Corroboration
@TheOne I use macOS, python 3.6.5Abash
@NielsDingsbums did you try installing using pip3 instead of pip?Decency
R
8

It is possible that pip install gspread installed gspread to a different python interpreter.

Try the following to reinstall gspread in the python interpreter you want to use.

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

EDIT: The method listed below has been deprecated and only works on Python 2.

import pip
pip.main(["install", "gspread"])
Robbert answered 11/7, 2018 at 20:33 Comment(3)
Thanks for the helpful comment! However, this is my code: import sys, subprocess subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gspread']) import gspread And this is the ouput: Requirement already satisfied: gspread in ... ModuleNotFoundError: No module named 'gspread' Isn't this super weird?Glandular
That is weird. That message would seem to indicate pip knows about a package that is installed named "gspread", but no module named "gspread". The same way the Python package "Pillow" has as a module name of "PIL". You may want to try adding the --force-reinstall flag to force pip to install gspread regardlessly.Robbert
Thank you for the answer! What fixed it for me was switching to a new virtual environment, this error stopped showing up. However, I am still very curious about the error that I got, that confirmed gspread was downloaded but it couldn't find it ahahGlandular
E
4

if you're using python3 you might need to use pip3. Best practice would be to do it in a virtualenv:

virtualenv --python=3.6 myvenv
source myvenv
pip install gspread
python -m stuff.py
Entree answered 11/7, 2018 at 20:42 Comment(0)
R
1

If you're using python3, you should install the modules/libraries with pip3. And interpret the codes with 'python3' and not with 'python'. Then it will work for import instructions.

Riband answered 26/10, 2020 at 11:19 Comment(1)
If you attempt to answer and twice the correct answer has been giving... why adding this blabla over python3 without giving straightforward solution? Keep in mind: coders are lazy but able to one-click the down-vote button in merely 500milliseconds. Please update to increase quality of answer. Welcome to SO ;-) End of Review (EoR).Typewriter
T
0

https://pypi.org/project/gspread/#files

Download the tar file : gspread-3.6.0.tar.gz from the above link

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

No error ...

Torosian answered 5/1, 2021 at 17:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.