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
verify that you have a version of python installed that is compatible with matlab compiler sdk™. for details, see .
end users must have an installation of matlab runtime to run the application. for testing purposes, you can use an installation of matlab instead of matlab runtime. for details, see install and configure matlab runtime.
create function in matlab
in matlab, examine the matlab code that you want packaged. for this example, create a function named
makesqr.m
that contains the following code:
function y = makesqr(x)
y = magic(x);
at the matlab command prompt, enter makesqr(5)
.
the output is a 5-by-5 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.
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
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.
in the exported functions section of the toolstrip, click .
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.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.
choose a name for your package. the library name field is automatically populated with
makesqr
as the name of the package. rename it asmagicsquarepkg
. for more information on naming requirements for the python package, see .
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 = 5
, save the file, and
return to the library compiler app.
caution
you must edit the matlab sample file to output your desired result. generated target language sample files use the same inputs and outputs as the sample matlab file.
the compiler converts this matlab code to python code during packaging. for more information and limitations, see .
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 .
additional installer options — default installation path for the generated installer and custom logo selection. see .
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 .
files installed for your end user — files that are installed with your application.
see .
package the application
when you are finished selecting your packaging options, save your library compiler project and generate the packaged application.
click package.
in the save project dialog box, specify the location to save the project.
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
, andfor_testing
.for more information about the files generated in these folders, see .
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. if you have already created a package using the library compiler, see install and run matlab generated python application.
save the following code in a sample file named
makesqrsample1.m
:x = 5; y = makesqr(x);
build the python package using the
compiler.build.pythonpackage
function and themakesqr.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',... 'verbose','on');
you can specify additional options in the
compiler.build
command by using name-value arguments. for details, see .the
compiler.build.results
objectbuildresults
contains information on the build type, generated files, included support packages, and build options.the function generates the following files within a folder named
magicsquarepkgpythonpackage
in your current working directory:samples\makesqrsample1.py
— python sample application file.gettingstarted.html
— html file that contains information on integrating your package.includedsupportpackages.txt
— text file that lists all support files included in the 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 .pyproject.toml
— configuration file that contains build system requirements and information, which are used bypip
to build the package. for details, see .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 .
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 application.
copy and paste the generated python file
makesqrsample1.py
from thesamples
folder into the folder that contains thesetup.py
file.the program listing for
makesqrsample1.py
is shown below.#!/usr/bin/env python """ sample script that uses the magicsquarepkg package created using matlab compiler sdk. refer to the matlab compiler sdk documentation for more information. """ import magicsquarepkg # import the matlab module only after you have imported # matlab compiler sdk generated python modules. import matlab try: my_magicsquarepkg = magicsquarepkg.initialize() except exception as e: print('error initializing magicsquarepkg package\\n:{}'.format(e)) exit(1) try: xin = matlab.double([5], size=(1, 1)) yout = my_magicsquarepkg.makesqr(xin) print(yout, sep='\\n') except exception as e: print('error occurred during program execution\\n:{}'.format(e)) my_magicsquarepkg.terminate()
at the system command prompt, navigate to the folder that contains
makesqrsample1.py
andsetup.py
.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.
run the application at the system command prompt.
python makesqrsample1.py
if you used sample matlab code in the packaging steps, this application returns the same output as the sample code.
[[17.0,24.0,1.0,8.0,15.0],[23.0,5.0,7.0,14.0,16.0],[4.0,6.0,13.0,20.0,22.0], [10.0,12.0,19.0,21.0,3.0],[11.0,18.0,25.0,2.0,9.0]]
note
on macos, you must use the
mwpython
script instead ofpython
. for example,mwpython makesqrsample1.py
.the
mwpython
script is located in the
folder, wherematlabroot
/binmatlabroot
is the location of your matlab or matlab runtime installation.
see also
| | | |