Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSpread ImportError: No module named oauth2client.service_account

Ok I'm following along with the Become A Technical Marketer course and I'm trying to learn how to manage Google Spreadsheets with GSpread. I've followed along with the documentation at http://gspread.readthedocs.io/en/latest/oauth2.html. I've followed the steps in the second URL above and ran a document with the following code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('DFS Google Sheets Data Imports-7205de852ff7.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("Authority Scraper").sheet1
wks.update_cell(1,2,"Hello World!")

From that I get the error in my terminal: from oauth2client.service_account import ServiceAccountCredentials ImportError: No module named oauth2client.service_account

terminal error printout

Someone please help me. The answers with other No module named oath2client.service_account are not working for me. Thanks!

like image 623
Ryan Bonhardt Avatar asked Jun 16 '16 04:06

Ryan Bonhardt


3 Answers

Running this command worked for me - sudo pip install --upgrade oauth2client

Got this from the oauth2client library github repo

like image 58
kevthanewversi Avatar answered Nov 02 '22 13:11

kevthanewversi


According to this discussion,

This is because OS X El Capitan ships with six 1.4.1 installed already and when it attempts to uninstall it, it doesn't have permission to do so because System Integrity Protection doesn't allow even root to modify those directories.

Amongst the few workarounds mentionned in the answers, it may be worth trying pip install --ignore-installed six to avoid the attempted uninstall of the system's six package.

like image 3
Jacques Gaudin Avatar answered Nov 02 '22 11:11

Jacques Gaudin


So I just encountered this issue as well and it ended up being a path issue for me. Granted, I know this is a little far-fetched (since everyone's dev environment is different), but writing this here in case it helps someone else.

The TLDR make sure something isn't mucking with your $PYTHONPATH.

Recall that when you do an "import" in python, python checks your sys.path for packages. This list has a precedence order (i.e. if a package is found in an earlier path in the list, then that package will be used).

In my case, it looks like my $PYTHONPATH had been modified when doing some appengine stuff a while ago. As it turns out, my appengine had its own oauth2client lib that's pretty old.

As a result, when python attempted from oauth2client.service_account it was grabbing the oauth2client in appengine rather than the oauth2client I was expecting it to grab (a result of the $PYTHONPATH having been modified).

You can verify if this is happening to you as well by printing the sys.path before your import call:

import sys
print sys.path
from oauth2client.service_account import ServiceAccountCredentials

In my case I could clearly see a bunch of appengine paths that were taking precedence. This lead me to check my ~/.bash_profile where wala I found this line:

export PYTHONPATH=$PYTHONPATH::$LOCAL_APPENGINE_HOME/lib/:$LOCAL_APPENGINE_HOME/lib/yaml/:$LOCAL_APPENGINE_HOME:$LOCAL_APPENGINE_HOME/lib/webapp2-2.5.2/`

Commented that out, started a new shell and everything worked dandy.

like image 1
Billy Avatar answered Nov 02 '22 12:11

Billy