pyrun — Execute Python code in the session's persistent Python workspace.
pyrun(code, name, value, ...) executes without requesting outputs; result = pyrun(code, outputNames, name, value, ...) returns named Python values.
Syntax
pyrun(code, name, value, ...)
result = pyrun(code, outputs, name, value, ...)Inputs
code- Type
StringScalar- Required
- Yes
- Default
- —
- Description
- Python source for the persistent workspace.
inputs- Type
Any- Required
- Variadic
- Default
- —
- Description
- Named Python input pairs.
code- Type
StringScalar- Required
- Yes
- Default
- —
- Description
- Python source.
outputs- Type
Any- Required
- Yes
- Default
- —
- Description
- Python output name or names, required when an output is requested.
Returns
result- Type
Any- Description
- Python result converted through the session foreign boundary.
Returned values from pyrun depend on how many outputs the caller requests.
Errors
RunMat:Foreign:UnsupportedOnWasm- When
- The current host has no explicitly provided CPython capability.
- Message
Python interoperability is unavailable on this host.
RunMat:Foreign:InvalidCall- When
- Arguments do not satisfy the Python interoperability contract.
- Message
Invalid Python interoperability operation.
RunMat:Foreign:HostUnavailable- When
- A Python operation is invoked without an active runtime context.
- Message
Python operation has no active runtime context.
Execution
Source runs in a persistent workspace for this Python session. Name-value inputs are converted at the foreign boundary; requested output names may be text, a string array, or a cell of text. Python exceptions retain their type, cause, and traceback. Returned opaque Python values remain session-owned foreign objects; compatible NumPy arrays can borrow read-only host buffers in-process and copy when mutation, layout, device residency, or process isolation requires it.
Example
Exchange named values with Python
answer = pyrun('import marker; answer = marker.add2(input_value)', 'answer', 'input_value', int32(40));
answerExpected output:
answer = 42Using pyrun with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how pyrun changes the result.
Run a small pyrun example, explain the result, then change one input and compare the output.
FAQ
Does pyrun share variables between calls?⌄
Yes. It uses the session's persistent Python workspace; pyrunfile has a separate script workspace.
Related Interop functions
Native
calllib · clibgen.buildInterface · libfunctions · libisloaded · libpointer · libstruct · loadlibrary · setdatatype · unloadlibrary
Java
isjava · javaaddpath · javaArray · javaclasspath · javaMethod · javaMethodEDT · javaObject · javaObjectEDT · javarmpath · jenv · usejava
Related guides
Source you can read
Every RunMat function's source is public. Read exactly how pyrun is executed, line by line, in Rust.
- View the source for pyrun in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.