pyinstaller使用時にtkinter filedialogでエラーが出るときの対応(pyinstaller使用時のexeでのno attribute)
なぜかエラーが出て実行できなかったので調査
サンプル:
import tkinter filename = tkinter.filedialog.askopenfilename() print(filename)
・インタプリタで実行→問題なし
・pyinstallerから実行
C:\temp>pyinstaller pyinstaller_errortest.py --onefile 171 INFO: PyInstaller: 3.4 171 INFO: Python: 3.4.4 171 INFO: Platform: Windows-XP-5.1.2600-SP3 171 INFO: wrote C:\temp\pyinstaller_errortest.spec 171 INFO: UPX is not available. 187 INFO: Extending PYTHONPATH with paths ['C:\\temp', 'C:\\temp'] 187 INFO: checking Analysis 187 INFO: Building Analysis because Analysis-00.toc is non existent 187 INFO: Initializing module dependency graph... 187 INFO: Initializing module graph hooks... 203 INFO: Analyzing base_library.zip ... 6203 INFO: Processing pre-find module path hook distutils 10578 INFO: running Analysis Analysis-00.toc 11359 INFO: Caching module hooks... 11375 INFO: Analyzing C:\temp\pyinstaller_errortest.py 11843 INFO: Loading module hooks... 11843 INFO: Loading module hook "hook-distutils.py"... 11843 INFO: Loading module hook "hook-_tkinter.py"... 12250 INFO: checking Tree 12250 INFO: Building Tree because Tree-00.toc is non existent 12250 INFO: Building Tree Tree-00.toc 12593 INFO: checking Tree 12593 INFO: Building Tree because Tree-01.toc is non existent 12593 INFO: Building Tree Tree-01.toc 12640 INFO: Loading module hook "hook-xml.py"... 13406 INFO: Loading module hook "hook-encodings.py"... 13671 INFO: Loading module hook "hook-pydoc.py"... 13718 INFO: Looking for ctypes DLLs 13765 INFO: Analyzing run-time hooks ... 13781 INFO: Including run-time hook 'pyi_rth__tkinter.py' 13796 INFO: Looking for dynamic libraries 14328 INFO: Looking for eggs 14328 INFO: Using Python library C:\WINDOWS\system32\python34.dll 14328 INFO: Found binding redirects: [] 14343 INFO: Warnings written to C:\temp\build\pyinstaller_errortest\warn-pyinsta ller_errortest.txt 14484 INFO: Graph cross-reference written to C:\temp\build\pyinstaller_errortest \xref-pyinstaller_errortest.html 14875 INFO: checking PYZ 14875 INFO: Building PYZ because PYZ-00.toc is non existent 14875 INFO: Building PYZ (ZlibArchive) C:\temp\build\pyinstaller_errortest\PYZ-0 0.pyz 17015 INFO: Building PYZ (ZlibArchive) C:\temp\build\pyinstaller_errortest\PYZ-0 0.pyz completed successfully. 17062 INFO: checking PKG 17062 INFO: Building PKG because PKG-00.toc is non existent 17062 INFO: Building PKG (CArchive) PKG-00.pkg 21625 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 21921 INFO: Bootloader C:\Python34\lib\site-packages\PyInstaller\bootloader\Wind ows-32bit\run.exe 21921 INFO: checking EXE 21921 INFO: Building EXE because EXE-00.toc is non existent 21921 INFO: Building EXE from EXE-00.toc 21921 INFO: Appending archive to EXE C:\temp\dist\pyinstaller_errortest.exe 21937 INFO: Building EXE from EXE-00.toc completed successfully.
変換は問題なく完了。
C:\temp\dist>pyinstaller_errortest.exe Traceback (most recent call last): File "pyinstaller_errortest.py", line 3, in <module> AttributeError: 'module' object has no attribute 'filedialog' [4924] Failed to execute script pyinstaller_errortest
tkinterにfiledialogがないというエラー。
答えはこれ、
python - ImportError: No module named FileDialog - after PyInstaller - Stack Overflow
python - No module named when using PyInstaller - Stack Overflow
Pyinstaller won't see second level imports.
多段のインポートが失敗する
1.pyinstallerのオプション--hidden-import ○○で指定する
2.ソース内でライブラリを直接インポートする(import tkinter→from tkinter import filedialog)
1.はうまくいかず、2.で対応
ちなみにWindows10/Python3.7.2の環境では、インタプリタ実行時点で件のコードがエラーとなった(no attribute)
pyinstallerはpython3.4を使用しているようだけど、importの仕様は3.7.2に近い?のか?