pyinstaller打包时出现如下错误

Traceback (most recent call last):
  File "main.py", line 11, in <module>
  File "cnocr\cn_ocr.py", line 163, in __init__
  File "cnocr\recognizer.py", line 132, in __init__
  File "cnocr\utils.py", line 252, in read_charset
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Dell\\AppData\\Local\\Temp\\_MEI152602\\cnocr\\label_cn.txt'

解决,添加参数

pyinstaller your_script.py --add-data "cnocr/label_cn.txt:cnocr"

其中,your_script.py 是你的主程序脚本,cnocr/label_cn.txt 是要加入打包列表中的文件,cnocr 是指定文件在打包后的存放位置(相对于主程序)。
例如,

pyinstaller -F -w main.py --add-data "C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\cnocr\label_cn.txt;cnocr" --noconsole

打包成一个可执行文件时不显示提示框如warning:

方法一:禁用警告

在你的代码开头添加以下代码可以禁用警告:

import warnings
warnings.filterwarnings('ignore')

这样,警告信息将不会在运行期间弹出。

方法二:使用 --noconsole 参数

使用 --noconsole 参数打包时,将不会显示控制台窗口,这样也会禁用警告弹窗。你可以使用以下命令进行打包:

pyinstaller your_script.py --noconsole

image.png

pyinstaller其他问题

提示WARNING:file already exists but should not:_C.cp37-win_amd64

解决:报错内容可能不同,但都是xxx已存在,问题的原因是pyinstaller打包时多打了一次,所以会报已经存在了。
这个解决方案就是把多余的去掉。在自动生成的xxx.spec中,在 a 和 PYZ 中间添加如下代码,去掉多余依赖项

for d in a.datas:
	if '_C.cp37-win_amd64.pyd' in d[0]:
		a.datas.remove(d)
		break

然后重新执行

pyinstaller -F xxx.spec 进行打包

更多其他问题参考

https://blog.csdn.net/u012219045/article/details/115397646