# 嵌入式Python方案指南
## 🎯 方案概述
这个方案将Python环境直接打包到你的Tauri应用中,用户无需安装Python即可使用所有功能。
## 🚀 快速开始
### 1. 设置嵌入式Python环境
```cmd
# 运行设置脚本
scripts\setup_embedded_python.bat
```
这将:
- 下载Python 3.11嵌入式版本
- 安装必要的依赖(requests, Pillow等)
- 复制你的python_core模块
- 创建启动脚本
- 更新Tauri配置
### 2. 测试嵌入式Python
```cmd
# 测试Python版本
src-tauri\python-embed\python.exe --version
# 测试依赖
src-tauri\python-embed\python.exe -c "import requests, PIL; print('Dependencies OK')"
# 测试python_core模块
src-tauri\python-embed\python.exe -c "import python_core; print('python_core OK')"
```
### 3. 启动应用测试
```cmd
cargo tauri dev
```
现在应用会自动使用嵌入式Python,无需系统Python环境。
## 📁 目录结构
设置完成后,你的项目结构如下:
```
your-project/
├── src-tauri/
│ ├── python-embed/ # 嵌入式Python环境
│ │ ├── python.exe # Python解释器
│ │ ├── python311.dll # Python库
│ │ ├── Lib/ # 标准库
│ │ ├── Scripts/ # pip等工具
│ │ ├── python_core/ # 你的Python模块
│ │ └── python-core.bat # 启动脚本
│ └── tauri.conf.json # 已更新配置
├── python_core/ # 原始Python代码
└── scripts/
├── setup_embedded_python.py
└── setup_embedded_python.bat
```
## 🔧 工作原理
### 执行优先级
Rust代码会按以下优先级选择Python环境:
1. **嵌入式Python** - 如果 `src-tauri/python-embed/python.exe` 存在
2. **Sidecar** - 如果 `mixvideo-python-core.exe` 存在
3. **系统Python** - 作为最后的回退选项
### 环境变量控制
你可以通过环境变量强制使用特定的Python:
```cmd
# 强制使用系统Python
set MIXVIDEO_FORCE_SYSTEM_PYTHON=1
cargo tauri dev
```
## 📦 构建和分发
### 开发模式
在开发模式下,嵌入式Python位于 `src-tauri/python-embed/`
### 生产构建
```cmd
cargo tauri build
```
构建时,Tauri会自动将 `python-embed` 目录打包到最终的可执行文件中。
### 分发
最终的应用包含:
- 你的Tauri应用
- 完整的Python环境
- 所有Python依赖
- 你的python_core模块
用户无需安装任何额外软件即可运行。
## 🧪 测试和调试
### 测试嵌入式Python
```cmd
# 直接测试
src-tauri\python-embed\python.exe -m python_core.main --module test --action hello
# 测试特定服务
src-tauri\python-embed\python.exe -m python_core.services.template_manager --action get_templates
```
### 在应用中测试
1. 启动应用:`cargo tauri dev`
2. 访问Python测试页面
3. 运行测试 - 应该显示"Using embedded Python for execution"
### 调试问题
如果遇到问题:
1. **检查Python路径**:
```cmd
dir src-tauri\python-embed\python.exe
```
2. **检查模块导入**:
```cmd
src-tauri\python-embed\python.exe -c "import sys; print(sys.path)"
```
3. **查看日志**:
- 控制台会显示使用的Python类型
- 检查错误信息
## ⚙️ 自定义配置
### 添加更多依赖
编辑 `scripts/setup_embedded_python.py`:
```python
dependencies = [
"requests",
"Pillow",
"numpy", # 添加新依赖
"opencv-python", # 添加新依赖
]
```
然后重新运行设置脚本。
### 更新Python版本
修改 `setup_embedded_python.py` 中的下载URL:
```python
python_url = "https://www.python.org/ftp/python/3.12.0/python-3.12.0-embed-amd64.zip"
```
### 自定义Python路径
如果需要自定义路径,修改 `get_embedded_python_path()` 函数。
## 🎉 优势
### 对比其他方案
| 方案 | 优势 | 劣势 |
|------|------|------|
| **嵌入式Python** | ✅ 无需用户安装
✅ 版本一致
✅ 完全控制 | ❌ 包体积较大
❌ 更新需要重新分发 |
| Sidecar | ✅ 包体积小
✅ 独立更新 | ❌ 构建复杂
❌ 依赖管理困难 |
| 系统Python | ✅ 包体积最小 | ❌ 需要用户安装
❌ 版本不一致 |
### 适用场景
嵌入式Python特别适合:
- 面向普通用户的应用
- 需要特定Python版本的应用
- 希望简化部署的企业应用
- 不希望依赖外部环境的应用
## 🔄 维护和更新
### 更新Python代码
1. 修改 `python_core/` 中的代码
2. 重新运行设置脚本:`scripts\setup_embedded_python.bat`
3. 测试更改:`cargo tauri dev`
### 更新依赖
1. 修改 `setup_embedded_python.py` 中的依赖列表
2. 重新运行设置脚本
3. 测试新依赖
### 版本管理
建议在版本控制中:
- ✅ 包含 `scripts/setup_embedded_python.py`
- ✅ 包含 `python_core/` 源代码
- ❌ 排除 `src-tauri/python-embed/` (太大)
- ✅ 在CI/CD中自动运行设置脚本
## 📋 最佳实践
1. **定期更新Python版本** - 保持安全性
2. **最小化依赖** - 减少包体积
3. **测试多平台** - 确保跨平台兼容性
4. **监控包大小** - 避免过度膨胀
5. **文档化依赖** - 便于维护
---
这个方案为你提供了一个完全自包含的Python环境,确保用户体验的一致性和简便性!