質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PyInstaller

PyInstallerは、Pythonのスクリプトを一括でWindowsなどで動く実行可能ファイルに変換できるツールです。このツールを用いることで自作のPythonプログラムを別で使用する場合でもPythonをインストールする必要がありません。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2512閲覧

pyinstallerで作成したexeファイルで発生するpkg_resources.DistributionNotFoundの対処法について

tomy-c

総合スコア179

PyInstaller

PyInstallerは、Pythonのスクリプトを一括でWindowsなどで動く実行可能ファイルに変換できるツールです。このツールを用いることで自作のPythonプログラムを別で使用する場合でもPythonをインストールする必要がありません。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/07/19 07:07

編集2019/07/19 07:12

pytonで最新バージョンのgoogle翻訳APIを組み込んだ実行ファイルを作成したいという目的の元、
下記の環境にてgoogle translate api のサンプルプログラムを作成しました。

・python3.7 64bit(公式より取得)
・VisualStudioCode (拡張機能でPythonの環境+日本語化をしています。インタプリタは上のpython3.7 64bit版です)
・powershell
・google translate api v3beta1

発生している問題・エラーメッセージ

現在の状態として
・コンソールから直接実行した場合
・VSCodeから実行した場合
上記の場合は実行結果は正常に動作します。

ですが、pyinstallerを使用し、出力されたexeファイルを実行した場合下記のエラーが発生し、
動作しませんでした。

pyinstaller で出力した実行ファイルで正常に動作させたい場合どうすれば宜しいのでしょうか?

PS C:\Users\"ユーザー名”\Documents\google_translate_test1\dist> .\main.exe ['C:\Users\"ユーザー名”\AppData\Local\Temp\_MEI225602\base_library.zip', 'C:\Users\"ユーザー名”\AppData\Local\Temp\_MEI225602', 'C:\Users\"ユーザー名”\Documents\google_translate_test1\dist\lib'] Traceback (most recent call last): File "google_translate_test1\main.py", line 41, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "c:\users\"ユーザー名”\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module exec(bytecode, module.__dict__) File "google_translate_test1\lib\google\cloud\translate_v3beta1\__init__.py", line 20, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "c:\users\"ユーザー名”\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module exec(bytecode, module.__dict__) File "site-packages\google\cloud\translate_v3beta1\__init__.py", line 21, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "c:\users\"ユーザー名”\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module exec(bytecode, module.__dict__) File "site-packages\google\cloud\translate_v3beta1\gapic\translation_service_client.py", line 47, in <module> File "site-packages\pkg_resources\__init__.py", line 481, in get_distribution File "site-packages\pkg_resources\__init__.py", line 357, in get_provider File "site-packages\pkg_resources\__init__.py", line 900, in require File "site-packages\pkg_resources\__init__.py", line 786, in resolve pkg_resources.DistributionNotFound: The 'google-cloud-translate' distribution was not found and is required by the application

pipで取得してきたgoogle-cloud-translateに含まれている
google-cloud-translateに対して DistributionNotFoundが発生しているため、pyinstallerでexeファイルを作成時に正常なpathが設定できていないと考えています。

該当のソースコード

※実験用プログラムの為作りが荒いことをお詫びします。
また、実行するためにはGoogleCloudPlatformに登録後クレジットカード等の決済手段を登録し、GCPよりプロジェクトの作成→秘密鍵.jsonの発行が必要になります。これらはサンプルソースコードの1行目のurlに手順が記載されてます。翻訳が中途半端ですが。

python

1# https://cloud.google.com/translate/docs/quickstart-client-libraries-v3?hl=ja 2# pip install --upgrade google-cloud-translate -t "プロジェクトディレクトリ/lib" 3 4# クライアントAPI gRPC通信している? 5# VSCodeではインタプリタを別途インストールしたpythonに変更しておくこと 6# (コンソールでの実行とVSCodeではデフォルトのインタプリタが異なり、ライブラリパスの参照先も異なる) 7# (pip でライブラリをinstallしている場合は特に影響がある? ※未確認) 8# https://code.visualstudio.com/docs/python/environments 9# VSCode → python 32bit 10# Ctrl + Shift + P → "Python: Select Interpreter" → Python3.7.4 64bit 11 12#デバッグ設定 lunch.json 13# https://stackoverflow.com/questions/54160948/how-to-fix-the-path-attribute-not-found-error-for-packages-installed-by-pi 14#{ 15# "name": "Python: Module", 16# "type": "python", 17# "request": "launch", 18# "module": "main", #エントリーポイントファイル名 (拡張子不要) 19# "console": "integratedTerminal" 20# }, 21 22# 作成したプロジェクトの秘密鍵をインポート(環境変数に登録する方法は駄目だった) 23import inspect # デバッグ用 24import os 25import sys 26 27#ライブラリへのパスを追加 28# ROOTPATH = os.path.dirname(__file__) 29ROOTPATH = os.path.dirname(sys.argv[0]) #pytinstaller では __file__は使用不可能 30LIBPATH = os.path.join(ROOTPATH, 'lib') 31sys.path.append(LIBPATH) 32 33# python上で環境変数を追加(google core import前に実行) 34os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'プロジェクト作成時にダウンロードできる秘密鍵.json' 35 36# デバッグ用(読み込んでいるディレクトリの確認) 37import pprint 38pprint.pprint(sys.path) 39 40# 無料なのはversion3のβ1版のみ 41from lib.google.cloud import translate_v3beta1 as translate 42client = translate.TranslationServiceClient() 43 44 45 46 47text = u'Hello, world!' 48location = 'global' 49 50 51parent = client.location_path('発行したプロジェクトのID', location) 52 53# 国コードは以下参照 54# https://ja.wikipedia.org/wiki/ISO_3166-1 55response = client.translate_text( 56 parent=parent, 57 contents=[text], 58 mime_type='text/plain', # mime types: text/plain, text/html 59 source_language_code='en-US', 60 target_language_code='fr') 61 62# ver3Beta1では一部言語の結果が8進数で返ってくる模様・・・→翻訳結果の指定方法がGoogle公式のサンプル通りでは出ず、 63# translation.translated_textに翻訳結果が格納されていた。 64for translation in response.translations: 65 66# オブジェクトの中身表示 67# print( inspect.getmembers( object) ) 68 print(u'Translated Text: {}'.format(translation.translated_text)) 69 70#入力待ち 71# input() 72

