Search code examples
pythongoogle-apiyoutube-apiyoutube-data-apigoogle-api-client

ModuleNotFoundError: No module named 'apiclient'


I am trying to run a python code that will help me in connecting & extracting data from YouTube Data API v3. However, the moment i try to run the code, it give me the following error right at the first line:

File "C:/Users/asaxena/Desktop/py4e/Social Media Data Analytics/youtube_search.py", line 3, in <module>
    from apiclient.discovery import build
ModuleNotFoundError: No module named 'apiclient'

I have already installed google-api-python-client in my working directory through the command: pip install --upgrade google-api-python-client But its not helping me in running the code.

from apiclient.discovery import build
import argparse
import csv
import unidecode

def youtube_search(options):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
    search_response = youtube.search().list(q=options.q, part="id,snippet", maxResults=options.max_results).execute()

    videos = []
    channels = []
    playlists = []

    csvFile = open('video_result.csv','w')
    csvWriter = csv.writer(csvFile)
    csvWriter.writerow(["title","videoId","viewCount","likeCount","dislikeCount","commentCount","favoriteCount"])

    for search_result in search_response.get("items", []):
        if search_result["id"]["kind"] == "youtube#video":
            title = search_result["snippet"]["title"]
            title = unidecode.unidecode(title)  # Dongho 08/10/16
            videoId = search_result["id"]["videoId"]
            video_response = youtube.videos().list(id=videoId,part="statistics").execute()
            for video_result in video_response.get("items",[]):
                viewCount = video_result["statistics"]["viewCount"]
                if 'likeCount' not in video_result["statistics"]:
                    likeCount = 0
                else:
                    likeCount = video_result["statistics"]["likeCount"]
                if 'dislikeCount' not in video_result["statistics"]:
                    dislikeCount = 0
                else:
                    dislikeCount = video_result["statistics"]["dislikeCount"]
                if 'commentCount' not in video_result["statistics"]:
                    commentCount = 0
                else:
                    commentCount = video_result["statistics"]["commentCount"]
                if 'favoriteCount' not in video_result["statistics"]:
                    favoriteCount = 0
                else:
                    favoriteCount = video_result["statistics"]["favoriteCount"]

            csvWriter.writerow([title,videoId,viewCount,likeCount,dislikeCount,commentCount,favoriteCount])

    csvFile.close()

At the end, I should be able to establish a successful connection with YouTube Data API v3, and extract data in a csv file.


Solution

  • You're importing a non-existing module. According to the documentation here you should be using:

    from googleapiclient.discovery import ...
    

    instead of:

    from apiclient.discovery import ...