PyDrive を用いてGoogleドライブからデスクトップにファイルをダウンロードするプログラムを作成し、正常に動作することを確認しました。

しかし、このファイルをexe化したところ、想定通りの動作をせず一瞬ウィンドウが立ち上がった後すぐ閉じてしまいました。原因や確認すべき項目がありましたらご教示頂けますか。

なお、Pythonのバージョンは3.6で、inputやprintだけの簡単な内容のものであれば問題なくexe化できました。  
そもそもPyDrive認証を含むpyファイルのexe化はできないものなのでしょうか？

### pyファイルの中身

```

    # 必要なライブラリのインポート
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive  
    file_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
    
    # OAuth認証を行う
    gauth = GoogleAuth()
    gauth.CommandLineAuth()
    drive = GoogleDrive(gauth)
    
    # ファイルをGoogleドライブからダウンロード
    f = drive.CreateFile({'id': file_id})
    f.GetContentFile('abc.bat')
    
```

exe化のコマンドは `py -m PyInstaller dl.py --onefile` で、結果は以下の通りです。  
※pyファイルの名前は「dl.py」  
※exe化はコマンドプロンプトにて実施  

```

     　　42 INFO: PyInstaller: 3.6
        142 INFO: Python: 3.6.8
        142 INFO: Platform: Windows-10-10.0.18362-SP0
        144 INFO: wrote C:\Users\admin\Desktop\dl.spec
        145 INFO: UPX is not available.
        150 INFO: Extending PYTHONPATH with paths
        ['C:\\Users\\admin\\Desktop', 'C:\\Users\\admin\\Desktop']
        151 INFO: checking Analysis
        152 INFO: Building Analysis because Analysis-00.toc is non existent
        152 INFO: Initializing module dependency graph...
        159 INFO: Caching module graph hooks...
        175 INFO: Analyzing base_library.zip ...
        3614 INFO: Caching module dependency graph...
        3690 INFO: running Analysis Analysis-00.toc
        3693 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
          required by C:\Users\admin\AppData\Local\Programs\Python\Python36\python.exe
        3778 INFO: Analyzing C:\Users\admin\Desktop\dl.py
        3976 INFO: Processing pre-safe import module hook   six.moves
        4494 INFO: Processing pre-find module path hook   distutils
        4496 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\admin\\AppData\\Local\\Programs\\Python\\Python36\\lib'
        5973 INFO: Processing module hooks...
        5973 INFO: Loading module hook "hook-certifi.py"...
        5976 INFO: Loading module hook "hook-distutils.py"...
        5977 INFO: Loading module hook "hook-encodings.py"...
        6149 INFO: Loading module hook "hook-google.api_core.py"...
        6152 INFO: Loading module hook "hook-httplib2.py"...
        6154 INFO: Loading module hook "hook-pkg_resources.py"...
        6724 INFO: Processing pre-safe import module hook   win32com
        6810 INFO: Excluding import '__main__'
        6812 INFO:   Removing import of __main__ from module pkg_resources
        6812 INFO: Loading module hook "hook-pydoc.py"...
        6813 INFO: Loading module hook "hook-pythoncom.py"...
        7190 INFO: Loading module hook "hook-pywintypes.py"...
        7571 INFO: Loading module hook "hook-sysconfig.py"...
        7573 INFO: Loading module hook "hook-win32com.py"...
        8063 INFO: Loading module hook "hook-xml.py"...
        8296 INFO: Looking for ctypes DLLs
        8308 INFO: Analyzing run-time hooks ...
        8312 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
        8316 INFO: Including run-time hook 'pyi_rth_certifi.py'
        8318 INFO: Including run-time hook 'pyi_rth_pkgres.py'
        8319 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
        8327 INFO: Looking for dynamic libraries
        8748 INFO: Looking for eggs
        8748 INFO: Using Python library C:\Users\admin\AppData\Local\Programs\Python\Python36\python36.dll
        8749 INFO: Found binding redirects:
        []
        8759 INFO: Warnings written to C:\Users\admin\Desktop\build\dl\warn-dl.txt
        8929 INFO: Graph cross-reference written to C:\Users\admin\Desktop\build\dl\xref-dl.html
        8967 INFO: checking PYZ
        8967 INFO: Building PYZ because PYZ-00.toc is non existent
        8968 INFO: Building PYZ (ZlibArchive) C:\Users\admin\Desktop\build\dl\PYZ-00.pyz
        10427 INFO: Building PYZ (ZlibArchive) C:\Users\admin\Desktop\build\dl\PYZ-00.pyz completed successfully.
        10468 INFO: checking PKG
        10468 INFO: Building PKG because PKG-00.toc is non existent
        10469 INFO: Building PKG (CArchive) PKG-00.pkg
        13745 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
        13749 INFO: Bootloader C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
        13750 INFO: checking EXE
        13750 INFO: Building EXE because EXE-00.toc is non existent
        13750 INFO: Building EXE from EXE-00.toc
        13751 INFO: Appending archive to EXE C:\Users\admin\Desktop\dist\dl.exe
        13768 INFO: Building EXE from EXE-00.toc completed successfully.
    
```

コマンドプロンプトにてexeファイルを実行した結果は以下の通りです。

```

        C:\Users\admin\Desktop>dl.exe
    Traceback (most recent call last):
      File "C:\Users\admin\Desktop\dl.py", line 2, in <module>
        from pydrive.auth import GoogleAuth
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
        exec(bytecode, module.__dict__)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\pydrive\auth.py", line 7, in <module>
        from apiclient.discovery import build
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
        exec(bytecode, module.__dict__)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\apiclient\__init__.py", line 8, in <module>
        from googleapiclient import discovery
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
        exec(bytecode, module.__dict__)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\discovery.py", line 67, in <module>
        from googleapiclient.http import build_http
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
        exec(bytecode, module.__dict__)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\http.py", line 67, in <module>
        from googleapiclient.model import JsonModel
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
        exec(bytecode, module.__dict__)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\model.py", line 36, in <module>
        _LIBRARY_VERSION = pkg_resources.get_distribution("google-api-python-client").version
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\pkg_resources\__init__.py", line 479, in get_distribution
        dist = get_provider(dist)
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\pkg_resources\__init__.py", line 355, in get_provider
        return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\pkg_resources\__init__.py", line 898, in require
        needed = self.resolve(parse_requirements(requirements))
      File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\pkg_resources\__init__.py", line 784, in resolve
        raise DistributionNotFound(req, requirers)
    pkg_resources.DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application
    [21036] Failed to execute script dl
    C:\Users\admin\Desktop>
    
```

exe実行時のエラーメッセージ`pkg_resources.DistributionNotFound: The 'google-api-python-
client' distribution was not found and is required by the application`  
を確認しましたが、既にgoogle-api-python-clientはインストール/アップグレード済です。  
他のサイトでも探してみましたが、これといった解決方法が見つかりませんでした。
CC BY-SA 4.0
2020-07-10T09:17:13.343
68465
2020-07-14T04:33:59.503
2020-07-14T04:33:59.503
41037
41037
question
python python3 google-api pyinstaller
PyDrive での認証を行うプログラムが、PyInstaller で exe 化すると正常に動かない
コマンドプロンプトから実行してみてください。多分、その部分でエラーが出ていると思います。
CC BY-SA 4.0
2020-07-11T09:35:05.023
68492
2020-07-11T09:35:05.023
35940
68465
answer