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
Distribute the installer to the desired location.
Run the installer.
Note the location where the installer writes the Python package files.
When the installer finishes, open a system command prompt window and navigate to the folder containing the Python package files.
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, addinstalldir
to thePYTHONPATH
environment variable. For more details, refer to the Installing Python Modules — Python 3.7.16 documentation.Add the MATLAB Runtime folders to the library path environment variable. For details, see Set MATLAB Runtime Path for Deployment.
Using Package Files
Distribute the following files to integrate a Python package in an application.
setup.py
— Python installer_init_.py
— initialization script for the Python packagepackageName
.ctf
— deployable archiveFunction signatures of the deployed MATLAB functions (for details, see Customize Code Suggestions and Completions)
Open a system command prompt window and navigate to the folder containing the Python package files.
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, addinstalldir
to thePYTHONPATH
environment variable. For more details, refer to the Installing Python Modules — Python 3.7.16 documentation.Install MATLAB Runtime that matches the version of MATLAB used to compile the Python package. For details, see Download and Install MATLAB Runtime.
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 thePackageName
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 theFunctionFiles
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:
flag.namespace
.packageName
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
compiler.build.pythonPackage
| mcc
Related Topics
- Generate Python Package and Build Python Application
- Invoke a Packaged MATLAB Function
- Download and Install MATLAB Runtime
- Initialize MATLAB Runtime