环境: anaconda3 + python3.6 + pyinstaller
1.安装 pyinstaller
由于版本的问题,如果你使用的是python3.6,你不能直接 pip install pyinstaller,可能会导致不兼容情况出现,此时你要从github上下载开发版文件,地址:https://github.com/pyinstaller/pyinstaller
具体安装: 打开Anaconda Promput 命令窗口,直接输入pip install -U git+https://github.com/pyinstaller/pyinstaller 完成pyinstaller的安装.
2.直接打包成为.exe
打开Anaconda Promput 命令窗口,cd到你需要打包的.py文件所在目录,例如我的:cd C:\Users\Administrator\123456789 (回车)
然后直接输入pyinstaller -F (你要打包的.py文件名),举例我的:pyinstaller -F read_tupian.py
如果打包成功,在当前文件夹下会产生其他几个文件夹,其中你需要的.exe文件在dist文件夹中。
3.报错①
在打包过程中出现报错
RuntimeError: maximum recursion depth exceeded in comparison
这是因为python默认迭代次数有限(大概是1000左右)
解决方案:此时你会发现在当前文件夹下产生.spec文件,可以用编译器打开此文件,在最上方加上
import sys
sys.setrecursionlimit(1000000)
这两行代码增大迭代次数
然后在Anaconda Promput 命令窗口输入pyinstaller (你要打包的.spec文件名),举例我的:pyinstaller read_tupian.spec
报错②
倘若你使用了skimage.io.imread读图的代码,你也能成功打包,但.exe文件不可执行,而且双击.exe后出出现一个闪屏,利用手机快速抓拍,会发现闪屏上的报错内容为:
..........
RuntimeError: No suitable plugin registered for imread.
You may load I/O plugins with the `skimage.io.use_plugin` command. A list of all available plugins are shown in the `skimage.io` docstring.
解决方案为:在当前文件夹下新建文件夹命名为hooks,在hooks文件夹下新建hook-skimage.io.py 的文件,代码内容为:
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
datas = collect_data_files("skimage.io._plugins")
hiddenimports = collect_submodules('skimage.io._plugins')
然后在Anaconda Promput 命令窗口输入:
pyinstaller --additional-hooks-dir=./hooks read_tupian.spec
以下是帮到我的链接:
https://github.com/pyinstaller/pyinstaller/issues/3313
https://blog.csdn.net/qq_42063091/article/details/82423003