1
0

More than 3 years have passed since last update.

Pyinstallerでexe化した時のPydriveのエラー対処

Last updated at Posted at 2021-08-06

PyinstallerでWindowsで実行可能なexe化を行った際にPydriveがインポートされず、
GoogleDriveへのファイルのアップロードが失敗してしまったので、そのエラーの対処方法の覚書です。

環境

・OS: Windows 10 64bit
・Python 3.8.10
・PyInstaller 4.3

エラー箇所

エラーの原因となっていたのは、下記のライブラリをインポートした部分です。
やりたかったこととしては、ローカルファイルを共有ドライブにアップロードするでしたが、Uploadの際に、Anaconda上では問題なく動作していましたが、Pyinstallerでexe化するとアップロードの際にエラーが発生して、GoogleAPIErrorが発生してしまった。

GoogleDriveup.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

ローカルファイルを共有ドライブにアップロードする方法は以下の記事を参照しました。
pydrive を使って 共有ドライブにアップロードする方法

エラー内容

コンソール上で確認できたエラーはgoogleapiclient.errors.UnknownApiNameOrVersionというものでした。もう一つのモジュールでGoogleDrive上のspreadsheetの書き込みが成功していたので、PyDriveの問題ということが分かりました。

対処方法

現状のPyinstallerはexe化実行時に、PyDriveを読み込み出来てずそのせいでエラーが起きていました。
対処方法としては、Pyinstaller実行後に出力されるspecファイルの中身を書き換える必要があります。

1.ファイルの先頭にfrom PyInstaller.utils.hooks import collect_data_filesを追加する。
2.data=[],の部分をdatas=collect_data_files("googleapiclient"),に書き換える。

GoogleDriveup.spec
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files

block_cipher = None


a = Analysis(['EXE化したいファイル名'],
             pathex=['ファイルへのパス'],
             binaries=[],
             datas=collect_data_files("googleapiclient"),
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='Enabler_registor',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='EXE化したいファイル')

これで問題なくファイルのアップロードが出来ました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0