※前回質問からの続きになります
https://teratail.com/questions/201443
現在pythonを使用し、google translate api v3beta1を利用した試作アプリを作っています。
前回のsys.pathエラーはなんとか回避できたのですが、pyinstallerで作成した実行ファイルを起動した際、今度は以下のようなエラーが発生してしまいます。
Exception ignored in:'grpc._cthon.cygrpc.ssl_roots_override_callback' E0723 (発生時刻) src/core/lib/security/seecurity_connector/ssl_utils.cc:453] assertion failed: pem_root_certs != nullptr
エラーの内容を調査したところ、stackoverflowやgithubにてssl認証が出来ていないというような内容の記事がいくつか見つかったためそこに記述されていた対処法をいくつか試してみましたがうまくいきませんでした。
ちなみにpyファイルをコンソールから直接起動する場合は正常に動作します。
このエラーを解消するにはどのような対応をすればよいのでしょうか。
御教唆お願い致します。
該当のソースコード
※前回から動作を少し変えました
引数1:翻訳したい内容
引数2:翻訳元の言語
引数3:翻訳先の言語
結果はcsvにutf-8で出力
python
1# coding: utf_8 2# https://cloud.google.com/translate/docs/quickstart-client-libraries-v3?hl=ja 3# pip install --upgrade google-cloud-translate -t "プロジェクトフォルダ/lib" 4 5# クライアントAPI gRPC通信している? 6# VSCodeではインタプリタを別途インストールしたpythonに変更しておくこと 7# (コンソールでの実行とVSCodeではデフォルトのインタプリタが異なり、ライブラリパスの参照先も異なる) 8# (pip でライブラリをinstallしている場合は特に影響がある? ※未確認) 9# https://code.visualstudio.com/docs/python/environments 10# VSCode → python 32bit 11# Ctrl + Shift + P → "Python: Select Interpreter" → Python3.7.4 64bit 12 13#デバッグ設定 lunch.json 14# https://stackoverflow.com/questions/54160948/how-to-fix-the-path-attribute-not-found-error-for-packages-installed-by-pi 15#{ 16# "name": "Python: Module", 17# "type": "python", 18# "request": "launch", 19# "module": "main", #エントリーポイントファイル名 (拡張子不要) 20# "console": "integratedTerminal" 21# }, 22 23# pyinsteller後の実行ファイル起動時、ライブラリが足りないとエラーが出る場合は 24# *.specファイルのpathexに必要なライブラリを全て記述して再度 pyinstaller する 25# https://codeday.me/jp/qa/20190404/548372.html 26# a = Analysis(['main.py'], 27# pathex=['path1',"path2","path3".........], 28# binaries=[], 29# datas=[], 30# hiddenimports=[], 31# ...... 32 33 34# 作成したプロジェクトの秘密鍵をインポート(環境変数に登録する方法は駄目だった) 35import inspect # デバッグ用 36import os 37import sys 38import csv #ファイル出力用 39 40 41#ライブラリへのパスを追加 42# ROOTPATH = os.path.dirname(__file__) 43ROOTPATH = os.path.dirname(sys.argv[0]) #pytinstaller では __file__は使用不可能 44LIBPATH = os.path.join(ROOTPATH, 'lib') 45sys.path.append(LIBPATH) 46 47# python上で環境変数を追加(google core import前に実行) 48os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'プロジェクト作成時に発行された秘密鍵.json' 49 50# SSL証明書のパス[※grpc\_cython\_credentials に入っていた証明書](pyファイル直接起動ならこの行とファイルは不要) 51# ※1 grpcのssl証明書の有効期限に注意(切れたらライブラリの更新が必要になるか、下記から取得) 52# ※2 証明書自体はここでも配布している(https://github.com/grpc/grpc/blob/master/etc/roots.pem) 53# https://qiita.com/akitooo/items/eb82a5f335d8ca9c9faf 54os.environ['REQUESTS_CA_BUNDLE'] = "./roots.pem" 55 56 57# デバッグ用(読み込んでいるディレクトリの確認) 58import pprint 59pprint.pprint(sys.path) 60 61# 無料なのはversion3のβ1版のみ 62from lib.google.cloud import translate_v3beta1 as translate 63client = translate.TranslationServiceClient() 64 65 66 67def main(): 68 #text = u'Hello, world!' 69 text = sys.argv[1] 70 #source = 'en-US' 71 source = sys.argv[2] 72 #target = 'fr' 73 target = sys.argv[3] 74 location = 'global' 75 76 parent = client.location_path('発行したプロジェクトのID', location) 77 78 # 国コードは以下参照 79 # https://ja.wikipedia.org/wiki/ISO_3166-1 80 response = client.translate_text( 81 parent=parent, 82 contents=[text], 83 mime_type='text/plain', # mime types: text/plain, text/html 84 source_language_code=source, 85 target_language_code=target) 86 87 # ファイル入出力でエンコードを合わせておくこと 88 f = open("./csv/output.csv", 'a', encoding="utf-8") 89 writer = csv.writer(f, lineterminator = "\n") 90 91 # ver3Beta1では一部言語の結果が8進数で返ってくる模様・・・→翻訳結果の指定方法がGoogle公式のコードと異なっていた 92 # translation.translated_textに翻訳結果が格納されていた。 93 for translation in response.translations: 94 95 # オブジェクトの中身表示 96 # print( inspect.getmembers( object) ) 97 print(u'Translated Text: {}'.format(translation.translated_text)) 98 99 # 文字列だけを出す場合は2次元配列化しておかないと一文字づつカンマ区切りで出力される(文字列が配列と解釈される) 100 writer.writerow([format(translation.translated_text)]) 101 102 #入力待ち 103 # input() 104 f.close() 105 106main()
試したこと
1・下記を参考にプログラムに証明書ファイル(roots.pem)の参照を追加しました。
https://stackoverflow.com/questions/17158529/fixing-ssl-certificate-error-in-exe-compiled-with-py2exe-or-pyinstaller/34227183#34227183
結果:pemファイルの設置なども確認して実行しましたが同じエラーが発生
2・下記を参考にspecファイルのdatasにpemファイルのパスを追加(ssl証明書を実行ファイルに組み込もうとしました)
https://github.com/googleapis/google-cloud-python/issues/5774
結果:pyinstallerの実行そのものでエラーが発生。(ログ取り忘れたので後で追加します)
その時のspecファイルは下記になります
# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis(['main.py'], pathex=['C:\Users\"ユーザー名"\Documents\google_translate_test1', 'c:\Users\"ユーザー名"\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles', 'C:\Users\"ユーザー名"\AppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\"ユーザー名"\AppData\Local\Programs\Python\Python37\DLLs', 'C:\Users\"ユーザー名"\AppData\Local\Programs\Python\Python37\lib', 'C:\Users\"ユーザー名"\AppData\Local\Programs\Python\Python37', 'C:\Users\"ユーザー名"\AppData\Roaming\Python\Python37\site-packages', 'C:\Users\"ユーザー名"\AppData\Local\Programs\Python\Python37\lib\site-packages', 'C:\Users\"ユーザー名"\Documents\google_translate_test1\lib' ], binaries=[], datas=[ ('roots.pem', 'grpc/_cython/_credentials/'), ], 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, a.binaries, a.zipfiles, a.datas, [], name='main', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=True )

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。