references/paddleocr-py313-compat.md
2.8 KB查看排版效果| 1 | # Paddleocr Py313 Compat |
| 2 | |
| 3 | > 来源: pdf-ocr-md v3.3.0 SKILL.md | 从主文件拆分,保持原内容不变 |
| 4 | |
| 5 | ### Python 3.13 兼容性注意事项 |
| 6 | |
| 7 | > ⚠️ **重要**:PaddleOCR 在 Python 3.13 环境下存在多个兼容性问题,需手动修复。 |
| 8 | |
| 9 | #### 问题1:imghdr 模块缺失 |
| 10 | |
| 11 | **现象:** |
| 12 | ``` |
| 13 | ModuleNotFoundError: No module named 'imghdr' |
| 14 | ``` |
| 15 | |
| 16 | **原因:** Python 3.13 移除了 `imghdr` 模块(原用于检测图像类型) |
| 17 | |
| 18 | **解决方案:** 创建兼容 shim 模块 |
| 19 | |
| 20 | ```python |
| 21 | # 保存到 site-packages/imghdr.py |
| 22 | """imghdr module shim for Python 3.13+ compatibility""" |
| 23 | def whatfile(f): |
| 24 | return None |
| 25 | |
| 26 | def what(buf, h=None): |
| 27 | return None |
| 28 | ``` |
| 29 | |
| 30 | #### 问题2:np.sctypes 移除 |
| 31 | |
| 32 | **现象:** |
| 33 | ``` |
| 34 | AttributeError: module 'numpy' has no attribute 'sctypes' |
| 35 | ``` |
| 36 | |
| 37 | **原因:** NumPy 2.0 移除了 `np.sctypees` 属性(`imgaug` 包使用了该属性) |
| 38 | |
| 39 | **解决方案:** 修改 `site-packages/imgaug/imgaug.py` 第44-46行 |
| 40 | |
| 41 | ```python |
| 42 | # 原代码(不可用): |
| 43 | # NP_FLOAT_TYPES = set(np.sctypes["float"]) |
| 44 | # NP_INT_TYPES = set(np.sctypes["int"]) |
| 45 | # NP_UINT_TYPES = set(np.sctypes["uint"]) |
| 46 | |
| 47 | # 修复后: |
| 48 | NP_FLOAT_TYPES = {np.float16, np.float32, np.float64} |
| 49 | NP_INT_TYPES = {np.int8, np.int16, np.int32, np.int64} |
| 50 | NP_UINT_TYPES = {np.uint8, np.uint16, np.uint32, np.uint64} |
| 51 | ``` |
| 52 | |
| 53 | #### 问题3:OpenCV 与 NumPy 2.x 不兼容 |
| 54 | |
| 55 | **现象:** |
| 56 | ``` |
| 57 | ImportError: numpy.core.multiarray failed to import |
| 58 | ``` |
| 59 | |
| 60 | **原因:** `opencv-python` 4.6 是针对 NumPy 1.x 编译的,与 NumPy 2.x ABI 不兼容 |
| 61 | |
| 62 | **解决方案:** 改用 `opencv-python-headless` 4.13.0.92+ |
| 63 | |
| 64 | ```bash |
| 65 | ~/.venvs/paddleocr/bin/pip uninstall opencv-python -y |
| 66 | ~/.venvs/paddleocr/bin/pip install opencv-python-headless --no-cache-dir |
| 67 | ``` |
| 68 | |
| 69 | #### 问题4:scikit-image DLL 加载失败(Windows) |
| 70 | |
| 71 | **现象:** |
| 72 | ``` |
| 73 | ImportError: DLL load failed while importing _cython_blas |
| 74 | ``` |
| 75 | |
| 76 | **原因:** Windows 安全策略阻止未签名的 C 扩展 DLL |
| 77 | |
| 78 | **解决方案:** |
| 79 | - 方案A:使用预编译的 wheel(推荐) |
| 80 | - 方案B:临时关闭 Windows Defender 实时保护 |
| 81 | - 方案C:使用 WSL2 或 Linux 环境 |
| 82 | |
| 83 | #### 推荐的 Python 3.13 安装命令 |
| 84 | |
| 85 | ```bash |
| 86 | # 环境 |
| 87 | python3 -m venv ~/.venvs/paddleocr |
| 88 | source ~/.venvs/paddleocr/bin/activate |
| 89 | |
| 90 | # 安装(使用 PaddlePaddle 3.0.0,支持 NumPy 2.x) |
| 91 | pip install paddlepaddle==3.0.0 |
| 92 | pip install "numpy>=2" paddleocr==2.7.3 |
| 93 | pip uninstall opencv-python -y |
| 94 | pip install opencv-python-headless --no-cache-dir |
| 95 | |
| 96 | # 创建 imghdr shim |
| 97 | cat > ~/.venvs/paddleocr/lib/python3.13/site-packages/imghdr.py << 'EOF' |
| 98 | def whatfile(f): |
| 99 | return None |
| 100 | def what(buf, h=None): |
| 101 | return None |
| 102 | EOF |
| 103 | |
| 104 | # 修补 np.sctypes 问题 |
| 105 | # 手动编辑 ~/.venvs/paddleocr/lib/python3.13/site-packages/imgaug/imgaug.py |
| 106 | ``` |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

