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 a package using the Library Compiler, the app generates an installer in the for_redistribution folder that installs MATLAB Runtime and the files required to install the generated Python package. If you create a package using compiler.build.pythonPackage or mcc, you can generate an installer using the compiler.package.installer function.

You can also distribute the Python package files manually. You must install MATLAB Runtime separately.

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 Library Compiler app, you specify the package name with the Library Name field and the namespace with the Namespace field.

    The Library 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

|

Related Topics

External Websites