matlab-call-python

Call Python libraries from MATLAB using py, pyrun, pyrunfile, and pyenv.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-call-python
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-call-python
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/matlab-external-language-interfaces/matlab-call-python
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-call-python

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Calling Python from MATLAB often fails with ModuleNotFoundError, unconfigured pyenv environments, type mismatches, or outdated syntax like pyargs and pyversion. This Skill provides the correct modern APIs, environment setup procedures, and structured error recovery so MATLAB-to-Python integration works reliably.

Core Features & Use Cases

  • Modern Python Interop Syntax: Enforces name=value keyword arguments, int32/int64 type wrapping, and pystringarray for string arrays instead of deprecated pyargs and pyversion.
  • Environment Setup and Recovery: Guides Python installation, virtual environment creation, pip package installation, and pyenv configuration with a structured triage procedure for ModuleNotFoundError and unresolved py.* errors.
  • Execution Mode Management: Explains InProcess vs OutOfProcess execution, module reloading, and safe use of terminate(pyenv) with user confirmation.
  • Use Case: A MATLAB script calling sklearn fails with "Unable to resolve the name 'py.sklearn'". The Skill triages the error, checks pyenv().Executable, creates a virtual environment, installs scikit-learn via pip, and reconfigures MATLAB to run the code successfully.

Quick Start

Ask the agent to call a Python library such as numpy or sklearn from MATLAB, or to fix a ModuleNotFoundError occurring in MATLAB Python code.

Frequently Asked Questions about matlab-call-python

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call Python functions from MATLAB?

Use the py. interface with syntax like py.module.function(arg, kwarg=value) for direct calls, pyrun for multi-statement scripts with persistent variables, or pyrunfile to execute a standalone .py file. Pass keyword arguments with MATLAB name=value syntax rather than the outdated pyargs.

How do I fix ModuleNotFoundError when calling Python from MATLAB?

First query pyenv to get the configured Python executable path, then install the missing package into that exact interpreter using pip. Do not trust paths shown in error tracebacks, since they point to importlib internals rather than the correct install target.

How do I configure MATLAB to use a Python virtual environment?

Create the virtual environment with your shell, then point MATLAB at it using pyenv(Version="/path/to/venv/python"). MATLAB does not use activation scripts; if Python is already loaded OutOfProcess, call terminate(pyenv) first, and if loaded InProcess you must restart MATLAB.

Why does Python receive the wrong type when MATLAB passes integers?

MATLAB passes double by default, so Python functions expecting int receive float and raise TypeError. Wrap counts, indices, and size arguments with int32(), or use int64() and uint64() for values exceeding 32 bits.

Can I use Windows Store Python with MATLAB?

No, Windows Store Python is not compatible with MATLAB because its sandboxed packaging prevents external applications from loading the Python shared library. Install Python from python.org or another distribution providing a standard shared library instead.

When should I use pyrun instead of the py. interface?

Use py.module.function for direct calls since it supports tab completion and workspace integration. Reserve pyrun for multi-statement scripts where variables persist in Python memory across calls, and pyrunfile for running standalone .py files without retained state.