天天看点

使用pyinstaller生成exe文件

安装

注:安装版本是有要求的。

Works out-of-the-box with any Python version 2.7 / 3.4-3.7.
安装:pip install pyinstaller
pypi地址:https://pypi.org/project/PyInstaller/
官网文档:https://pyinstaller.readthedocs.io/en/stable/           

打包

方式一:
打开cmd窗口,到主文件目录文件下,运行:
pyinstaller mainscript.py

在脚本同级目录下会生成了两个文件夹。
-build
-dist

在dist中有个.exe文件,双击就可以执行了。
可以把dist文件夹单独拿出来,build不知道做什么用,其中的exe文件与dist中的相同,但是双击后会出现闪退。

如果项目下有配置文件需要加载,要把需要的配置文件放入到同exe一级的目录下。           
方式二:
打开cmd窗口,到主文件目录文件下,运行:
pyinstaller --onefile --windowed myscript.py

还是两个文件夹。
-build
-dist

dist中只有一个exe文件,但是无法打开。提示:Failed to execute script XXX。           

报错

出错一:
解决:
卸载了enum34模块
pip uninstall enum34           
出错二:
解决:
pip install -U --pre setuptools           

继续阅读