【问题标题】:ModuleNotFoundError: No module named 'Google' despite installing with pip [duplicate]ModuleNotFoundError:尽管使用 pip 安装,但没有名为“Google”的模块 [重复]
【发布时间】:2022-01-27 01:52:16
【问题描述】:

我正在尝试将文件上传到谷歌驱动器,但由于某种原因,即使我多次执行pip install Google,“谷歌”库也没有加载。可能是什么问题?

Traceback (most recent call last):
  File "C:\Users\clarot\pythonProject\main.py", line 2, in <module>
    from Google import Create_Service #pip install Google pip install --upgrade google-api-python-client
ModuleNotFoundError: No module named 'Google'
from googleapiclient.http import MediaFileUpload
from Google import Create_Service #ERRORS OUT HERE

from pathlib import Path
downloads_path = str(Path.home() / "Downloads")

CLIENT_SECRET_FILE = "client_secret.json"
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ["https://www.googleapis.com/auth/drive"]

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

folder_id = ""
file_names = ["test.rar"]
mime_types = ["application/vnd.rar"]

for file_name, mime_type in file_names:
    file_metadata = {
        "name": file_name,
        "parents": [folder_id]
    }

    media = MediaFileUpload(downloads_path+'{0}'.format(file_name), mime_type=mime_type) #

    service.files().create(
        body=file_metadata,
        media_body=media,
        fields='id'
    ).execute()

【问题讨论】:

  • 你是否在虚拟环境中安装了模块?
  • 我该怎么做?我在PyCharm下的终端上做了pip install Google,我在cmd上试了一下。
  • pypi 上名为Google 的python 项目(即你在做pip install google 时得到的不提供名为Google 的python 包。你从哪里得到这个代码?我强烈建议编写此代码的人有一个文件google.py,其中包含一些自定义函数

标签: python pip google-api-python-client


【解决方案1】:

你应该

pip uninstall google

因为您安装的内容与 google 无关,仅提供 import googlesearch,但我找不到有意义的文档,因为它是第三方而不是来自 google

然后,您需要在项目目录中创建一个名为google.py 的文件(即在您的main.py 旁边)。在那里插入此代码:

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

the tutorial 上找到我怀疑你一直在关注的

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2020-05-14
    • 2021-01-26
    • 1970-01-01
    • 2019-11-07
    • 2021-06-26
    相关资源
    最近更新 更多