Supported platforms: Windows®, Linux®, Mac
This example shows how to create a Python® package that uses a MATLAB® function. You can integrate the generated package into a Python application. This example also shows how to call the Python package from a Python application. The target system does not require a licensed copy of MATLAB.
In MATLAB, examine the MATLAB code that you want packaged. For this example, write a function
makesqr.m as follows:
function y = makesqr(x) y = magic(x);
At the MATLAB command prompt, enter
The output is:
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow. In Application Deployment, click Library Compiler.
Alternatively, you can open the Library Compiler app from the MATLAB command prompt by entering:
In the Type section of the toolstrip, click Python Package.
In the Library Compiler app project window, specify the files of the MATLAB application that you want to deploy.
In the Exported Functions section of the toolstrip, click .
In the Add Files window, browse to the example folder, and select the function you want to package. Click Open.
The function is added to the list of exported function files. Repeat this step to package multiple files in the same application.
For this example, select the
makesqr.m file that you
In the Packaging Options section of the toolstrip, decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the options:
Runtime downloaded from web — Generate an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application. You can specify the filename of the installer.
Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the filename of the installer.
The first time you select this option, you are prompted to download the MATLAB Runtime installer.
The Library Name field is automatically
makesqr as the name of the package.
Rename it as
MagicSquarePkg. For more information
on naming requirements for the Python package, see Import Compiled Python Packages.
Add MATLAB files to generate the sample Python driver files. Although Python driver files are not necessary to create packages, they are used to demonstrate how to Install and Run MATLAB Generated Python Application.
In the Samples section, select Create
New Sample, and click
MATLAB file opens for you to edit.
% Sample script to demonstrate execution of function y = makesqr(x) x = 0; % Initialize x here y = makesqr(x);
x = 0to
x = 3, save the file, and return to the Library Compiler app. For more information and limitations, see Sample Driver File Creation.
You can customize the installer, customize your application, and add more information about the application as follows:
Library information — Information about the deployed application. You can also customize the appearance of the application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata. See Customize the Installer.
Additional installer options — Default installation path for the generated installer and custom logo selection. See Change the Installation Path.
Files required for your library to run — Additional files required by the generated application to run. These files are included in the generated application installer. See Manage Required Files in Compiler Project.
Files installed for your end user — Files that are installed with your application.
To generate the packaged application, click Package.
In the Save Project dialog box, specify the location to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the packaging process is complete, examine the generated output.
Three folders are generated in the target folder location:
For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.
PackagingLog.txt — Log file generated by
If you have already created a Python package using the Library Compiler app, you can skip this section. However, if you want to know how to create a Python package from the MATLAB command window using a programmatic approach, follow these instructions.
Save the following code in a sample file named
x = 3; a = makesqr(x);
Build the Python package using the
compiler.build.pythonPackage function and the
makesqr.m file that you wrote earlier. Use
name-value arguments to add a sample file and specify the package name.
buildResults = compiler.build.pythonPackage('makesqr.m',... 'PackageName','MagicSquarePkg',... 'SampleGenerationFiles','makesqrSample1.m');
buildResults contains information on the build
type, generated files, and build options.
This syntax generates the following files within a folder named
MagicSquarePkgpythonPackage in your current working
samples\makesqrSample1.py — Python sample driver file.
GettingStarted.html — HTML file that
contains information on integrating your package.
mccExcludedFiles.log — Log file that
contains a list of any toolbox functions that were not
included in the application. For information on
non-supported functions, see MATLAB Compiler
readme.txt — Text file that contains
packaging and interface information.
requiredMCRProducts.txt — Text file that
contains product IDs of products required by MATLAB Runtime to run the application.
setup.py — Python file that installs the package.
unresolvedSymbols.txt — Text file that
contains information on unresolved symbols.
The generated package does not include MATLAB Runtime or an installer.
You can specify additional options in the
compiler.build command by using one or more of
the following name-value arguments:
'AdditionalFiles' — Path to additional files
to include in the package.
'AutoDetectDataFiles' — Flag to automatically
include data files.
'OutputDir' — Path to the output
directory that contains generated files.
'PackageName'— Name of the generated
'SampleGenerationFiles'— MATLAB sample files used to generate sample Python package files. For more information, see Sample Driver File Creation.
'Verbose'— Flag to display progress
information indicating compiler output during the build
After creating your Python packages, you can call them from a Python application. The Python application that you create uses the sample Python driver code generated during packaging. The Python driver code calls the Python packages, and it is based on the sample MATLAB file you selected in previous setup steps.
These steps are also explained in the
Before starting, make sure that you Install and Configure MATLAB Runtime, and that you have Python installed.
Copy and paste the generated Python driver code file
samples folder into the folder that contains the
Use the system command line to navigate to the folder that contains the
generated sample Python driver code file and
Install the application using
python at the system
python setup.py install
From the system command prompt, run the application.
On macOS, you must use the
mwpython script is located in the
matlabroot is the location of your