简体   繁体   中英

Python: Google Drive API v3 Can't Work After Converted to .exe Using Pyinstaller

I am trying to build Google Drive API v3 using Python. It worked fine if I execute the 'drive.py' file, but when I convert it into 'drive.exe' using pyinstaller (I tried both pyinstaller -F drive.py and pyinstaller -D drive.py ), it failed to execute with the error message below:

Traceback (most recent call last):
  File "drive.py", line 31, in <module>
    main()
  File "drive.py", line 26, in main
    service = build('drive', 'v3', credentials=creds)
  File "googleapiclient\_helpers.py", line 134, in positional_wrapper
  File "googleapiclient\discovery.py", line 282, in build
  File "googleapiclient\discovery.py", line 387, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: drive  version: v3
[9972] Failed to execute script drive

And this is my code:

import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

def main():
    creds = None

    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)

    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)

        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)
    print('service get')

if __name__ == '__main__':
    main()

Is there any way that I can fix this problem?
Thanks for your help.

I am new to this but I searched online regarding this and found that adding static_discovery=False in your build function solves this.

Your build function will sort of look like this build('drive', 'v3', credentials=creds, static_discovery=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM