使用pyinstaller生成exe文件

2023-04-20,,

安装

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

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。

报错

出错一:

在安装pyinstaller模块包的时候报了一次错误。
AttributeError: module 'enum' has no attribute 'IntFlag'

解决:
卸载了enum34模块
pip uninstall enum34
出错二:

在堆wxpython程序封装的时候,出了一次错误。
AttributeError: 'str' object has no attribute 'items'

解决:
pip install -U --pre setuptools

《使用pyinstaller生成exe文件.doc》

下载本文的Word格式文档,以方便收藏与打印。