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 osdef res_path(relative_path):"""获取资源绝对路径"""try:base_path = sys._MEIPASSexcept 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 zipfiledef res_path(relative_path):"""获取资源绝对路径"""try:base_path = sys._MEIPASSexcept 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 // 压缩文件