Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named gspread

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.
like image 943
Niels Dingsbums Avatar asked Jul 11 '18 19:07

Niels Dingsbums


2 Answers

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"])
like image 103
The Matt Avatar answered Sep 18 '22 19:09

The Matt


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
like image 40
jhole89 Avatar answered Sep 19 '22 19:09

jhole89