打包文件
This commit is contained in:
74
build_exe.py
Normal file
74
build_exe.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
打包脚本 - 将项目打包为exe文件
|
||||
使用方法: python build_exe.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import shutil
|
||||
|
||||
def build_exe():
|
||||
"""使用PyInstaller打包为exe"""
|
||||
|
||||
# 项目根目录
|
||||
root_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
os.chdir(root_dir)
|
||||
|
||||
# 主程序文件
|
||||
main_script = "main.py"
|
||||
|
||||
# 打包命令
|
||||
cmd = [
|
||||
"pyinstaller",
|
||||
"--name=服装布料计算管理器",
|
||||
"--onefile", # 打包为单个exe文件
|
||||
"--windowed", # 不显示控制台窗口
|
||||
"--hidden-import=PyQt5.QtCore",
|
||||
"--hidden-import=PyQt5.QtGui",
|
||||
"--hidden-import=PyQt5.QtWidgets",
|
||||
"--hidden-import=sqlite3",
|
||||
"--clean", # 清理临时文件
|
||||
]
|
||||
|
||||
# 如果有图标文件,添加图标参数
|
||||
if os.path.exists("icon.ico"):
|
||||
cmd.append("--icon=icon.ico")
|
||||
|
||||
# 如果数据库文件存在,添加到打包数据中
|
||||
if os.path.exists("fabric_library.db"):
|
||||
# Windows使用分号,Linux/Mac使用冒号
|
||||
separator = ";" if sys.platform == "win32" else ":"
|
||||
cmd.append(f"--add-data=fabric_library.db{separator}.")
|
||||
|
||||
cmd.append(main_script)
|
||||
|
||||
print("开始打包...")
|
||||
print(f"执行命令: {' '.join(cmd)}")
|
||||
|
||||
try:
|
||||
# 执行打包
|
||||
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
print("打包成功!")
|
||||
print(f"\n输出文件位置: {os.path.join(root_dir, 'dist', '服装布料计算管理器.exe')}")
|
||||
|
||||
# 复制数据库文件到dist目录(如果存在)
|
||||
if os.path.exists("fabric_library.db"):
|
||||
dist_dir = os.path.join(root_dir, "dist")
|
||||
if os.path.exists(dist_dir):
|
||||
shutil.copy2("fabric_library.db", dist_dir)
|
||||
print(f"数据库文件已复制到: {os.path.join(dist_dir, 'fabric_library.db')}")
|
||||
|
||||
print("\n打包完成!可以在 dist 目录中找到生成的exe文件。")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"打包失败: {e}")
|
||||
print(f"错误输出: {e.stderr}")
|
||||
sys.exit(1)
|
||||
except FileNotFoundError:
|
||||
print("错误: 未找到 pyinstaller,请先安装: pip install pyinstaller")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
build_exe()
|
||||
|
||||
Reference in New Issue
Block a user