Files
hzdyjs/学校学生操行历史数据迁移/README-Python兼容性.md
T
2026-01-04 10:00:08 +08:00

170 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Python版本兼容性说明
## 问题分析
原始脚本使用了Python 3.7+的语法特性,在Python 3.6环境运行报错:
```
SyntaxError: invalid syntax
```
### 具体问题
1. **`from __future__ import annotations`** - Python 3.7+特性
2. **类型注解语法** `-> None` - 在`__future__`导入后变为强制语法
## 解决方案
### 文件版本说明
| 文件名 | Python版本 | 特性支持 | 适用环境 |
|--------|-----------|----------|----------|
| `02_parse_score_data_fixed.py` | 3.7+ | 类型注解 + f-string | 开发环境 |
| `02_parse_score_data_py36.py` | 3.6+ | 移除类型注解 | 兼容版本 |
| `02_parse_score_data_py36_compatible.py` | 3.6+ | 移除类型注解 + .format()格式 | 中等兼容环境 |
| `02_parse_score_data_py34_compatible.py` | 3.4+ | 无typing模块 + .format()格式 | **超老环境推荐** |
### 常见错误及解决方案
#### 错误:`ImportError: No module named typing`
**原因**: Python版本过低(< 3.5)或未安装typing模块
**解决**: 使用 `02_parse_score_data_py34_compatible.py`
#### 错误:`SyntaxError: invalid syntax` (f-string)
**原因**: Python版本不支持f-string(< 3.6)
**解决**: 使用带`.format()`的兼容版本
#### 错误:`SyntaxError: invalid syntax` (类型注解)
**原因**: Python版本不支持类型注解语法
**解决**: 使用移除类型注解的版本
### 生产环境使用
#### 方法1:智能启动脚本(强烈推荐)
```bash
# 自动检测Python版本和可用模块,自动选择最合适的脚本版本
./run_parser.sh
# 查看运行日志
tail -f 2.log
```
#### 方法2:手动选择版本
```bash
# 对于超老环境(Python < 3.6 或无typing模块)
nohup python3 02_parse_score_data_py34_compatible.py > 2.log 2>&1 &
# 对于Python 3.6环境
nohup python3 02_parse_score_data_py36_compatible.py > 2.log 2>&1 &
```
#### 方法2:直接运行
```bash
# Python 3.6环境使用完全兼容版本
nohup python3 02_parse_score_data_py36_compatible.py > 2.log 2>&1 &
# 查看运行日志
tail -100f 2.log
```
#### 进程管理
```bash
# 查看进程状态
ps aux | grep python
# 停止进程
kill $(cat parser.pid)
# 查看日志统计
grep "解析成功" 2.log | wc -l # 成功记录数
grep "解析失败" 2.log | wc -l # 失败记录数
```
### 版本对比
#### 02_parse_score_data_py36_compatible.py (推荐用于生产环境)
- ✅ 完全兼容Python 3.6
- ✅ 使用 `.format()` 替代 f-string
- ✅ 移除所有类型注解
- ✅ 避免复杂的字符串嵌套表达式
- ✅ 稳定的生产环境运行
### 代码修改内容
#### 移除的特性
```python
# 移除此行
from __future__ import annotations
# 函数定义从:
def connect_db(self) -> None:
# 改为:
def connect_db(self):
```
#### 保持兼容的特性
- f-string (Python 3.6+)
- `pathlib` (Python 3.6+)
- `dataclasses` (Python 3.7+但未使用)
## 版本检测脚本
```bash
# 检查Python版本
python3 --version
# 检查语法兼容性
python3 -m py_compile 02_parse_score_data_py36.py
```
## 环境升级建议
如果可能,建议升级生产环境到Python 3.8+,这样可以:
- 使用更新的pandas版本
- 获得更好的性能
- 支持完整的类型注解语法
- 使用现代的异步特性
## 故障排除指南
#### 问题1:`ImportError: No module named typing`
```bash
# 原因:Python版本过低(< 3.5)
# 解决:使用无typing版本
python3 02_parse_score_data_py34_compatible.py
```
#### 问题2:`SyntaxError: invalid syntax` (f-string相关)
```bash
# 原因:Python版本不支持f-string(< 3.6)
# 解决:使用.format()版本
python3 02_parse_score_data_py36_compatible.py
```
#### 问题3:依赖安装失败
```bash
# 检查依赖安装
pip3 install --user -r requirements-prod.txt
python3 test_dependencies.py
```
#### 问题4:数据库连接问题
```bash
# 检查数据库配置
python3 -c "from db_config import DB_CONFIG; print('配置正确')"
```
#### 通用诊断命令
```bash
# 检查Python版本
python3 --version
# 检查typing模块
python3 -c "import typing; print('typing可用')" 2>/dev/null || echo "无typing模块"
# 检查依赖
python3 -c "import pymysql, pandas, sqlalchemy; print('依赖正常')"
# 测试脚本语法
python3 -m py_compile 02_parse_score_data_py34_compatible.py && echo "语法正确"
```