GSpread ImportError: No module named oauth2client.service_account
Asked Answered
S

4

12

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!

Sadowski answered 16/6, 2016 at 4:39 Comment(7)
It seems that your interpreter cannot find the oauth2client module. Which OS/Python version are you running ? Did pip install --upgrade oauth2client run without errors ?Baudelaire
I wonder if this is related: github.com/burnash/gspread/issues/357I think SignedJwtAssertionCredentials was deprecated in oauth2client and gspread moved to ServiceAccountCredentials as well. As suggested, you could have a mismatch between your version of oauth2client and its supported login mechanism, so update it to newest version for ServiceAccountCredentials or use SignedJwtAssertionCredentials (old method) with your existing version.Timelag
This is causing a lot of confusion actually, the question directly before yours in the gspread tag is also directly related, so viewers of this question might also be interested in another manifestation of errors that arise from this change. I had a typo in my github link too. It's: github.com/burnash/gspread/issues/357Timelag
@JacquesGaudin yeah for some reason it won't let me upgrade it or install it ever I just realized because when I run pip list oauth2client is not on there and when I try to upgrade or install it period I get this [error] (imgur.com/vkPAHva)Sadowski
@Timelag thanks for the comment. You may be right, but I'm just having trouble getting oauth2client even loaded I think I've realizedSadowski
From this discussion (github.com/pypa/pip/issues/3165) you can do pip install --ignore-installed sixBaudelaire
@JacquesGaudin I forgot to respond earlier, but thank you for your help!Sadowski
L
18

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

Got this from the oauth2client library github repo

Langsyne answered 19/5, 2017 at 11:50 Comment(0)
B
3

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.

Baudelaire answered 20/6, 2016 at 11:34 Comment(0)
C
1

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.

Carpenter answered 8/9, 2017 at 0:6 Comment(0)
H
1

You can fix the error by checking if there is more than one folder in your library.

C:\Python27\Lib\site-packages\oauth2client
C:\Program Files (x86)\Google\Google_Appengine\lib\google-api-python-client\oauth2client

you just need to delete one of the folders or change the library path in the compiler.

Here is the link to a video that I made on the topic:

How to fix: "No module named service_account" - Python

Handspring answered 25/10, 2017 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.