A-A+

使用PyInstaller打包并压缩文件

2025年06月23日 17:16 学习笔记 暂无评论 共833字 (阅读613 views次)

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】

# 安装PyInstaller
pip install pyinstaller

# 生成spec文件
pyi-makespec --onefile --windowed --name="文本处理工具" your_script.py

# 编辑生成的spec文件,添加以下内容来减小体积:

修改spec文件:

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

block_cipher = None

a = Analysis(
    ['your_script.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=['matplotlib', 'numpy', 'pandas', 'scipy', 'IPython', 'jupyter'],  # 排除不需要的大包
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    noarchive=False,
)

pyx = PYZ(a.pure, a.zipped_data)

exe = EXE(
    pyx,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='文本处理工具',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,  # 启用UPX压缩
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

然后执行:

pyinstaller your_script.spec

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×

给我留言