打包文件
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()
|
||||||
|
|
||||||
62
build_exe.spec
Normal file
62
build_exe.spec
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# -*- mode: python ; coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
PyInstaller 配置文件
|
||||||
|
可以直接使用: pyinstaller build_exe.spec
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
block_cipher = None
|
||||||
|
|
||||||
|
# 收集所有需要打包的Python文件
|
||||||
|
a = Analysis(
|
||||||
|
['main.py'],
|
||||||
|
pathex=[],
|
||||||
|
binaries=[],
|
||||||
|
datas=[
|
||||||
|
('fabric_library.db', '.') if os.path.exists('fabric_library.db') else None,
|
||||||
|
],
|
||||||
|
hiddenimports=[
|
||||||
|
'PyQt5.QtCore',
|
||||||
|
'PyQt5.QtGui',
|
||||||
|
'PyQt5.QtWidgets',
|
||||||
|
'sqlite3',
|
||||||
|
],
|
||||||
|
hookspath=[],
|
||||||
|
hooksconfig={},
|
||||||
|
runtime_hooks=[],
|
||||||
|
excludes=[],
|
||||||
|
win_no_prefer_redirects=False,
|
||||||
|
win_private_assemblies=False,
|
||||||
|
cipher=block_cipher,
|
||||||
|
noarchive=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 过滤掉None值
|
||||||
|
a.datas = [d for d in a.datas if d is not None]
|
||||||
|
|
||||||
|
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
|
||||||
|
|
||||||
|
exe = EXE(
|
||||||
|
pyz,
|
||||||
|
a.scripts,
|
||||||
|
a.binaries,
|
||||||
|
a.zipfiles,
|
||||||
|
a.datas,
|
||||||
|
[],
|
||||||
|
name='服装布料计算管理器',
|
||||||
|
debug=False,
|
||||||
|
bootloader_ignore_signals=False,
|
||||||
|
strip=False,
|
||||||
|
upx=True,
|
||||||
|
upx_exclude=[],
|
||||||
|
runtime_tmpdir=None,
|
||||||
|
console=False, # 不显示控制台窗口
|
||||||
|
disable_windowed_traceback=False,
|
||||||
|
argv_emulation=False,
|
||||||
|
target_arch=None,
|
||||||
|
codesign_identity=None,
|
||||||
|
entitlements_file=None,
|
||||||
|
icon=None, # 可以指定图标文件路径,例如: 'icon.ico'
|
||||||
|
)
|
||||||
|
|
||||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
PyQt5>=5.15.0
|
||||||
|
pyinstaller>=5.0.0
|
||||||
|
|
||||||
30
打包.bat
Normal file
30
打包.bat
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
@echo off
|
||||||
|
chcp 65001 >nul
|
||||||
|
echo ========================================
|
||||||
|
echo 服装布料计算管理器 - 打包脚本
|
||||||
|
echo ========================================
|
||||||
|
echo.
|
||||||
|
|
||||||
|
echo 正在检查依赖...
|
||||||
|
python -c "import PyQt5" 2>nul
|
||||||
|
if errorlevel 1 (
|
||||||
|
echo 错误: 未安装PyQt5,正在安装...
|
||||||
|
pip install -r requirements.txt
|
||||||
|
if errorlevel 1 (
|
||||||
|
echo 安装失败,请手动执行: pip install -r requirements.txt
|
||||||
|
pause
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo 开始打包...
|
||||||
|
python build_exe.py
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ========================================
|
||||||
|
echo 打包完成!
|
||||||
|
echo exe文件位置: dist\服装布料计算管理器.exe
|
||||||
|
echo ========================================
|
||||||
|
pause
|
||||||
|
|
||||||
85
打包说明.md
Normal file
85
打包说明.md
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
# 打包为EXE文件说明
|
||||||
|
|
||||||
|
## 前置要求
|
||||||
|
|
||||||
|
1. 确保已安装Python 3.7或更高版本
|
||||||
|
2. 安装项目依赖:
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
## 打包方法
|
||||||
|
|
||||||
|
### 方法一:使用打包脚本(推荐)
|
||||||
|
|
||||||
|
直接运行打包脚本:
|
||||||
|
```bash
|
||||||
|
python build_exe.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方法二:使用PyInstaller命令
|
||||||
|
|
||||||
|
使用spec文件打包:
|
||||||
|
```bash
|
||||||
|
pyinstaller build_exe.spec
|
||||||
|
```
|
||||||
|
|
||||||
|
或者直接使用命令行:
|
||||||
|
```bash
|
||||||
|
pyinstaller --name=服装布料计算管理器 --onefile --windowed --add-data=fabric_library.db;. main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方法三:使用spec文件(推荐用于自定义配置)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pyinstaller build_exe.spec
|
||||||
|
```
|
||||||
|
|
||||||
|
## 打包输出
|
||||||
|
|
||||||
|
打包完成后,生成的exe文件位于 `dist` 目录中:
|
||||||
|
- `dist/服装布料计算管理器.exe`
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
1. **数据库文件**:如果项目中有 `fabric_library.db` 文件,打包脚本会自动将其包含在exe同目录下。首次运行exe时,如果数据库不存在,程序会自动创建。
|
||||||
|
|
||||||
|
2. **文件大小**:打包后的exe文件可能较大(通常50-100MB),这是因为包含了Python解释器和所有依赖库。
|
||||||
|
|
||||||
|
3. **杀毒软件**:某些杀毒软件可能会误报,这是正常现象。PyInstaller打包的exe文件需要添加白名单。
|
||||||
|
|
||||||
|
4. **依赖库**:确保所有依赖都已正确安装,特别是PyQt5。
|
||||||
|
|
||||||
|
5. **测试**:打包完成后,建议在干净的Windows系统上测试exe文件是否能正常运行。
|
||||||
|
|
||||||
|
## 常见问题
|
||||||
|
|
||||||
|
### 问题1:打包失败,提示找不到模块
|
||||||
|
**解决方案**:在 `build_exe.spec` 的 `hiddenimports` 中添加缺失的模块。
|
||||||
|
|
||||||
|
### 问题2:exe运行时缺少DLL文件
|
||||||
|
**解决方案**:确保PyQt5已正确安装,可以尝试重新安装:
|
||||||
|
```bash
|
||||||
|
pip uninstall PyQt5
|
||||||
|
pip install PyQt5
|
||||||
|
```
|
||||||
|
|
||||||
|
### 问题3:exe文件太大
|
||||||
|
**解决方案**:
|
||||||
|
- 使用 `--exclude-module` 排除不需要的模块
|
||||||
|
- 使用 `--onedir` 模式代替 `--onefile`(会生成一个文件夹而不是单个exe)
|
||||||
|
|
||||||
|
### 问题4:数据库路径问题
|
||||||
|
**解决方案**:程序已自动处理数据库路径,exe运行时会在exe同目录下查找或创建数据库文件。
|
||||||
|
|
||||||
|
## 优化建议
|
||||||
|
|
||||||
|
1. **添加图标**:在 `build_exe.spec` 中设置 `icon='icon.ico'`,需要准备一个ico格式的图标文件。
|
||||||
|
|
||||||
|
2. **减小体积**:如果不需要某些功能,可以排除相关模块:
|
||||||
|
```python
|
||||||
|
excludes=['matplotlib', 'numpy', 'pandas'] # 示例
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **版本信息**:可以创建版本信息文件(.rc文件)并添加到spec配置中。
|
||||||
|
|
||||||
Reference in New Issue
Block a user