pyinstaller打包python脚本为exe文件到指定路径
安装pyinstaller
直接使用pip命令安装
pip install pyinstaller
如果国内安装第三方库比较慢,可以使用以下指令进行清华源加速
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/
Python脚本打包
进入脚本路径后,执行对应的pyinstaller命令
在需要帮助时,可直接执行 pyinstaller-h
或者 pyinstaller --help
获得命令帮助
部分选项参数如下:
选项参数
-h 显示帮助信息
-v 显示版本号
–distpath 指定打包后的程序存放目录,默认存放在当前目录下的dist目录
–workpath 为输出的所有临时文件指定存放目录
-c 显示命令行窗口
-w 不显示命令行窗口
-D 生成结果是一个包含exe程序的目录,所有第三方依赖库和其他资源和exe程序位于同一目录下
-F 生成结果是一个exe程序,所有第三方依赖库和其他资源都被打包进该exe程序中
-i 为生成的程序指定一个icon图标
-n 指定生成的.exe和.spec文件名
一个在window下,带控制台输出,并仅生成一个exe文件, 同时指定icon的命令如下:
pyinstaller --console --onefile -i xxx.icon xxx.py
一个仅生成一个exe文件, 同时指定输出路径的命令如下:
pyinstaller -F test.py --distpath .\ -n newname
Q&A
问题1:打包 带有引用了 numpy 包的py脚本时, 在执行pyinstaller命令时报错,报错信息如下:
The current Numpy installation () fails to pass a sanity check due to a bug in the windows runtime
问题原因:本地的numpy 的版本是1.19.4,正好是一个有bug的脚本
解决办法:卸载当前版本,降级numpy至1.19.3
# 首先卸载numpy1.19.4
pip uninstall numpy
pip install numpy==1.19.3
参见官网bug说明:
As a workaround, you temporarily pin to NumPy 1.19.3 (`pip install numpy==1.19.3`) which has a fixed version of OpenBLAS that works correctly in Windows 2004/20H2. This fix broken other things (Linux, Windows-on-docker) and so could not be retained in NumPy 1.19.4.