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!

Ryan Bonhardt
  • 121
  • 1
  • 1
  • 7
  • 1
    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 ? – Jacques Gaudin Jun 16 '16 at 11:49
  • 1
    I wonder if this is related: https://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. – roganjosh Jun 16 '16 at 14:20
  • This is causing a lot of confusion actually, the question [directly before](http://stackoverflow.com/questions/37710245/gspread-to-access-google-spreadsheet-httpaccesstokenrefresherror-invalid-jwt) 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: https://github.com/burnash/gspread/issues/357 – roganjosh Jun 16 '16 at 14:41
  • @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] (http://imgur.com/vkPAHva) – Ryan Bonhardt Jun 16 '16 at 16:48
  • @roganjosh thanks for the comment. You may be right, but I'm just having trouble getting oauth2client even loaded I think I've realized – Ryan Bonhardt Jun 16 '16 at 16:48
  • 1
    From this discussion (https://github.com/pypa/pip/issues/3165) you can do `pip install --ignore-installed six` – Jacques Gaudin Jun 17 '16 at 09:26
  • @JacquesGaudin I forgot to respond earlier, but thank you for your help! – Ryan Bonhardt Jun 20 '16 at 04:35

4 Answers4

17

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

Got this from the oauth2client library github repo

kevthanewversi
  • 3,686
  • 4
  • 27
  • 27
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.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
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.

Billy
  • 547
  • 3
  • 5
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

virtualdvid
  • 2,323
  • 3
  • 14
  • 32