Kaynağa Gözat

[docs] Add packaging.md (#4385)

* add packaging.md

* modify <code>

* modify packaging.nd

* modify packaging.md

* modify packaging.md

* add packaging.en.md

* modify packaging.md

* modify mkdocs.yml

* modify mkdocs.yml

* modify mkdocs.yml

* modify packaging.mmd

* modify mkdocs.yml

* modify packaging.md

* modify packaging.md

* modify mkdocs.yml
guoshengjian 3 ay önce
ebeveyn
işleme
d4ef94db9c

+ 101 - 0
docs/pipeline_deploy/packaging.en.md

@@ -0,0 +1,101 @@
+# Package PaddleX Projects
+
+This guide applies to packaging PaddleX projects using PyInstaller.
+
+> Since Nuikta's packaging principle is incompatible with PaddleX, packaging with Nuikta is currently not supported.
+
+## Preparing the Environment
+
+- **Complete the PaddleX installation according to the [PaddleX Installation Documentation](../installation/installation.en.md).**
+- **Install PyInstaller.**
+
+Install PyInstaller:
+
+```bash
+pip install pyinstaller
+```
+
+> Ensure that all dependencies required by the Python script to be packaged are installed in the current environment to prevent anomalies in the packaged executable due to missing dependencies.
+
+## Running the Packaging Script
+
+Copy the Python script below and save it as a `py` file, for example, `package.py`.
+
+```python
+import paddlex
+import importlib.metadata
+import argparse
+import subprocess
+import sys
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--file', required=True, help='Your file name, e.g. main.py.')
+parser.add_argument('--nvidia', action='store_true', help='Include NVIDIA CUDA and cuDNN dependencies.')
+
+args = parser.parse_args()
+
+main_file = args.file
+
+user_deps = [dist.metadata["Name"] for dist in importlib.metadata.distributions()]
+deps_all = list(paddlex.utils.deps.DEP_SPECS.keys())
+deps_need = [dep for dep in user_deps if dep in deps_all]
+
+cmd = [
+    "pyinstaller", main_file,
+    "--collect-data", "paddlex",
+    "--collect-binaries", "paddle"
+]
+
+if args.nvidia:
+    cmd += ["--collect-binaries", "nvidia"]
+
+for dep in deps_need:
+    cmd += ["--copy-metadata", dep]
+
+print("PyInstaller command:", " ".join(cmd))
+
+try:
+    result = subprocess.run(cmd, check=True)
+except subprocess.CalledProcessError as e:
+    print("Installation failed:", e)
+    sys.exit(1)
+```
+
+
+**Supported Script Parameters:**
+
+| Parameter         | Required | Description                                                                                                               |
+|--------------|------------------------------------------------------------------------------------------------------------------------------|---------|
+| --file   | Yes     | The name of the file to be packaged (e.g., `main.py`).
+| --nvidia     | No     | Packages NVIDIA CUDA and cuDNN related dependencies into the same directory as the executable. If the system environment paths already include CUDA and cuDNN dependencies or if CUDA and cuDNN dependencies are not required, this option can be omitted.
+
+**Example Usage of the Packaging Script:**
+
+```bash
+python package.py --file main.py
+# Packages NVIDIA CUDA and cuDNN related dependencies into the same directory as the executable.
+python package.py --file main.py --nvidia
+```
+
+**Execution Result**
+
+- The packaging script will execute a command similar to:
+
+    `pyinstaller main.py --collect-data paddlex --collect-binaries paddle [--copy-metadata xxx …]`, where `--copy-metadata xxx` dynamically adds package metadata based on the dependencies required by PaddleX installed in the current environment.
+
+- The executable file and related dependency libraries will be generated in the `dist` folder.
+
+## Appendix
+
+**The above packaging process was tested in the following environment:**
+
+- Operating System: **Win 11**
+- Python: **3.10.18**
+- PaddlePaddle: **3.0.0**
+- PaddleX: **3.1.3**
+- PyInstaller: **6.14.2**
+
+**Common Issues**
+
+- When running the executable, if you encounter an error message like `RuntimeError: xxx requires additional dependencies`, it indicates that the current packaging environment lacks the necessary dependencies. Please ensure that the environment is set up correctly as described in the Preparations section.
+- When running the executable, if an error message indicates that CUDA or cuDNN related dynamic link libraries cannot be found, please check whether the system environment variables correctly include the paths to NVIDIA CUDA and cuDNN dependencies, or consider adding `--nvidia` when running the packaging script to include CUDA and cuDNN dependencies in the same directory as the executable.

+ 101 - 0
docs/pipeline_deploy/packaging.md

@@ -0,0 +1,101 @@
+# 打包PaddleX项目
+
+本说明适用于通过PyInstaller打包PaddleX项目。
+
+> 由于Nuikta的打包原理与PaddleX不适配,当前暂不支持通过Nuikta进行打包。
+
+## 准备环境
+
+- **根据[PaddleX安装文档](../installation/installation.md)完成PaddleX安装**
+- **安装PyInstaller**
+
+安装PyInstaller:
+
+```bash
+pip install pyinstaller
+```
+
+> 请确认当前准备环境中安装有待打包的Python脚本所需的全部依赖,以避免缺少依赖导致打包后的可执行程序出现异常。
+
+## 执行打包脚本
+
+将下方Python脚本拷贝后存成`py`文件,文件名可以为`package.py`。
+
+```python
+import paddlex
+import importlib.metadata
+import argparse
+import subprocess
+import sys
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--file', required=True, help='Your file name, e.g. main.py.')
+parser.add_argument('--nvidia', action='store_true', help='Include NVIDIA CUDA and cuDNN dependencies.')
+
+args = parser.parse_args()
+
+main_file = args.file
+
+user_deps = [dist.metadata["Name"] for dist in importlib.metadata.distributions()]
+deps_all = list(paddlex.utils.deps.DEP_SPECS.keys())
+deps_need = [dep for dep in user_deps if dep in deps_all]
+
+cmd = [
+    "pyinstaller", main_file,
+    "--collect-data", "paddlex",
+    "--collect-binaries", "paddle"
+]
+
+if args.nvidia:
+    cmd += ["--collect-binaries", "nvidia"]
+
+for dep in deps_need:
+    cmd += ["--copy-metadata", dep]
+
+print("PyInstaller command:", " ".join(cmd))
+
+try:
+    result = subprocess.run(cmd, check=True)
+except subprocess.CalledProcessError as e:
+    print("Installation failed:", e)
+    sys.exit(1)
+```
+
+
+**打包脚本支持的参数如下:**
+
+| 参数         | 是否必需 | 说明                                                                                                               |
+|--------------|------------------------------------------------------------------------------------------------------------------------------|---------|
+| --file   | 是     | 你的待打包文件名(如`main.py`)。
+| --nvidia     | 否     | 将NVIDIA的CUDA、cuDNN相关依赖库一同打包到可执行文件的同级目录中。如果系统环境变量路径已包含CUDA、cuDNN相关依赖库或者不需要使用CUDA、cuDNN相关依赖库,则无需开启。
+
+**打包脚本调用示例如下:**
+
+```bash
+python package.py --file main.py
+# 将NVIDIA的CUDA、cuDNN相关依赖库打包至可执行文件的同级目录中。
+python package.py --file main.py --nvidia
+```
+
+**运行结果**
+
+- 安转脚本将执行类似如下命令:
+
+    `pyinstaller main.py --collect-data paddlex --collect-binaries paddle [--copy-metadata xxx …]`,其中`--copy-metadata xxx`会根据当前环境已安装的PaddleX需要的依赖动态添加包的元信息。
+
+- 可执行文件和相关依赖库将生成到`dist`文件夹中。
+
+## 附录
+
+**以上打包流程在如下环境中测试:**
+
+- 操作系统:**Win 11**
+- Python:**3.10.18**
+- PaddlePaddle:**3.0.0**
+- PaddleX:**3.1.3**
+- PyInstaller:**6.14.2**
+
+**常见问题**
+
+- 在运行可执行文件时,出现报错信息 `RuntimeError: xxx requires additional dependencies`,说明当前打包环境缺少相关依赖,请确认已按照准备环境部分说明正确安装环境。
+- 在运行可执行文件时,出现报错信息提示CUDA、cuDNN相关动态链接库找不到,请检查系统环境变量中是否正确添加NVIDIA的CUDA、cuDNN相关依赖库路径或者考虑在运行打包脚本时添加 `--nvidia`,将CUDA、cuDNN相关依赖库打包进可执行文件的同级目录中。

+ 2 - 0
mkdocs.yml

@@ -184,6 +184,7 @@ plugins:
             PaddleX时序任务模型配置文件参数说明: PaddleX Time Series Module Config Parameters
             模型产线部署: Pipeline Deploy
             高性能推理: High Performance Inference
+            打包PaddleX项目: Package PaddleX Projects
             服务化部署: Serving
             端侧部署: On-Device Deployment
             获取 ONNX 模型: Obtaining ONNX Models
@@ -427,6 +428,7 @@ nav:
        - 服务化部署: pipeline_deploy/serving.md
        - 端侧部署: pipeline_deploy/on_device_deployment.md
        - 获取 ONNX 模型: pipeline_deploy/paddle2onnx.md
+       - 打包PaddleX项目: pipeline_deploy/packaging.md
   - 多硬件使用:
        - 多硬件使用指南: other_devices_support/multi_devices_use_guide.md
        - 飞桨多硬件安装: