Main Content

Import Python Code to Simulink Using Python Importer Wizard

This topic provides an overview of using Python® Importer to implement Python modules or packages containing multiple modules in Simulink®.

You can access Python Importer from Blockset Designer or as a standalone application. Python Importer lets you import Python modules or packages, select functions to import, and configure input and output ports. It generates a Simulink custom block library containing a MATLAB® System block for each specified function.

Python Importer workflow shown as a flowchart of three steps, create Python packages and modules, import them to Python importer and generate custom simulink library

Use Python Importer from Blockset Designer to:

  • Add test and documentation for each block.

  • Support inter-module dependencies by including a Python project folder.

  • Enable packaging and sharing of the custom blockset using Blockset Designer.

The generated blocks are added to a sublibrary in Blockset Designer. For more information on creating, managing, and publishing custom blockset in Blockset Designer, see Create a Blockset Project.

Use Python Importer as a standalone application for rapid prototyping in cases where documentation and tests are not required for the generated blocks, and sharing of the blockset is not required.

Configure System to Use Python

To call Python modules in MATLAB, you must have a compatible version of Python installed. For more information on supported Python versions and setting up your system to use Python in MATLAB, see Configure Your System to Use Python.

You can access all standard Python libraries, third-party functionality, or user-created modules from MATLAB. For more information on using Python in MATLAB, see Access Python Modules from MATLAB - Getting Started.

