francium
francium

Reputation: 2480

Python Youtube API Client Warnings

TLDR, using google-api-python-client, it's giving me a couple of warnings,

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google'

And I want to hide them. How do I hide them?


Details

I'm using google-api-python-client==1.6.2 to perform searching using YouTube data API. I don't need OAuth in this case, so I've not installed anything besides google-api-python-client.

When I run my code, I get a long WARNING with a couple of Tracebacks. My application is still running as I can still use curl to hit the server and I get a result.

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
    from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
    from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
    from . import file_cache
  File "/home/user/.virtualenvs/tmp/lib/python3.6/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
    'file_cache is unavailable when using oauth2client >= 4.0.0')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0

Here's my code,

my_package/main.py

from my_package.server import app

HOST = 'localhost'
PORT = 8100
app.run(HOST, PORT)

my_package/server.py

from flask import Flask, request
from flask_restful import Resource, Api
from logging import info
from my_package.youtube import YouTube


app = Flask(__name__)
api = Api(app)

yt_client = YouTube()


class Search(Resource):
    def get(self, query):
        info("Handling `get` request for the resource 'Search'.")
        return yt_client.search(query)

api.add_resource(Search, '/search/<string:query>')


if __name__ == '__main__':
    info('Application starting.')
    app.run(debug=True)

my_package/youtube.py

from logging import info
import apiclient as google


class YouTube:
    MAX_RESULTS = 25

    def __init__(self):
        info('Creating a YouTube API instance.')
        self.API_KEY = 'MY_API_KEY'
        self.youtube = google.discovery.build('youtube', 'v3',
                                              developerKey=self.API_KEY)

    def search(self, query):
        info(f"Performing a search for '{query}'")
        results = self.youtube.search().list(
            q=query,
            part='snippet',
            maxResults=self.MAX_RESULTS
        ).execute()

        return results.get('items', [])

Here's my requirements.txt

aniso8601==1.2.1
certifi==2017.4.17
chardet==3.0.3
click==6.7
Flask==0.12.2
Flask-RESTful==0.3.6
google-api-python-client==1.6.2
httplib2==0.10.3
idna==2.5
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==1.0
oauth2client==4.1.0
pyasn1==0.2.3
pyasn1-modules==0.0.9
python-dateutil==2.6.0
pytz==2017.2
requests==2.17.3
rsa==3.4.2
six==1.10.0
uritemplate==3.0.0
urllib3==1.21.1
Werkzeug==0.12.2

Upvotes: 1

Views: 809

Answers (1)

clockwatcher
clockwatcher

Reputation: 3373

Set the cache_discovery parameter in your build call to False.

google.discovery.build('youtube','v3',developerKey=self.API_KEY,
                       cache_discovery=False)

That will prevent the import of the cache.discovery module which is what is logging the warning.

Upvotes: 2

Related Questions