$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-call-pythonCall Python libraries from MATLAB using the py. interface, pyrun, pyrunfile, or pyenv. Use when writing or executing MATLAB code that calls Python functions or passes data between MATLAB and Python. REQUIRED when triaging Python errors from MATLAB (ModuleNotFoundError, ImportErro
| 1 | # Call Python from MATLAB |
| 2 | |
| 3 | Call Python libraries from MATLAB using modern APIs, correct environment setup, |
| 4 | and structured error recovery. |
| 5 | |
| 6 | ## When to Use |
| 7 | |
| 8 | - Writing MATLAB code that calls Python via `py.*`, `pyrun`, or `pyrunfile` |
| 9 | - MATLAB code fails with `ModuleNotFoundError`, `Python is not configured`, or |
| 10 | other Python-related errors |
| 11 | - User asks to set up Python for MATLAB or create a virtual environment |
| 12 | - User asks to install a Python package for use in MATLAB |
| 13 | |
| 14 | ## When NOT to Use |
| 15 | |
| 16 | - Calling MATLAB from Python (MATLAB Engine API for Python — different direction) |
| 17 | - Building MEX files or Python C extensions |
| 18 | - Pure Python development with no MATLAB involvement |
| 19 | - MATLAB Compiler or MATLAB Production Server deployment |
| 20 | |
| 21 | ## Rules |
| 22 | |
| 23 | ### NEVER use `pyargs` |
| 24 | |
| 25 | Use MATLAB name=value syntax for Python keyword arguments. `pyargs` is outdated |
| 26 | (name=value available since R2021a). |
| 27 | |
| 28 | ```matlab |
| 29 | % TEMPLATE — not executable (requires scikit-learn) |
| 30 | rfc = py.sklearn.ensemble.RandomForestClassifier(n_estimators=int32(100), random_state=int32(42)); |
| 31 | ``` |
| 32 | |
| 33 | ### NEVER use `pyversion` |
| 34 | |
| 35 | `pyversion` is not recommended. Use `pyenv`, which supports `terminate`, `ExecutionMode`, |
| 36 | and environment switching. |
| 37 | |
| 38 | ### ALWAYS use the Bash tool for pip installs and venv creation |
| 39 | |
| 40 | Do not use MATLAB `system()`. The Bash tool provides streaming output and avoids |
| 41 | complex string escaping. |
| 42 | |
| 43 | ### ALWAYS follow recovery procedure before any pip install or venv creation |
| 44 | |
| 45 | BEFORE running pip install or creating virtual environments, follow |
| 46 | `references/environment-setup.md`. Do NOT infer Python paths from error |
| 47 | tracebacks — those paths show importlib internals, not the correct install |
| 48 | target. |
| 49 | |
| 50 | ### ALWAYS ask user confirmation before calling terminate(pyenv) |
| 51 | |
| 52 | `terminate(pyenv)` restarts the Python process and clears all Python state |
| 53 | (imported modules, variables, open connections). Always inform the user what |
| 54 | will be lost and get explicit consent before calling terminate. |
| 55 | |
| 56 | ### ALWAYS wrap with integer types when Python expects int |
| 57 | |
| 58 | MATLAB passes double by default. Python functions expecting int receive float, |
| 59 | causing TypeError. Use `int32()` for counts, indices, and size arguments. If the |
| 60 | value won't fit in 32 bits, use `int64()` or `uint64()` to prevent overflow. |
| 61 | |
| 62 | ### ALWAYS use `pystringarray` for string arrays (R2026a or later) |
| 63 | |
| 64 | When passing MATLAB string arrays to Python in R2026a or later, use |
| 65 | `pystringarray`. Requires NumPy 2.0 or later. In R2025b and earlier, or when |
| 66 | NumPy < 2.0, use `py.list(cellstr(...))`. |
| 67 | |
| 68 | ```matlab |
| 69 | % R2026a and later: |
| 70 | names = ["Alice", "Bob", "Charlie"]; |
| 71 | pyNames = pystringarray(names); |
| 72 | |
| 73 | % R2025b and earlier: |
| 74 | names = ["Alice", "Bob", "Charlie"]; |
| 75 | pyNames = py.list(cellstr(names)); |
| 76 | ``` |
| 77 | |
| 78 | ## Workflow |
| 79 | |
| 80 | When calling Python from MATLAB, follow this workflow. |
| 81 | |
| 82 | ### 1. Write the Python code using modern syntax |
| 83 | |
| 84 | ```matlab |
| 85 | result = py.module.function(arg1, arg2, kwarg1="value", kwarg2=42); |
| 86 | ``` |
| 87 | |
| 88 | Prefer `py.module.function(...)` for direct function calls — it's readable, |
| 89 | integrates with the MATLAB workspace, and supports tab completion. Reserve |
| 90 | `pyrun` for multi-statement scripts where variables and imports persist in |
| 91 | Python memory across calls (REPL-like execution). Reserve `pyrunfile` for |
| 92 | running a standalone `.py` file (equivalent to running from the command line; |
| 93 | no state carries over). |
| 94 | |
| 95 | ### 2. Run the code |
| 96 | |
| 97 | Execute via `mcp__matlab__evaluate_matlab_code` (for inline code) or |
| 98 | `mcp__matlab__run_matlab_file` (for script files). |
| 99 | |
| 100 | ### 3. If it succeeds — done |
| 101 | |
| 102 | ### 4. If it fails with a Python error — triage |
| 103 | |
| 104 | Follow `references/environment-setup.md` ONLY for environment-related errors: |
| 105 | |
| 106 | 1. `ModuleNotFoundError` |
| 107 | 2. MATLAB error about unresolved `py.*` name (e.g., "Unable to resolve the name |
| 108 | 'py.numpy'") |
| 109 | 3. `pyenv().Executable` is empty |
| 110 | 4. `py` or `pyenv` calls error stating environment is not supported |
| 111 | 5. Any other error suggesting a missing library or unconfigured environment |
| 112 | |
| 113 | For all other Python errors (ValueError, TypeError, logic errors in the Python |
| 114 | code itself) — debug the code directly. These are code bugs, not environment |
| 115 | issues. |
| 116 | |
| 117 | ## Key Functions |
| 118 | |
| 119 | | Function | Purpose | Available From | |
| 120 | |----------|---------|----------------| |
| 121 | | `pyenv` | Query/configure Python environment | R2019b | |
| 122 | | `pyrun` | Execute Python statements; variables persist across calls | R2021b | |
| 123 | | `pyrunfile` | Execute a standalone `.py` file | R2021b | |
| 124 | | `pystringarray` | Pass MATLAB |