Use Python Importer with Blockset Designer

  1. Open the Simulink start page, pause on Blockset Designer and click Create Project.

    Blockset designer project in Simulink start page

  2. Specify a name for your project. Note that when you start typing a name for your project, a new folder is automatically created for you in the directory username\MATLAB\Projects\<foldername>. If you would like to specify an existing project folder, click Browse.

    If you create a new folder, click OK to create a new project folder. Confirm this action by clicking Yes.

  3. Now you are directed to the Blockset Designer project user interface. To open Python Importer, click the arrow on the far right of the Add section. Under External Code, click Python Importer.

    Select Python Importer from the toolbar under external code

  4. Specify the sublibrary name. The Python Importer wizard opens.

    Python Importer App introduction page

    Click Start to begin a new session. Click Load to open an existing session.

  5. On the Specify Custom Code page, specify either Python module files or Python packages containing the custom code to import. For more information on Python packages, see https://docs.python.org/3/tutorial/modules.html. You can also specify the location of Python project containing inter-module dependencies required to execute the specified function in the modules. Python Importer also supports import of functions defined within classes. For an example of importing a function defined within a class, see Integrate Python Functions Specified Within Classes Using Python Importer.

    If you make a change to the source code inside a Python module or import package after starting Python Importer, you need to unload and reload the imported modules to enable Python Importer to detect the changes. For more information on unloading and reloading Python modules, see Reload Modified User-Defined Python Module section in Call User-Defined Python Module.

    Python Importer with Files to Import and Python project folder specified

    Click Next to proceed to the Analyze page.

  6. Analyze [page specifying the analyze step where the Python importer identifies functions included in the code

    Click Next to configure import settings.

  7. The Python Importer identifies functions to import from the specified custom modules or packages. On the Import page, select the functions that you want to import. A MATLAB System object™ and MATLAB System block are created for each of the selected functions.

    Python importer lists the identified functions to import. Make your selection here

    Click Next to continue.

  8. For each selected function from the previous page, the Python Importer wizard generates a default port specification for its inputs and outputs. You can change the input port into a parameter of the generated block by changing its corresponding Scope option. Change the input and output names using Label option and data type using Type option. Specify the size and complexity of each port using Complexity and Size options respectively.

    The class constructor inputs arguments that you define in your class definition file will appear as parameters of the generated block. You can configure the label, type, complexity and size of the parameters in this step.

    Python Importer configure ports

  9. The Create Simulink Library page provides specifications of the generated library. Click Next to confirm and generate the library.

    Python Importer library specification

  10. On successful completion of the import process, the block library is generated containing a MATLAB System block for each of the selected functions.

    Python Importer with generated block library

    Do not close the library file. Click Finish to complete the import process and exit Python Importer.

  11. Blockset Designer adds each block and its supporting files to the project. The generated blocks are added to the specified sublibrary. You can add documentation and test for the generated System blocks in Blockset Designer. For more information on Blockset Designer test and documentation, see Create and Organize Block Artifacts.

    Blockset designer with generated blocks added to the sublibrary

  12. You can now publish and share the created blockset. For more information, see Publish the Created Blockset.

Use Python Importer as a Standalone Application

Use Python Importer as a standalone application to import Python functions and packages into Simulink for rapid prototyping.

  1. Use these commands to launch Python Importer from the command line.

    obj = Simulink.PythonImporter();
    obj.view();
  2. Once the Welcome page loads, click Start to begin the import process.

    Python Importer app welcome page

  3. On the Settings page, in Simulink library file namebox, specify the file-name for the library file in which the custom blocks will be created. In the Output folder box, specify the path of folder where you want the Simulink library and artifacts to be located.

    Python Importer Library

  4. On the Specify Custom Code page, specify either Python Files or Python Packages containing the custom code to import. For more information on Python packages, see https://docs.python.org/3/tutorial/modules.html. Python Importer also supports import of functions defined within classes. For an example of importing a function defined within a class, see Integrate Python Functions Specified Within Classes Using Python Importer.

    If you make a change to the source code inside a Python module or import package after starting Python Importer, you need to unload and reload the imported modules to enable Python Importer to detect the changes. For more information on unloading and reloading Python modules, see Reload Modified User-Defined Python Module section in Call User-Defined Python Module.

    Python Importer source code

  5. On the Import page, select the functions that you want to import. A MATLAB System object and MATLAB System block are created for each of the selected functions.

    Python Importer select functions to import

  6. For each selected function from the previous page, the Python Importer wizard generates a default port specification for its inputs and outputs. You can change the input port into a parameter of the generated block by changing its corresponding Scope option. Change the input and output names using Label option and data type using Type option. Specify the size and complexity of each port using Complexity and Size options, respectively.

    The class constructor inputs arguments that you define in your class definition file will appear as parameters of the generated block. You can configure the label, type, complexity and size of the parameters in this step.

    Python Importer configure ports

  7. The Create Simulink Library page provides specifications of the generated library. Click Next to confirm and generate the library.

    Python Importer library specification

  8. Python Importer successful code import window

    On successful completion of the import process, the library is generated in the specified folder along with the MATLAB System object for each of the selected functions. Refresh the library browser in Simulink to find the newly generated library. For more information on adding block library to Simulink library browser, see Add Library to Library Browser.

Specify Simulation Mode for the Generated Blocks

You can compile and simulate the blocks generated by Python Importer in Simulink using Code Generation or Interpreted Execution simulation modes. Use the Code Generation mode to improve the simulation performance of the blocks, perform simulation in rapid accelerator mode, and to enable simulation in model reference with accelerator mode. Set the simulation mode for a block using the Simulate Using parameter in its Block Parameter dialog box. For more information, see Interpreted Execution vs. Code Generation.

Simulation Mode for Python Importer generated block on block dialog

The following limitations apply for blocks in Code Generation simulation mode.

  • Methods specified within Python classes are not supported.

  • Out-of-process execution mode is not supported. Set the ExecutionMode property of pyenv function to InProcess to change your Python execution mode to in-process.

  • Signals with enumeration data type are not supported.

  • Signals with complex data type are not supported.

  • Bus signals that constitute array of bus type signals are not supported.

  • You cannot manually modify the source code of the generated MATLAB System block. Doing so makes the block invalid.

  • To preserve signal data, the data type and size of the input and output ports that you specify in Python Importer must match the data type and size of signals accepted and returned by the Python code.

  • Heterogeneous Python lists are not supported. The number and type of elements must match the data type and size that you specify in Python Importer.

  • All numpy arrays must be contiguous.

Python Importer sets Code Generation as the default simulation mode for compatible blocks. For blocks that do not support Code Generation simulation mode, the Simulate Using parameter is set as Interpreted Execution. The Interpreted Execution mode generally provides slower simulation but faster model compilation as compared to the Code Generation mode.

See Also