工作中需要把python程序打包成exe过程中,配置pyinstaller,遇到的问题及解决办法,防止以后忘记。
1、配置pyinstaller 环境
首先安装pywin32: pip install pywin32
安装pyinstaller :pip install pyinstaller
注意:如果不先安装pywin32的话会出现很多模块找不到的问题,找了好久再发现是这个问题的,所以最好在安装pyinstalller之前安装pywin32
2、介绍下pyinstaller使用命令:
假如不要控制台只要界面的话
pyinstaller -F -w python脚本文件
要添加图标的话:pyinstaller -F -i ico图片 python脚本文件
main.py内容如下:
def add(x,y):
return (x+y)
把该脚本打包成有控制台的exe则运行下面的命令:
pyinstaller -F main.py
运行之后则在脚本所在的目录下生成dist 和build文件夹
dist:文件夹下面生成的main.exe就是我们想要的exe程序,点击exe程序运行即可
3、执行打包命令遇到的问题及解决方案:
1)、问题:RecursionError: maximum recursion depth exceeded:该问题是打包的程序深度超出了系统默认的深度
解决方案:
- 执行 pyinstaller,会生成 main.spec文件:
- 在main.spec文件头加上下面代码
import sys
sys.setrecursionlimit(5000) - 再次执行 pyinstaller 和 .spec文件: pyinstaller main.spec即可
2)、问题:
File “D:\Python34\lib\site-packages\PyInstaller\hooks\hook-zmq.py”, line 18, in
hiddenimports.extend(collect_submodules(‘zmq.backend’))
File “D:\Python34\lib\site-packages\PyInstaller\utils\hooks_init_.py”, line 619, in collect_submodules
repr(pkg_dir), package))
File “D:\Python34\lib\site-packages\PyInstaller\utils\hooks_init_.py”, line 90, in exec_statement
return _exec_python_cmd(cmd)
File "D:\Python34\lib\site-packages\PyInstaller\utils\hooks_init.py", line 77, in __exec_python_cmd
txt = exec_python(*cmd, env=pp_env)
File “D:\Python34\lib\site-packages\PyInstaller\compat.py”, line 549, in exec_python
return exec_command(*cmdargs, **kwargs)
File “D:\Python34\lib\site-packages\PyInstaller\compat.py”, line 356, in exec_command
out = out.decode(encoding)
解决方案:
找到D:\Python34\lib\site-packages\PyInstaller\compat.py", line 356,
out = out.decode(encoding)
修改为:
out = out.decode(encoding, errors=‘ignore’)
3)、问题:AttributeError: module ‘enum’ has no attribute ‘IntFlag’
解决方案:原因:Windows解决多版本python执行pip3时出错AttributeError: module ‘enum’ has no attribute ‘IntFlag’?
这可能是由enum34包引起的。因为python 3.4中有一个标准库枚举模块,所以您应该卸载enum34,它不再与标准库中的枚举兼容,因为枚举。IntFlag是在python 3.6中添加的
办法:卸载enum34: pip uninstall enum34