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.
Run the Python setup script. To install to a location other than the default, consult "Installing Python Modules" in the official Python documentation.
python setup.py install
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:
Function signatures of the deployed MATLAB functions
_init_.py
— initialization script for the Python packagesetup.py
— generated Python installer
Open a system command prompt window and navigate to the folder containing the Python package files.
Run the Python setup script. To install to a location other than the default, consult "Installing Python Modules" in the official Python documentation.
python setup.py install
Install MATLAB Runtime that matches the version of MATLAB used to compile the Python package. For details, see Install and Configure 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
- Package MATLAB Function and Deploy to Python Application
- Invoke a Packaged MATLAB Function
- Install and Configure MATLAB Runtime
- Initialize MATLAB Runtime