sys.path比較

コンソールからpyファイルを直接実行時に出力されたsys.path

['C:\Users\"ユーザー名”\Documents\google_translate_test1', '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', '.\lib']

pyinstallerで作成したexeが出力したsys.path

['C:\Users\"ユーザー名”\Documents\google_translate_test1\dist\main\base_library.zip', 'C:\Users\"ユーザー名”\Documents\google_translate_test1\dist\main', 'lib', '.\lib']

※pyinstaller側は圧倒的にpathが不足している

補足情報

プロジェクトの構成は以下のようになっています

├─.vscode #VSCodeのデバッグ用設定とかが入っている ├─build #pyinstaller 実行時のビルド ├─dist #pyinstaller 実行時の実行ファイル格納場所 └─lib #pipで入手したgoogle-cloud-translateクライアントライブラリ格納場所(作った) ├─bin ├─cachetools ├─cachetools-3.1.1.dist-info ├─certifi ├─certifi-2019.6.16.dist-info ├─chardet ├─chardet-3.0.4.dist-info ├─google │ ├─auth │ ├─cloud │ │ ├─translate_v2 │ │ └─translate_v3beta1 │ ├─logging │ ├─longrunning │ ├─oauth2 │ ├─protobuf │ ├─rpc │ └─type ├─googleapis_common_protos-1.6.0-py3.7.egg-info ├─google_api_core-1.14.0.dist-info ├─google_auth-1.6.3.dist-info ├─google_cloud_core-1.0.2.dist-info ├─google_cloud_translate-1.6.0.dist-info ├─grpc ├─grpcio-1.22.0.dist-info ├─idna ├─idna-2.8.dist-info ├─pkg_resources ├─protobuf-3.9.0.dist-info ├─pyasn1 ├─pyasn1-0.4.5.dist-info ├─pyasn1_modules ├─pyasn1_modules-0.2.5.dist-info ├─pytz ├─pytz-2019.1.dist-info ├─requests ├─requests-2.22.0.dist-info ├─rsa ├─rsa-4.0.dist-info ├─setuptools ├─setuptools-41.0.1.dist-info ├─six-1.12.0.dist-info ├─urllib3 └─urllib3-1.25.3.dist-info

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

PyinstallerでFirestoreを使ったPythonスクリプトをEXE化する際のTips
https://qiita.com/akitooo/items/eb82a5f335d8ca9c9faf

google translate とは違いますが、上記の内容を元に main.pyファイルと同じディレクトリに生成されていたmain.specファイルを開き下記のpathexに必要なライブラリのパスを全部記述した状態でpyinstallerを実行することで少なくとも今回発生したエラーは解消されました。 ※別のエラーが発生し、結局動いていないのでこの方法が正しいかは確証が持てませんが。(その問題に対しては別途質問をする予定です。)

python

1# -*- mode: python ; coding: utf-8 -*- 2 3block_cipher = None 4 5 6a = Analysis(['main.py'], 7 pathex=['コンソール実行時に出ていたsys.pathを全部記述'], 8 binaries=[], 9 datas=[], 10 hiddenimports=[], 11 hookspath=[], 12 runtime_hooks=[], 13 excludes=[], 14 win_no_prefer_redirects=False, 15 win_private_assemblies=False, 16 cipher=block_cipher, 17 noarchive=False) 18pyz = PYZ(a.pure, a.zipped_data, 19 cipher=block_cipher) 20exe = EXE(pyz, 21 a.scripts, 22 a.binaries, 23 a.zipfiles, 24 a.datas, 25 [], 26 name='main', 27 debug=False, 28 bootloader_ignore_signals=False, 29 strip=False, 30 upx=True, 31 upx_exclude=[], 32 runtime_tmpdir=None, 33 console=True ) 34

投稿2019/07/23 00:39

tomy-c

総合スコア179

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問