使用 Python 包编译器创建 Python 包
支持的平台:Windows®、Linux®、Mac
此示例演示了如何使用 Python 包编译器将多个 MATLAB® 函数打包成一个 Python® 包。您可以在一个名为 Python 的应用程序中调用该包,该应用程序可从一个矩形中计算数据。
在 R2025a 之前的版本中: 使用库编译器创建一个 Python 包,具体操作请参考使用库编译器应用程序创建 Python 应用程序 (R2024b)。
前提条件
验证是否已满足生成 Python 包和运行 Python 应用程序的系统要求。有关详细信息,请参阅MATLABCompiler SDK Python 目标要求。
最终用户必须安装 MATLAB Runtime 才能运行应用程序。有关详细信息,请参阅下载并安装 MATLAB Runtime。
出于测试目的,您可以使用安装的 MATLAB 来代替 MATLAB Runtime。
编写可部署的 MATLAB 代码
在 MATLAB 中,检查您想要打包的 MATLAB 代码。此示例使用 目录中的这些文件:\extern\examples\compilersdk\python\rectangle\matlabroot
| MATLAB 函数 | getPointCoordinates.m getRectangleArea.mgetRectangleCorners.m getRectangleHeight.mgetRectangleWidth.mmakePoint.mmakeRectangle.mPoint.mRectangle.mrectangleDemo.m |
| Python 应用程序代码 | rectangleDriver.py |
在 MATLAB 命令提示符下,将随 MATLAB 一起提供的 rectangle 文件夹复制到您的工作目录中。
copyfile(fullfile(matlabroot,"extern","examples", ... "compilersdk","python","rectangle"),"rectangle");
导航至新的 rectangle 文件夹。
检查 MATLAB 函数 rectangleDemo.m。
该函数创建两个 Point 对象,使用这些点作为角点创建一个 Rectangle 对象,然后计算并显示有关矩形的数据。
function rectangleDemo() % RECTANGLEDEMO Construct a rectangle and print information about it pointZeroZero = makePoint(0, 0); pointThreeFour = makePoint(3, 4); rectA = makeRectangle(pointZeroZero, pointThreeFour); corners = getRectangleCorners(rectA); showPointCoordinates(corners.upperLeft, 'Upper left-hand corner'); showPointCoordinates(corners.lowerLeft, 'Lower left-hand corner'); showPointCoordinates(corners.upperRight, 'Upper right-hand corner'); showPointCoordinates(corners.lowerRight, 'Lower right-hand corner'); fprintf('Area: %.1f\n', area(rectA)); fprintf('Height: %.1f\n', height(rectA)); fprintf('Width: %.1f\n', width(rectA)); end % This is an auxiliary function. It cannot be called outside rectangleDemo(). function showPointCoordinates(pt, desc) coordinates = getPointCoordinates(pt); fprintf('%s: (%.1f, %.1f)\n', desc, coordinates.X, coordinates.Y); end
创建工程和编译任务
使用 Python 包编译器创建函数的编译任务。编译器任务允许您为特定部署目标编译工程中的文件。
要打开该 App,在 App 选项卡上,展开 App 图库。在应用程序部署部分,点击 Python 包编译器。

您还可以在 MATLAB 命令行窗口中使用 pythonPackageCompiler 函数打开该 App。
打开 App 后,“创建编译器任务”对话框会提示您将任务添加到新的或现有 MATLAB 工程中。在此示例中,选择启动新工程并创建编译器任务,并在 rectangle 文件夹中创建一个名为 RectangleProject 的新工程。有关创建和使用 MATLAB 工程的详细信息,请参阅创建工程。

编辑器中打开了一个名为 PythonPackage1 的新编译任务。您可以通过打开编译器任务管理器或转到管理任务选项卡并创建一个新的编译器任务来编译其他部署目标的代码。
指定编译选项
在打包之前,您可以为 Python 软件包及其安装程序指定选项,以自定义构建和打包过程。例如,您可以混淆处理 MATLAB 代码,或指定在生成的安装程序中包含 MATLAB Runtime 的方法。
在此示例中,在编译器任务的导出的函数部分,点击添加文件,然后选择 rectangleDemo.m。在“工程”面板中,该文件现在带有标签 Design 和 Exported Function File。

