Featured image of post Using PyInstaller to package PaddleOCR on Ascend 310P environment

Using PyInstaller to package PaddleOCR on Ascend 310P environment

Using PyInstaller to package PaddleOCR model on Ascend 310P

Based on PaddlePaddle deployment on Ascend 310P, use PyInstaller to package PaddleOCR.

Scope and prerequisites

  • Target: Package a PaddleOCR-based CLI program for Ascend 310P NPU runtime.
  • OS/Arch: Linux x86_64 (example paths use Conda + Python 3.11).
  • Key versions:
    • Python: 3.11 (adjust paths if you use a different version)
    • PyInstaller: 6.x or newer
    • PaddlePaddle (Ascend build) and runtime for 310P is correctly installed
  • Tip: Verify environment
1
2
3
python -V
pyinstaller --version
python -c "import paddle, cv2; print(paddle.__version__, cv2.__version__)"

Spec file

Package with the following test_ocr.spec:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import copy_metadata, collect_data_files

datas = [('/opt/PaddleX/paddlex', 'paddlex')] # Python package directory of the PaddleX project source code
datas += [('/root/miniconda3/envs/asr_ocr_npu/lib/python3.11/site-packages/paddle', 'paddle')] # Directory of the Paddle PyPI package
datas += collect_data_files('lmdb')
datas += copy_metadata('ftfy')
datas += copy_metadata('lxml')
datas += copy_metadata('opencv-contrib-python')
datas += copy_metadata('pyclipper')
datas += copy_metadata('pypdfium2')
datas += copy_metadata('ultra-infer-npu-python')
datas += copy_metadata('scikit-learn')


a = Analysis(
    ['./test_ocr.py'], # Entry script to be packaged
    pathex=[],
    binaries=[('/root/miniconda3/envs/asr_ocr_npu/lib/python3.11/site-packages/paddle/libs', '.')],  # Directory of Paddle shared libraries
    datas=datas,
    hiddenimports=['cv2', 'pypdfium2', 'ultra_infer', 'paddle.cinn_config'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='test_ocr',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='test_ocr',
)

Build

1
pyinstaller ./test_ocr.spec

What each section does

  • datas: include packages and non-Python assets required at runtime (e.g., ftfy, lxml, opencv-contrib-python).
  • binaries: include Paddle shared libraries under paddle/libs so the app runs without Paddle preinstalled.
  • hiddenimports: add modules PyInstaller may miss (e.g., cv2, paddle.cinn_config, ultra_infer).
  • UPX: reduces size. If startup issues occur, try upx=False.
  • One-folder vs one-file: this builds a folder via COLLECT; it’s more reliable for large frameworks.
Facing the sea with spring blossoms.
Built with Hugo
Theme Stack designed by Jimmy