pyinstaller后不能运行的问题处理

开发完python程序后使用pyinstaller 打包成exe程序后出现错误:

通常的打包命令:
pyinstaller.exe --hidden-import=queue -w -F $FileName$

pyinstaller后不能运行的问题处理_第1张图片
运行出错

为了定位问题的出现需要修改上述打包命令为:

pyinstaller.exe --hidden-import=queue -F $FileName$

在运行程序后快速进行屏幕抓取:(可能需要抓多次)


pyinstaller后不能运行的问题处理_第2张图片
错误信息

从上图中,可以看出由于文件找不到导致程序出错。
从这里也可以大致了解下pyinstaller成可执行程序的流程是:
会将exe解压到系统某处(图中为AppData/Local/Temp下),然后解释执行。

解决 FileNotFoundError 问题

https://gist.github.com/GaryLee/d191f239acf2f47bec93

# When you're encountering following error.
# > IOError: [Errno 2] No such file or directory: 'C:\\Users\\username\\AppData\\Local\\Temp\\_MEI502322\\openpyxl\\.constants.json'
# 
# This solution tested under Python 2.7.10 and Pyinstaller 3.0.
# 
# Put this file to your script folder. 
# Add this hook to your distribution by
# > pyinstaller --onefile --additional-hooks-dir=. yourscript.py
#
from PyInstaller.utils.hooks import collect_data_files

# Instruct pyinstaller to collect data files from openpyxl package.
datas = collect_data_files('openpyxl')

在工程下创建 hook-jsonrpcclient.py

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('jsonrpcclient')

然后使用如下命令打包:

pyinstaller.exe --onefile --additional-hooks-dir=. --hidden-import=queue -w -F robotManagerTools.py

pyinstaller后不能运行的问题处理_第3张图片
处理示图

[参考]
https://pythonhosted.org/PyInstaller/spec-files.html#adding-data-files
https://pyinstaller.readthedocs.io/en/stable/usage.html
https://pythonhosted.org/PyInstaller/hooks.html

你可能感兴趣的:(pyinstaller后不能运行的问题处理)