first commit
This commit is contained in:
166
go_backend/PYTHON_CROSS_PLATFORM.md
Normal file
166
go_backend/PYTHON_CROSS_PLATFORM.md
Normal file
@@ -0,0 +1,166 @@
|
||||
# Python虚拟环境跨平台配置说明
|
||||
|
||||
## 📋 概述
|
||||
|
||||
Go服务已支持跨平台调用Python脚本,可以在Windows和Ubuntu/Linux环境下正常运行。
|
||||
|
||||
## 🔄 Windows vs Linux 路径对比
|
||||
|
||||
### Windows环境
|
||||
```
|
||||
backend/
|
||||
├── venv/
|
||||
│ ├── Scripts/ ← Windows使用Scripts目录
|
||||
│ │ ├── python.exe
|
||||
│ │ ├── activate.bat
|
||||
│ │ └── ...
|
||||
│ └── Lib/
|
||||
```
|
||||
|
||||
**Python解释器**: `backend/venv/Scripts/python.exe`
|
||||
|
||||
### Ubuntu/Linux环境
|
||||
```
|
||||
backend/
|
||||
├── venv/
|
||||
│ ├── bin/ ← Linux使用bin目录
|
||||
│ │ ├── python
|
||||
│ │ ├── activate
|
||||
│ │ └── ...
|
||||
│ └── lib/
|
||||
```
|
||||
|
||||
**Python解释器**: `backend/venv/bin/python`
|
||||
|
||||
## 🚀 部署步骤
|
||||
|
||||
### 在Ubuntu服务器上部署
|
||||
|
||||
1. **创建Python虚拟环境**:
|
||||
```bash
|
||||
cd /path/to/backend
|
||||
python3 -m venv venv
|
||||
```
|
||||
|
||||
2. **激活虚拟环境**:
|
||||
```bash
|
||||
source venv/bin/activate
|
||||
```
|
||||
|
||||
3. **安装依赖**:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
playwright install chromium
|
||||
```
|
||||
|
||||
4. **启动Go服务**:
|
||||
```bash
|
||||
cd /path/to/go_backend
|
||||
go run main.go
|
||||
```
|
||||
|
||||
### 在Windows上开发
|
||||
|
||||
1. **创建Python虚拟环境**:
|
||||
```cmd
|
||||
cd backend
|
||||
python -m venv venv
|
||||
```
|
||||
|
||||
2. **激活虚拟环境**:
|
||||
```cmd
|
||||
venv\Scripts\activate
|
||||
```
|
||||
|
||||
3. **安装依赖**:
|
||||
```cmd
|
||||
pip install -r requirements.txt
|
||||
playwright install chromium
|
||||
```
|
||||
|
||||
4. **启动Go服务**:
|
||||
```cmd
|
||||
cd go_backend
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## 🔧 技术实现
|
||||
|
||||
### 跨平台路径检测
|
||||
|
||||
Go代码中使用`runtime.GOOS`自动检测操作系统:
|
||||
|
||||
```go
|
||||
func getPythonPath(backendDir string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
// Windows: venv\Scripts\python.exe
|
||||
return filepath.Join(backendDir, "venv", "Scripts", "python.exe")
|
||||
}
|
||||
// Linux/Mac: venv/bin/python
|
||||
return filepath.Join(backendDir, "venv", "bin", "python")
|
||||
}
|
||||
```
|
||||
|
||||
### 使用位置
|
||||
|
||||
该函数在以下服务中被调用:
|
||||
- `service/xhs_service.go` - 小红书登录服务
|
||||
- `service/employee_service.go` - 员工服务(绑定小红书账号)
|
||||
|
||||
## ✅ 验证部署
|
||||
|
||||
### 测试Python环境
|
||||
```bash
|
||||
# Ubuntu
|
||||
cd backend
|
||||
source venv/bin/activate
|
||||
python xhs_cli.py --help
|
||||
|
||||
# Windows
|
||||
cd backend
|
||||
venv\Scripts\activate
|
||||
python xhs_cli.py --help
|
||||
```
|
||||
|
||||
### 测试Go调用
|
||||
```bash
|
||||
# 启动Go服务后,测试发送验证码接口
|
||||
curl -X POST http://localhost:8080/api/employee/xhs/send-code \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"phone":"13800138000"}'
|
||||
```
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
1. **不要提交venv目录到Git**:已在`.gitignore`中配置忽略
|
||||
2. **环境隔离**:Windows和Ubuntu各自维护独立的venv环境
|
||||
3. **依赖一致性**:确保requirements.txt在两个平台上一致
|
||||
4. **Playwright浏览器**:在Ubuntu上需要安装chromium依赖库
|
||||
|
||||
## 🐛 常见问题
|
||||
|
||||
### Q: Ubuntu上提示找不到Python
|
||||
**A**: 确保已安装Python3:
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install python3 python3-venv python3-pip
|
||||
```
|
||||
|
||||
### Q: Playwright启动失败
|
||||
**A**: 安装系统依赖:
|
||||
```bash
|
||||
playwright install-deps chromium
|
||||
```
|
||||
|
||||
### Q: Go服务找不到Python脚本
|
||||
**A**: 检查`backend`目录与`go_backend`目录的相对位置,确保为:
|
||||
```
|
||||
project/
|
||||
├── backend/ # Python脚本
|
||||
└── go_backend/ # Go服务
|
||||
```
|
||||
|
||||
## 📚 相关文档
|
||||
|
||||
- [Go服务环境变量配置](ENV_CONFIG_GUIDE.md)
|
||||
- [Python CLI工具文档](../backend/XHS_CLI_README.md)
|
||||
Reference in New Issue
Block a user