Main Content

Initialize the MATLAB Runtime

When integrating compiled MATLAB® functions into a Python® application, your code must initialize the MATLAB Runtime:

  1. Call the initialize_runtime() function, which allows you to provide a list of startup options to the MATLAB Runtime.

  2. Use the initialize() function of each compiled package in the application. The first time the function is called, a MATLAB Runtime is loaded and started.

Provide MATLAB Runtime Startup Options


On Mac OS X, you must pass the MATLAB Runtime options to the mwpython command when starting Python. Use -mlstartup followed by a comma-separated list of MATLAB Runtime options. MATLAB Runtime options passed to initialize_runtime() are ignored.

The MATLAB Runtime has two startup options that you can specify:

  • -nojvm — disable the Java® Virtual Machine, which is enabled by default. This can help improve the MATLAB Runtime performance.

  • -nodisplay — on Linux®, run the MATLAB Runtime without display functionality.

You specify these options before you initialize the compiled MATLAB functions. You do so by calling the initialize_runtime() method of a generated Python package with the MATLAB Runtime options. The list of MATLAB Runtime options is passed as a list of strings. For example, to start the MATLAB Runtime for the package addmatrix with no display and no Java Virtual Machine:

import addmatrix

addmatrix.initialize_runtime(['-nojvm', '-nodisplay'])

If your application uses multiple Python packages, you call initialize_runtime() from only one package. The first call sets the run-time options for the MATLAB Runtime session. Any subsequent calls are ignored.

Start MATLAB Runtime with Compiled MATLAB Functions

To invoke a compiled MATLAB function, load it into the MATLAB Runtime. Do this by calling the initialize() method of the generated Python package. The initialize() method returns an object that can be used to invoke the compiled MATLAB functions in the package. For example, to start the MATLAB Runtime and load the MATLAB functions in the addmatrix package, use:

import addmatrix

myAdder = addmatrix.initialize()


If the initialize_runtime() function is not called before a call to initialize() function, the MATLAB Runtime is started with no startup options.


You cannot import matlab.engine after importing your component. For more information on matlab.engine, see Start and Stop MATLAB Engine for Python.

Related Topics