2

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()
3
  • what api are you trying to connect to exactly? Copying someone else's code without understanding its not a really good way to start. Can I see the full example you are using? Commented Mar 17, 2024 at 15:31
  • I understand the code - it is the module that is the issue. I have tried a TON of examples - I just added one above. Commented Mar 17, 2024 at 15:48
  • Yes but if i know the api i can point you directly at the quickstart for that api if i dont personally have on. Note thats not enough auth code to work. You may want to check the official sample Commented Mar 17, 2024 at 17:21

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I had Python310 on my main C Drive for the VSCode install and I never realized it was there until I saw your note. As soon as I removed it, the Version was correct and the code ran!
0

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

2 Comments

I ended up downloading PyCharm, opened up the project and it installed google-auth-httplib2 just like you said. Thank you!
I use PyCharm to never managed to get VsCode to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.