Search code examples
pythonpython-3.xgoogle-oauthgoogle-authentication

Cannot import `google` in Python code


I'm trying to follow the code from here: https://developers.google.com/api-client-library/python/auth/web-app but I can't figure out what library I need to install. What am I doing wrong?

$ virtualenv -p python3 test
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/chrisb/test/bin/python3
Also creating executable in /Users/chrisb/test/bin/python
Installing setuptools, pip, wheel...done.

$ source test/bin/activate
(test) $ pip install google-api-python-client
Collecting google-api-python-client
  Downloading https://files.pythonhosted.org/packages/0e/7b/0adbc5ee668e9006422c38ee6b7b92c018b03839fc7a11b18cc51054e88b/google_api_python_client-1.6.6-py2.py3-none-any.whl (53kB)
    100% |████████████████████████████████| 61kB 4.4MB/s
Collecting oauth2client<5.0.0dev,>=1.5.0 (from google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/82/d8/3eab58811282ac7271a081ba5c0d4b875ce786ca68ce43e2a62ade32e9a8/oauth2client-4.1.2-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 6.0MB/s
Collecting uritemplate<4dev,>=3.0.0 (from google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/e5/7d/9d5a640c4f8bf2c8b1afc015e9a9d8de32e13c9016dcc4b0ec03481fb396/uritemplate-3.0.0-py2.py3-none-any.whl
Collecting httplib2<1dev,>=0.9.2 (from google-api-python-client)
Collecting six<2dev,>=1.6.1 (from google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting pyasn1>=0.1.7 (from oauth2client<5.0.0dev,>=1.5.0->google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/ba/fe/02e3e2ee243966b143657fb8bd6bc97595841163b6d8c26820944acaec4d/pyasn1-0.4.2-py2.py3-none-any.whl (71kB)
    100% |████████████████████████████████| 71kB 7.9MB/s
Collecting rsa>=3.1.4 (from oauth2client<5.0.0dev,>=1.5.0->google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl (46kB)
    100% |████████████████████████████████| 51kB 10.4MB/s
Collecting pyasn1-modules>=0.0.5 (from oauth2client<5.0.0dev,>=1.5.0->google-api-python-client)
  Downloading https://files.pythonhosted.org/packages/e9/51/bcd96bf6231d4b2cc5e023c511bee86637ba375c44a6f9d1b4b7ad1ce4b9/pyasn1_modules-0.2.1-py2.py3-none-any.whl (60kB)
    100% |████████████████████████████████| 61kB 10.2MB/s
Installing collected packages: pyasn1, httplib2, rsa, six, pyasn1-modules, oauth2client, uritemplate, google-api-python-client
Successfully installed google-api-python-client-1.6.6 httplib2-0.11.3 oauth2client-4.1.2 pyasn1-0.4.2 pyasn1-modules-0.2.1 rsa-3.4.2 six-1.11.0 uritemplate-3.0.0

(test) $ python
Python 3.6.4 (default, Dec 25 2017, 14:57:56)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import google.oauth2.credentials
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'google'

Solution

  • Either you're installing the wrong thing or importing the wrong thing. Below the

    pip install --upgrade google-api-python-client
    

    command in the link you posted is

    pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
    

    which is what you want to download to import the google.oauth2.credentials