Google Drive API not working after using pyinstaller

Viewed 31

I wrote some code that downloads a zip file from a google drive and then extracts the contents. It works fine when I run it normally from vs code, but when I build it to an application with pyinstaller, I get an error saying

Unable to connect: name: drive  version: v3

EDIT: I ran again with debug=all included in the pyinstaller command, and this is the error output:

    Traceback (most recent call last):
  File "main.py", line 20, in <module>
    from Google import Create_Service
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Alfie Anil\Desktop\St Edward the Confessor Projects\Administration Software\Updating Project\Executable Tests\Path 2\Project - 2 - Copy\Google.py", line 3, in <module>
    from google_auth_oauthlib.flow import Flow, InstalledAppFlow
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\google_auth_oauthlib\__init__.py", line 21, in <module>
    from .interactive import get_user_credentials
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\google_auth_oauthlib\interactive.py", line 27, in <module>
    import google_auth_oauthlib.flow
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\google_auth_oauthlib\flow.py", line 66, in <module>
    import google.auth.transport.requests
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\google\auth\transport\requests.py", line 26, in <module>
    import requests
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\requests\__init__.py", line 118, in <module>
    from . import utils
  File "<frozen importlib._bootstrap>", line 1042, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\requests\utils.py", line 40, in <module>
    DEFAULT_CA_BUNDLE_PATH = certs.where()
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\site-packages\certifi\core.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\importlib\resources.py", line 201, in path
    with open_binary(package, resource) as fp:
  File "c:\users\alfie anil\appdata\local\programs\python\python38\lib\importlib\resources.py", line 91, in open_binary
    return reader.open_resource(resource)
  File "<frozen importlib._bootstrap_external>", line 988, in open_resource
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ALFIEA~1\\AppData\\Local\\Temp\\_MEI48922\\certifi\\cacert.pem'

I have a cacert.pem file, but it is not in the location specified. I don't want to access it from there first of all, and even if I wanted to it won't let me paste it in there.

I'm not sure what the problem is so help is appreciated.

Pyinstaller command I use:

pyinstaller --onefile -w --hidden-import=babel.numbers main.py

Main Code

temp_dir = pathlib.Path().resolve()
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(temp_dir, 'cacert.pem')

latest_version = new_versions[0]

                import os
                import io
                from Google import Create_Service
                from googleapiclient.http import MediaIoBaseDownload
                import pathlib
                import shutil

                try:
                    # Accessing Drive
                    current_directory = pathlib.Path().resolve()
                    
                    CLIENT_SECRET_FILE = os.path.join(current_directory, "client_secret_1029816507264-v01sr0lv1g58jl1ssnuqso82b4k4sett.apps.googleusercontent.com.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)

                    file_ids = []
                    file_names = []
                    file_ids.append(latest_version[0])
                    file_names.append(latest_version[1])

                    file_directory = ""

                    for file_id, file_name in zip(file_ids, file_names):
                        request = service.files().get_media(fileId=file_id)

                        fh = io.BytesIO()

                        downloader = MediaIoBaseDownload(fd=fh, request=request)

                        done = False
                        while not done:
                            status, done = downloader.next_chunk()
                            print("Download Progress: {0}".format(status.progress() * 100))

                        fh.seek(0)

                        current_directory = pathlib.Path(__file__).parent.resolve()

                        file_directory = os.path.join(current_directory, file_name)
                        print(file_directory)
                        with open(os.path.join(current_directory, file_name), 'wb') as f:
                            f.write(fh.read())
                            f.close()

                    print("Service Completed")
                    try:
                        shutil.unpack_archive(file_directory, current_directory)
                    except Exception as e:
                        print(e)
                except Exception as e:
                    messagebox.showerror("Error", message=e)

This is the Google File

    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
import tkinter
from tkinter import messagebox


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:
        messagebox.showerror("Error", "Unable to connect: {}\nCred: {}".format(e, cred))
        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
0 Answers
Related