Search code examples
python

ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'


I'm trying to follow along a tutorial on pulling in Google Analytics data using Python.

Having followed the steps throughout the tutorial, when I call the script I get the error message in the title:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredential

Traceback (most recent call last): File "ga.py", line 4, in from oauth2client.service_account import ServiceAccountCredentials ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'

I was reading this SO post after some Googling.

I tried changing my package version:

pip install oauth2client==1.5.2

I tried adding new packages per the answers on that post:

pip install pyopenssl
pip install pycrypto
pip install httplib2
pip install oauth2client
pip install ssl

In each case I encountered the same message after trying to run ga.py:

(zen_ga) Macs-MacBook:zen_ga macuser$ python ga.py Traceback (most recent call last): File "ga.py", line 4, in from oauth2client.service_account import ServiceAccountCredentials ImportError: cannot import name 'ServiceAccountCredentials' from 'oauth2client.service_account'

How can I import ServiceAccountCredentials from oauth2client.service_account?


Solution

  • First, the class you are trying to import is called ServiceAccountCredentials, not ServiceAccountCredential. So try this instead: from oauth2client.service_account import ServiceAccountCredentials.

    oauth2client 1.5.2 does not include ServiceAccountCredentials. Use a more recent version to use it - the latest version is 4.1.3.

    Finally, you should note that oauth2client is deprecated, and it is now recommended to use google-auth instead.