Google-api-python-client: googleapiclient has no attribute discovery

Created on 19 Dec 2016  路  11Comments  路  Source: googleapis/google-api-python-client

It seems this is a common problem, but none of the suggestions I've read so far resolve this for me.

I've ran pip install google-api-python-client in virtualenvs with python 2.7.11, 2.7.12, and 3.5.2

In python consoles in every version, I can't get anything to find the discovery module.

Via apiclient:

Python 2.7.11 (default, Dec  5 2015, 14:44:47)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import apiclient
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chrismay/.virtualenvs/google-calendar-2.7.11/lib/python2.7/site-packages/apiclient/__init__.py", line 19, in <module>
    from googleapiclient import discovery
  File "/Users/chrismay/.virtualenvs/google-calendar-2.7.11/lib/python2.7/site-packages/googleapiclient/discovery.py", line 39, in <module>
    from email.generator import BytesGenerator
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/generator.py", line 15, in <module>
    from email.header import Header
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/header.py", line 16, in <module>
    import email.quoprimime
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/quoprimime.py", line 49, in <module>
    from email.utils import fix_eols
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/utils.py", line 32, in <module>
    from email._parseaddr import quote
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/_parseaddr.py", line 16, in <module>
    import time, calendar
  File "calendar.py", line 11, in <module>
    service = googleapiclient.discovery.build('calendar', 'v3', developerKey=api_key)
AttributeError: 'module' object has no attribute 'discovery'

Via googleapiclient

Python 2.7.11 (default, Dec  5 2015, 14:44:47)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import googleapiclient
>>> service = googleapiclient.discovery.build('calendar', 'v3', developerKey=api_key)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'discovery'

Running a script in PyCharm's debug mode, the googleapiclient module has only these attributes:
NullHandler, channel, errors, and logging

I've even tried cloning this repo, as an alternative to pip installing, but did not get that to work either.

Is there somethign I'm missing?

triage me

Most helpful comment

@Chris-May it seems you have a file named calendar.py that's aliasing the standard-library calendar module which is causing a circular import.

I'd recommend renaming calendar.py to something else or maybe seein gif from __future__ import absolute_import helps.

All 11 comments

You should probably do:

import googleapiclient.discovery

service = googleapiclient.discovery.build('calendar', 'v3', developerKey=api_key)

Pretty much the same story.

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/chrismay/PycharmProjects/google-calendar/calendar.py", line 3, in <module>
    import googleapiclient.discovery
  File "build/bdist.macosx-10.10-intel/egg/googleapiclient/discovery.py", line 41, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/generator.py", line 15, in <module>
    from email.header import Header
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/header.py", line 16, in <module>
    import email.quoprimime
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/quoprimime.py", line 49, in <module>
    from email.utils import fix_eols
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/utils.py", line 32, in <module>
    from email._parseaddr import quote
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/email/_parseaddr.py", line 16, in <module>
    import time, calendar
  File "/Users/chrismay/PycharmProjects/google-calendar/calendar.py", line 5, in <module>
    import apiclient
  File "build/bdist.macosx-10.10-intel/egg/apiclient/__init__.py", line 19, in <module>
ImportError: cannot import name discovery

@Chris-May it seems you have a file named calendar.py that's aliasing the standard-library calendar module which is causing a circular import.

I'd recommend renaming calendar.py to something else or maybe seein gif from __future__ import absolute_import helps.

Here's another wrinkle: I downloading the source code from https://pypi.python.org/pypi/google-api-python-client/#downloads into a new 2.7 virtualenv and ran python setup.py install.

When it finished, I opened a REPL session, and was able to import build from apiclient.discovery. Convinced this solved the problem, I edited my script and ran it from the command line. Unfortunately, the error persisted. This is the tail of the traceback:

File "/Users/chrismay/PycharmProjects/google-calendar/calendar.py", line 2, in <module>
    from apiclient.discovery import build
ImportError: No module named discovery

@jonparrott Thanks! I didn't get notified about your comment. That's exactly the problem. Thanks!!!

@Chris-May glad we could get it figured out. :)

@Chris-May can you share the resolution?I am stuck at the same point.

You should probably do:

import googleapiclient.discovery

service = googleapiclient.discovery.build('calendar', 'v3', developerKey=api_key)

@theacodes This resolved the problem for me, but, why does importing googleapiclient only doesn't work?

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 578, in del
self._fd.close()
AttributeError: 'MediaFileUpload' object has no attribute '_fd'

I got the same error while executing MediaFileUpload() but It's fine to work in the windows machine. Anyone here can please help me. Thanks

Hi I have same problem but this solutions doesn't work
can somebody help me abiut it?

For what it's worth, the problem I had was that I had a python script named calendar.py, which is bad, because there's a standard library module with the name calendar.py, and my code was taking precedence over it.

Google's code was trying to use the standard library, and not finding the functionality it needed in my file.

Was this page helpful?
0 / 5 - 0 ratings