Pyinstaller 使用笔记

发布时间 2023-09-30 07:41:40作者: klchang

Pyinstaller 用于将 Python 应用和连同其所需依赖打包在一起,使其可以在不安装 Python 解释器的相同操作系统的计算机上运行。Pyinstaller 库支持 Python 3.8 及其更新版本,可以正确地打包较大 Python 包 ,如 numpy, matplotlib, PyQt, wxPython 等等。需要注意, Pyinstaller 不是一个跨平台的工具,也就是说在 Windows 打得包只能运行在 Windows 系统上。

 

安装 

# 安装 pyinstaller 
pip install pyinstaller
# 更新到最新版本的 pyinstaller 
pip install -U pyinstaller 
# 采用国内阿里 pypi 镜像安装加速
pip install pyinstaller -i https://mirrors.aliyun.com/pypi/simple/

 

使用

使用命令行选项打包

通过命令行选项输入相关参数,具体命令如下:

pyinstaller -F -w main.py -n 测试工具

其中,

-F 选项,创建一个打包的执行文件;-w 选项,执行文件运行过程中,不出现命令行窗口;-n 测试工具,指定输出的可执行文件名为 "测试工具"。更多选项,参见 Using Pyinstaller - Options 。

使用配置文件打包

pyinstaller 测试工具.spec

配置文件 "测试工具.spec" 的具体内容,如下所示

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='测试工具',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

从配置文件中,可以比较明显地看出,对 main.py 文件进行打包,不显示 console 命令行窗口,输出执行文件名为 "测试工具"。

 

调试

01. 重新带有 console 的可执行文件

如果 Pyinstaller 生成的可执行文件运行过程中,出现闪退问题。通过重新生成带有 console 命令行的可执行文件,具体命令如下:

pyinstaller -F -c main.py -n 测试工具

从上面命令可以看出,只需把 -w 选项替换为 -c 选项,即可。

或者直接修改配置文件,将 console=False 改为 console=True,重新运行如下命令:

pyinstaller 测试工具.spec

02. 命令行界面运行可执行文件(不要直接双击运行)

为了避免闪退时,命令行界面同时闪退,需要在命令行(Windows cmd 或 Linux shell)下运行 Pyinstaller 生成的可执行文件。

 

问题及解决方法

问题1,

Python 代码直接运行无问题,而 Pyinstaller 打包生成的可执行文件运行时,出现 ModuleNotFoundError:  No module named XXXXXX 问题。

解决方法,

问题原因是,依赖包动态导入其他 Python 库。此时,需要把 XXXXXX 库,添加到配置文件的 hiddenimports=[] 的列表,修改为 hiddenimports=['XXXXXX'] ,其中 XXXXXX 为没有找到的模块名,注意,模块名需要放到引号内。

 

参考资料

[1] PyInstaller Manual. https://pyinstaller.org/en/stable/. 

[2] 2个技巧,学会Pyinstaller打包的高级用法. https://zhuanlan.zhihu.com/p/398619997