1、要打包的项目(例子)
# 设置默认注释:settings->editor->file and code Templates
import tkinter as tk
import random
import threading
import time
def dow():
window = tk.Tk()
window.title('谢谢你')
window.geometry("200x50" + "+" + str(random.randrange(0, window.winfo_screenwidth())) + "+" + str(
random.randrange(0, window.winfo_screenheight())))
tk.Label(window,
text='谢谢你!', # 标签的文字
bg='Green', # 背景颜色
font=('楷体', 20), # 字体和字体大小
width=20, height=4 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
threads = []
for i in range(10): # 需要的弹框数量
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.1)
# time.sleep(0.00000000000000000000000001) # 间隔时间
threads[i].start()
2、安装 Pyinstaller 模块
pip install pyinstaller
国内镜像(速度快)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
3、在终端进入要打包的项目目录
4、输入打包命令
pyinstaller -F -w -i mc.ico 1_test.py
参数 | 说明 |
---|---|
-F或-D | -F表示打包为单个exe,-D表示打包为一个文件夹(含exe),前者运行时 启动较慢,后者相对较快 |
-w | 表示运行exe时不显示黑框,第一次打包时建议显示(exe运行不成功可以查看报错) |
-i | 表示设置exe的图标,为.ico文件 |
mc.ico | 图标 |
1_test.py | 要打包的文件 |
5、 打包成功后目录会多出几个文件,其中 dist 里面就是 .exe 文件,可直接运行
两个目录:build、dist
一个文件:newImg.spec
6、快速结束程序方法(赠送)
任务管理器==》详细信息==》里面找到程序结束即可
参考 :Python打包为EXE(可执行文件)_月明星稀---的博客-CSDN博客_python打包成exe文件
谢谢 月明星稀---
打包后运行.exe报错
问题1:找不到相应库
解决:
进入所使用的python解析库目录,找到 pyinstaller 库目录下的 hooks 文件夹,添加一个文件,上面缺的是librosa库,所以添加了 hook-librosa.py 文件(命名规范 hook-***.py 即可)
添加内容(里面 librosa 是缺的库名)
from PyInstaller.utils.hooks import collect_submodules, collect_data_files
datas = collect_data_files('librosa')
保存后把原打包内容删掉,重新打包即可
问题2:缺失项目引用的文件
把项目引用的文件拷贝到打包的 dist 目录,跟 .exe 放一起即可
参考1:PyInstaller关于No such file or directory报错 - 腾讯云开发者社区-腾讯云
参考2:使用pyinstaller打包程序运行时出现No such file or directory的问题解决 - 灰信网(软件开发博客聚合)
二次打包 .spec 不成功问题
pyinstaller main.spec
1、报错 UPX is not available.
400 INFO: PyInstaller: 5.3
400 INFO: Python: 3.7.8
402 INFO: Platform: Windows-10-10.0.17763-SP0
428 INFO: UPX is not available.
430 INFO: Extending PYTHONPATH with paths
['F:\\gs\\python\\audio3']
1139 INFO: checking Analysis
3383 INFO: checking PYZ
3775 INFO: checking PKG
5408 INFO: Bootloader c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
5408 INFO: checking EXE
解决:
1、访问网址: upx下载
2、下载对应版本压缩包
3、解压后把里面的 .exe 文件拷贝到所使用的 python 解释器的 Scripts 目录里面即可
参考:UPX is not available_骡马跪族的博客-CSDN博客_upx怎么上不了网
end