pyinstaller打包異常

報錯:RecursionError: maximum recursion depth exceeded
解決:
先執行一次

pyi-makespec mytest.py

會在同目錄下生成一個mytest.spec文件,編輯文件,在開頭添加

import sys
sys.setrecursionlimit(10000)

保存後,對spec執行打包

pyinstaller mytest.spec

報錯:UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xce in position
解決:
在命令行執行pyinstaller之前,先執行

chcp 65001

運行打包後的文件,報錯:File “site-packages\pkg_resources_init_.py”, line 86, in < module >
ModuleNotFoundError: No module named 'pkg_resources.py2_warn

解決:
按圖索驥,註釋掉C:\ProgramData\Anaconda3\Lib\site-packages\pkg_resources\ __init__.py的86行

#__import__('pkg_resources.py2_warn')

運行打包後的文件,報錯: File “C:\ProgramData\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”, line 475, in get_data
with open(path, ‘rb’) as fp:
FileNotFoundError: [Errno 2] No such file or directory: ‘D:\mytest\plotly\package_data\templates\plotly.json’
[21652] Failed to execute script mytest

解決:
第三方庫引用到了一些非.py、.pyc文件,這些文件在pyinstaller打包時會被忽略!上面的錯誤是plotly庫裏的一個.json文件無法找到導致的。
對於此處要這樣做,進入C:\ProgramData\Anaconda3\lib\site-packages\PyInstaller\hooks,新建hook-plotly.py,編輯

from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('plotly')

collect_data_files會讓pyinstaller在打包時自動把plotly庫裏的非.py、.pyc文件拷貝出來

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章