from Google import Create_Service ModuleNotFoundError: No module named 'Google'

Question:

I’m trying to use Gmail api in python to send email but I cant get past importing the Google module despite using "pip install –upgrade google-api-python-client" or "pip install google".

However pip freeze shows:

asgiref==3.3.4
beautifulsoup4==4.10.0
cachetools==4.2.2
certifi==2021.5.30
cffi==1.14.6
charset-normalizer==2.0.6
dj-database-url==0.5.0
Django==3.2.3
django-ckeditor==6.1.0
django-ckeditor-5==0.0.14
django-heroku==0.3.1
django-js-asset==1.2.2
django-phone-field==1.8.1
django-tawkto==0.3
**google==3.0.0**
google-api-core==2.0.1
google-api-python-client==2.21.0
google-auth==2.1.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.4.6
google-cloud==0.34.0
google-cloud-bigquery==2.26.0
google-cloud-core==2.0.0
google-cloud-storage==1.42.2
google-cloud-vision==2.4.2
google-crc32c==1.1.2
google-resumable-media==2.0.2
googleapis-common-protos==1.53.0
grpcio==1.40.0
gunicorn==20.1.0
httplib2==0.19.1
idna==3.2
oauthlib==3.1.1
packaging==21.0
Pillow==8.2.0
proto-plus==1.19.0
protobuf==3.18.0
psycopg2==2.8.6
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
pyparsing==2.4.7
python-decouple==3.4
pytz==2021.1
requests==2.26.0
requests-oauthlib==1.3.0
rsa==4.7.2
six==1.16.0
soupsieve==2.2.1
sqlparse==0.4.1
uritemplate==3.0.1
urllib3==1.26.6
whitenoise==5.2.0

my code:

from Google import Create_Service
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'gmail'
API_VERSION = 'v1'
SCOPES = ['https://mail.google.com/']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

Any help would be greatly appreciated

Asked By: Edith

||

Answers:

Implicit relative imports are not anymore supported as documented:

There is no longer any implicit import machinery

So if Google.py is in the same directory as the code you pasted, you have to reference it’s realtive location explicitly.

from .Google import Create_Service  # Notice the dot (.)

Or it can also be an absolute path. Assuming this is a Django project, then it would be something like:

from my_proj.Google import Create_Service  # This assumes that your file is in my_proj/my_proj/Google.py

You should create in folder of project file Google.py, here code of it

import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request


def Create_Service(client_secret_file, api_name, api_version, *scopes):
print(client_secret_file, api_name, api_version, scopes, sep='-')
CLIENT_SECRET_FILE = client_secret_file
API_SERVICE_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
print(SCOPES)

cred = None

pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
# print(pickle_file)

if os.path.exists(pickle_file):
    with open(pickle_file, 'rb') as token:
        cred = pickle.load(token)

if not cred or not cred.valid:
    if cred and cred.expired and cred.refresh_token:
        cred.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
        cred = flow.run_local_server()

    with open(pickle_file, 'wb') as token:
        pickle.dump(cred, token)

try:
    service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
    print(API_SERVICE_NAME, 'service created successfully')
    return service
except Exception as e:
    print('Unable to connect.')
    print(e)
    return None

def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
return dt
Answered By: Replicate
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.