ModuleNotFoundError: No module named 'google' with Python

102 views Asked by At

I have a very simply program that I copied from Google's workspaces, the import statements are as follows

import os.path

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

When I run this code, I keep getting:

from google.auth.transport.requests import Request
ModuleNotFoundError: No module named 'google'

I have been through every question on the topic here, pip installed the world and still no love. I created a requirements.txt file and used pip show to obtained the current versions that ARE installed in my C:\Python310\Lib\site-packages\ - which is in my PATH along with C:\Python310\ and C:\Python310\Scripts.

openai==1.14.0
langchain==0.1.12
streamlit==1.32.2
google==3.0.0
google-auth==2.28.2
requests-oauthlib==1.4.0
google_auth_oauthlib==1.2.0
oauthlib==3.2.2
google-api-python-client==2.122.0

The requirements document, in VSCode, shows no missing references and yet I keep getting the same error. I even went through VSCode Intellisense and manually recreated each Import statement with no issues. So VSCode sees the modules but I still get that same error.

Can somebody possibly help me out?

Thank you for your time.

UPDATE: Complete code sample*

# Imports
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import base64
from email.mime.text import MIMEText


# Authenticate
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 
          'https://www.googleapis.com/auth/gmail.modify']

creds = Credentials.from_authorized_user_file('token.json', SCOPES)

service = build('gmail', 'v1', credentials=creds)
service.users().messages().list(userId='me', maxResults=10).execute()
2

There are 2 answers

1
Ashutosh Malviya On BEST ANSWER

I have two suggestions that you should try.

  1. Verify that the site-packages directory where the required packages are installed is included in your PYTHONPATH environment variable. You can do this by printing sys.path in your Python script to see which directories Python searches for packages.

  2. Double-check that the required packages (google, google-auth, google-auth-oauthlib, google-api-python-client) are installed in the correct site-packages directory. You can use pip list to see the installed packages and their versions.

2
Linda Lawton - DaImTo On

My samples all use

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Think you are missing google-auth-httplib2

This is also shown in the official sample for Gmail install_the_google_client_library