依赖关系分析过程会自动查找并包含支持文件 MATLAB(例如 Point.m 和 Rectangle.m),然后将它们添加到包中。有关详细信息,请参阅使用 MATLAB Compiler 进行依赖关系分析。在“工程”面板中,文件带有标签 Design。

在包信息部分,将字符串 My Python Package 替换为您的 Python 包的名称,即 rectangleLib。
查看代码和包 Python 包
要查看包含有关构建和打包组件的说明的代码,请点击导出编译脚本旁边的箭头,然后选择显示代码。在右侧,一个窗口显示了一个部署脚本,其中包含与您的构建选项相对应的 compiler.build.pythonPackage 和 compiler.package.installer 函数。您可以通过点击导出编译脚本按钮将此代码转换为 MATLAB 脚本文件。运行生成的构建脚本与点击编译和打包按钮等效。

要创建不带安装程序的 Python 软件包,请点击编译和打包 > 编译。
编译器会在工程文件夹中的 文件夹中生成文件。<compiler_task_name>/outputbuild 子文件夹包含 Python 包,而 package 子文件夹包含 Python 包的安装程序以及 MATLAB 运行时。要为生成的文件选择不同的输出位置,请更新输出位置部分中的路径。
创建并运行 Python 应用程序
创建 Python 包后,您需要编写 Python 应用程序的源代码,该应用程序会调用包中的 MATLAB 函数。
一个名为 rectangleDriver.py 的 Python 应用程序示例包含在 rectangle 文件夹中。
rectangleDriver 应用程序执行这些操作。
定义两个类,
PyPoint和PyRectangle。使用
rectangleLib.initialize创建对包的句柄。将包句柄传递给
PyPoint和PyRectangle类。创建一个
PyRectangle对象,并打印其面积、高度和宽度。将每个角保存为一个
PyPoint对象,并打印其坐标。调用
rectangleDemoMATLAB 函数,从不同的矩形中创建并显示数据。
要运行由 MATLAB Compiler SDK™ 生成的 Python 包,您必须安装 MATLAB Runtime。有关详细信息,请参阅下载并安装 MATLAB Runtime。如果您使用编译和打包创建了安装程序,该安装程序包含与用于编译 Python 包的 MATLAB 版本相匹配的 MATLAB 运行时版本。
注意
要测试应用程序是否针对 MATLAB 而不是 MATLAB Runtime,请在 MATLAB 命令行窗口中,在以下系统命令前添加 !(感叹号)运算符。例如,!python rectangleDriver.py。
打开系统命令提示符窗口,并导航至包含您生成的 Python 安装脚本的构建输出文件夹,该脚本名为 setup.py。
使用 python 命令安装该包。有关详细信息,请参阅安装并导入 MATLAB Compiler SDK Python 包。
python -m pip install .导航到工程根文件夹,运行 rectangleDriver.py 应用程序。
cd D:\Work\rectangle
python rectangleDriver.py
Initializing module and creating a rectangle with corners (-1, 2) and (5, 10)...
Area: 48.0
Height: 8.0
Width: 6.0
Corners:
upperLeft: (-1.0, 2.0)
lowerLeft: (-1.0, 10.0)
upperRight: (5.0, 2.0)
lowerRight: (5.0, 10.0)
Executing rectangleDemo...
Upper left-hand corner: (0.0, 0.0)
Lower left-hand corner: (0.0, 4.0)
Upper right-hand corner: (3.0, 0.0)
Lower right-hand corner: (3.0, 4.0)
Area: 12.0
Height: 4.0
Width: 3.0
注意
在 macOS 上,您必须使用 mwpython 脚本,而不能使用 python。例如,mwpython rectangleDriver.py。
mwpython 脚本位于 文件夹中,其中 matlabroot/binmatlabroot 是 MATLAB 或 MATLAB Runtime 的安装位置。
另请参阅
Python 包编译器 | compiler.build.pythonPackage | mwpython
