pyinstaller打包教程

这里写目录标题

基础篇

  • pyinstaller -F -W main.py // -F 表示打包成一个可执行文件 -W 表示去除黑框
  • pyinstaller -F -w -i ./smile.ico belle.py // 给应用程序加上图标 在Mac上打包要下载.icns格式的图标文件
  • pyinstaller -n=good main.py // 重命名 pyinstaller -n=‘good’ belle.py
  • pyinstaller --clean -y belle.py // 再次打包需要清理之前的缓存并清除临时文件
  • pyinstaller --hidden-import=xxx belle.py // ModuleNotFoundError: No module named xxx (或者直接在入口程序import 该模块,没有安装就需要pip install)

进阶篇

  • 编写批处理文件: 命令行添加到txt文件中,重命名未.bat(mac/linux用.sh)
  • 生成依赖库文件:pipreqs ./ --encoding=utf-8
pip install -r requirements.txt
xxx.bat

可执行文件运行:单文件需要解压相关依赖到(print(sys._MEIPASS))
改变临时文件夹的生成位置——使用–runtime-tmpdir命令就行了


打包资源文件:

import sys
import os
 
 
def res_path(relative_path):
    """获取资源绝对路径"""
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
 
    return os.path.join(base_path, relative_path)
win.iconbitmap(res_path('./icon.ico'))    # 设置窗口图标

pyinstaller -F -w --add-data=img/icon.ico;img belle.py  // 图片
-----------------------------------------------------------------
import os
import sys
import zipfile
 
 
def res_path(relative_path):
    """获取资源绝对路径"""
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
 
    return os.path.join(base_path, relative_path)
 
 
# 读取压缩文件
test_zip = zipfile.ZipFile(res_path('test.zip'), 'r')
 
# 返回所有内容
print(test_zip.namelist())
 
# 解压到临时文件夹中
test_zip.extractall(sys._MEIPASS)
 
os.system('pause')

pyinstaller -F --add-data=test.zip;. --runtime-tmpdir=. belle.py  // 压缩文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值