【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter

时间:2022-01-11 01:33:03

Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS

使用 Pyinstaller 打包时候报错

3027 ERROR: Tcl/Tk improperly installed on this system.
(lyj_venv) ➜ liyongjiandeMBP.lan [/Users/liyongjian/lyj] pyinstaller --windowed --onefile --clean --noconfirm ServenDigitsDrawV2.py
INFO: PyInstaller: 3.4
INFO: Python: 3.7.
INFO: Platform: Darwin-18.5.-x86_64-i386-64bit
INFO: wrote /Users/liyongjian/lyj/ServenDigitsDrawV2.spec
INFO: UPX is not available.
INFO: Removing temporary files and cleaning cache in /Users/liyongjian/Library/Application Support/pyinstaller
INFO: Extending PYTHONPATH with paths
['/Users/liyongjian/lyj', '/Users/liyongjian/lyj']
INFO: checking Analysis
INFO: Building Analysis because Analysis-.toc is non existent
INFO: Initializing module dependency graph...
INFO: Initializing module graph hooks...
INFO: Analyzing base_library.zip ...
INFO: running Analysis Analysis-.toc
INFO: Caching module hooks...
INFO: Analyzing /Users/liyongjian/lyj/ServenDigitsDrawV2.py
INFO: Loading module hooks...
INFO: Loading module hook "hook-_tkinter.py"...
ERROR: Tcl/Tk improperly installed on this system.
INFO: Loading module hook "hook-encodings.py"...
INFO: Loading module hook "hook-xml.py"...
INFO: Loading module hook "hook-pydoc.py"...
INFO: Looking for ctypes DLLs
INFO: Analyzing run-time hooks ...
INFO: Including run-time hook 'pyi_rth__tkinter.py'
INFO: Looking for dynamic libraries
INFO: Looking for eggs
INFO: Using Python library /Library/Frameworks/Python.framework/Versions/3.7/Python
INFO: Warnings written to /Users/liyongjian/lyj/build/ServenDigitsDrawV2/warn-ServenDigitsDrawV2.txt
INFO: Graph cross-reference written to /Users/liyongjian/lyj/build/ServenDigitsDrawV2/xref-ServenDigitsDrawV2.html
INFO: checking PYZ
INFO: Building PYZ because PYZ-.toc is non existent
INFO: Building PYZ (ZlibArchive) /Users/liyongjian/lyj/build/ServenDigitsDrawV2/PYZ-.pyz
INFO: Building PYZ (ZlibArchive) /Users/liyongjian/lyj/build/ServenDigitsDrawV2/PYZ-.pyz completed successfully.
INFO: checking PKG
INFO: Building PKG because PKG-.toc is non existent
INFO: Building PKG (CArchive) PKG-.pkg
INFO: Building PKG (CArchive) PKG-.pkg completed successfully.
INFO: Bootloader /Users/liyongjian/lyj/lyj_venv/lib/python3./site-packages/PyInstaller/bootloader/Darwin-64bit/runw
INFO: checking EXE
INFO: Building EXE because EXE-.toc is non existent
INFO: Building EXE from EXE-.toc
INFO: Appending archive to EXE /Users/liyongjian/lyj/dist/ServenDigitsDrawV2
INFO: Fixing EXE for code signing /Users/liyongjian/lyj/dist/ServenDigitsDrawV2
INFO: Building EXE from EXE-.toc completed successfully.
INFO: checking BUNDLE
INFO: Building BUNDLE because BUNDLE-.toc is non existent
INFO: Building BUNDLE BUNDLE-.toc
INFO: moving BUNDLE data files to Resource directory

执行的时候报错

➜ liyongjiandeMBP.lan [/Users/liyongjian] /Users/liyongjian/lyj/dist/ServenDigitsDrawV2 ; exit;
Traceback (most recent call last):
File "PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line , in <module>
FileNotFoundError: Tcl data directory "/var/folders/f9/6m090zhs6w55rmbmhkmpff440000gn/T/_MEITVlJln/tcl" not found.
[] Failed to execute script pyi_rth__tkinter [进程已完成]

解决办法:

在文件中增加

 and 'Python' not in path_to_tcl
PyInstaller/hooks/hook-_tkinter.py
@@ -, +, @@ def _find_tcl_tk(hook_api): # _tkinter depends on Tcl/Tk compiled as frameworks.
path_to_tcl = bins[][]
- if 'Library/Frameworks' in path_to_tcl:
+ if 'Library/Frameworks' in path_to_tcl and 'Python' not in path_to_tcl:

参考链接:https://github.com/pyinstaller/pyinstaller/pull/3830
               https://*.com/questions/51847801/pyinstaller-app-not-opening-on-mac