Main Content

Install and Import MATLAB Compiler SDK Python Packages

After you generate a Python® package using MATLAB® Compiler SDK™, you must install MATLAB Runtime and the package on the target machine. Import the package in your Python application to call the compiled MATLAB functions.

Install Python Package

If you create an installer using the Python Package Compiler app or the compiler.package.installer function, the installer includes the files required to install your Python package and optionally MATLAB Runtime.

You can also distribute the Python package files manually. In order to run the Python application, the target machine must have access to MATLAB Runtime that matches the version of MATLAB used to compile the package, at the same update level or newer. For details, see About MATLAB Runtime.

Using Generated Installer

  1. Distribute the installer to the desired location.

  2. Run the installer.

  3. Note the location where the installer writes the Python package files.

  4. When the installer finishes, open a system command prompt window and navigate to the folder containing the Python package files.

  5. Install the package by running python -m pip install . [options] on the system command line. If you have full administrator privileges and install to the default location, you do not need to specify any options. Otherwise, use --user to install to your home folder or --prefix="installdir" to install to a specific directory. If you installed to a location other than the default, add installdir to the PYTHONPATH environment variable. For more details, refer to the Installing Python Modules — Python 3.7.16 documentation.

  6. Add the MATLAB Runtime folders to the library path environment variable. For details, see Set MATLAB Runtime Path for Deployment.

Using Package Files

  1. Distribute the following files to integrate a Python package in an application.

    • setup.py — Python installer

    • _init_.py — initialization script for the Python package

    • packageName .ctf — deployable archive

    • Function signatures of the deployed MATLAB functions (for details, see Customize Code Suggestions and Completions)

  2. Open a system command prompt window and navigate to the folder containing the Python package files.

  3. Install the package by running python -m pip install . [options] on the system command line. If you have full administrator privileges and install to the default location, you do not need to specify any options. Otherwise, use --user to install to your home folder or --prefix="installdir" to install to a specific directory. If you installed to a location other than the default, add installdir to the PYTHONPATH environment variable. For more details, refer to the Installing Python Modules — Python 3.7.16 documentation.

  4. Install MATLAB Runtime that matches the version of MATLAB used to compile the Python package. For details, see Download and Install MATLAB Runtime.

  5. Add the MATLAB Runtime folders to the library path environment variable. For details, see Set MATLAB Runtime Path for Deployment.

Import Python Package

During compilation, you specify the package namespace, which is a period-separated list, such as companyname.groupname.packageName.

  • If you use the compiler.build.pythonPackage function, you specify the namespace using the PackageName option. The name of the generated package is set to the last entry of the period-separated list.

    If not specified, PackageName defaults to the name of the first MATLAB file listed in the FunctionFiles argument.

  • If you use the Python Package Compiler app, you specify the package name with the Package Name field and the namespace with the Namespace field.

    The Package Name field defaults to the name of the first MATLAB file listed in the app. You can leave the Namespace field empty.

  • If you use the mcc function, you specify the package name and namespace as part of the -W python:namespace.packageName flag.

    If not specified, the namespace defaults to the name of the first MATLAB file listed in the command.

Import the package in your Python application using the import statement followed by the full namespace.

For example, if you compile your MATLAB functions and specify the package name as addmatrix with no namespace, you import it as follows:

import addmatrix

If you compile your MATLAB functions using the namespace com.mathworks.addmatrix, you import it as follows:

import com.mathworks.addmatrix

See Also

|

Topics

External Websites