首页 > 解决方案 > 如何解决 pyinstaller exe 中的“pkg_resources.DistributionNotFound”错误

问题描述

我正在尝试制作一个使用 Google Drive API 的应用程序。我正在使用 PyInstaller 将我的 python 文件转换为可执行文件。我运行了从最基本的 python 脚本生成的 .exe 文件,我得到了一个错误,上面写着

pkg_resources.DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application

我的代码甚至没有使用“google-api-python-client”,但我将它安装在我的 anaconda 环境中,但我仍然面临同样的问题。

我正在添加代码以供参考:-

import pickle
import os
from googleapiclient.discovery import build
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(port=0)

        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
    
s=Create_Service('client_secret_kc.json','drive','v3',['https://www.googleapis.com/auth/drive'])

我用来转换成.exe的pyinstaller代码如下

pyinstaller -c -F --add-data "client_secret_kc.json;." Google.py

NB- Google.py 是上面的 python 脚本 gien 的名称。

感谢您的帮助。

标签: pythongoogle-drive-apigoogle-oauthpyinstaller

解决方案


推荐阅读