首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Google导入Create_Service ModuleNotFoundError:没有名为“Google”的模块

从Google导入Create_Service ModuleNotFoundError:没有名为“Google”的模块
EN

Stack Overflow用户
提问于 2021-09-20 10:12:23
回答 2查看 8K关注 0票数 2

我试图在python中使用Gmail api发送电子邮件,但我无法避免导入Google模块,尽管我使用了"pip安装-升级google python-client“或"pip install google”。

然而,pip冻结显示:

代码语言:javascript
复制
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

我的代码:

代码语言:javascript
复制
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)

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-19 12:34:34

您应该在项目文件Google.py的文件夹中创建,这里是它的代码

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2021-09-20 10:55:38

隐式相对导入不再支持记录在案

不再有任何隐含的进口机器。

因此,如果Google.py与您粘贴的代码位于同一个目录中,则必须引用它的真实位置明确地说

代码语言:javascript
复制
from .Google import Create_Service  # Notice the dot (.)

或者也可以是一条绝对的道路。假设这是Django项目,则如下所示:

代码语言:javascript
复制
from my_proj.Google import Create_Service  # This assumes that your file is in my_proj/my_proj/Google.py
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69252673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档