Main Content

Generate a Python Package and Build a Python Application

Supported platforms: Windows®, Linux®, Mac

This example shows how to create a Python® package from a MATLAB® function and integrate the generated package into a Python application.

Prerequisites

Create Function in MATLAB

In MATLAB, examine the MATLAB code that you want packaged. For this example, write a function makesqr.m.

function makesqr(x)
y = magic(x);
disp(y)

At the MATLAB command prompt, enter makesqr(5).

The output is a 5x5 matrix.

    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

Create Python Application Using Library Compiler App

Compile the function into a Python package using the Library Compiler app. Alternatively, if you want to create a Python package from the MATLAB command window using a programmatic approach, see Create Python Package Using compiler.build.pythonPackage.

  1. 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.

    libraryCompiler

    Compiler tab with the Library Compiler app open

  2. 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.

    1. In the Exported Functions section of the toolstrip, click Add exported function to the project.

    2. 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 wrote earlier.

  3. 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 file name of the installer.

    • Runtime included in package — Generate an application that includes the MATLAB Runtime installer. You can specify the file name of the installer.

      Note

      The first time you select this option, you are prompted to download the MATLAB Runtime installer.

Specify Package Settings

Next, define the name of your Python package.

  • The Library Name field is automatically populated with 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.

Create Sample Driver File

You can add MATLAB files to the project to generate sample Python driver files. Although Python driver files are not necessary to create a package, you can use them to implement a Python application, as shown in Install and Run MATLAB Generated Python Application.

In the Samples section, select Create New Sample, and click makesqr.m. A 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);

Change x = 0 to x = 4, save the file, and return to the Library Compiler app.

For more information and limitations, see Sample Driver File Creation.

Customize the Application and Its Appearance

In the Library Compiler app, you can customize the installer, customize your application, and add more information about the application.

  • 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.

    See Specify Files to Install with Application.

Fields in the Library Compiler app for customizing your application

Package the Application

When you are finished selecting your packaging options, save your Library Compiler project and generate the packaged application.

  1. Click Package.

    In the Save Project dialog box, specify the location to save the project.

  2. 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 in the target folder.

    • Three folders are generated: for_redistribution, for_redistribution_files_only, and for_testing.

      For more information about the files generated in these folders, see Files Generated After Packaging MATLAB Functions.

    • The log file PackagingLog.html contains packaging results.

Create Python Package Using compiler.build.pythonPackage

As an alternative to the Library Compiler app, you can create a Python package using a programmatic approach using the following steps.

  1. Save the following code in a sample file named makesqrSample1.m:

    x = 4;
    a = makesqr(x);

  2. Build the Python package using the compiler.build.pythonPackage function and the makesqr.m file that you wrote earlier. Use name-value arguments to specify the package name and add a sample file.

    buildResults = compiler.build.pythonPackage('makesqr.m',...
    'PackageName','MagicSquarePkg',...
    'SampleGenerationFiles','makesqrSample1.m');

    The compiler.build.Results object buildResults contains information on the build type, generated files, and build options.

  3. The function generates the following files within a folder named MagicSquarePkgpythonPackage in your current working directory:

    • 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 Limitations.

    • 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.

    Note

    The generated package does not include MATLAB Runtime or an installer. To create an installer using the buildResults object, see compiler.package.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 package.

    • '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 process.

Install and Run MATLAB Generated Python Application

After creating your Python package, you can call it from a Python application. This example uses the sample Python code generated during packaging. You can use this sample Python application code as a guide to write your own Python application.

  1. Copy and paste the generated Python file makesqrSample1.py from the samples folder into the folder that contains the setup.py file.

  2. At the system command prompt, navigate to the folder that contains makesqrSample1.py and setup.py.

  3. Install the application using the python command.

    python setup.py install

    To install to a location other than the default, consult "Installing Python Modules" in the official Python documentation.

  4. Run the application. If you used sample MATLAB code in the packaging steps, this application returns the same output as the sample code.

    python makesqrSample1.py

    Note

    On macOS, you must use the mwpython script instead of python. For example, mwpython makesqrSample1.py.

    The mwpython script is located in the matlabroot/bin folder, where matlabroot is the location of your MATLAB installation.

See Also

| | | |