Skip to main content
Python

Python Runtime

Execute, evaluate, and manage Python programs with Agent OS Exec.

PythonRuntime mirrors the shared Exec lifecycle: create one VM, run multiple fresh Python processes in it, then dispose it.

import { PythonRuntime } from "@rivet-dev/agentos/python";


const python = await PythonRuntime.create({
	cwd: "/workspace",
	env: { GREETING: "hello from the VM" },
});

try {
	const execution = await python.execute(`
import os
import sys

print(os.environ["GREETING"])
print("stderr is captured separately", file=sys.stderr)
	`);
	console.log(execution);

	const evaluation = await python.evaluate<{ sum: number; cwd: string }>(
		`{"sum": 2 + 40, "cwd": __import__("os").getcwd()}`,
	);
	console.log(evaluation);
} finally {
	await python.dispose();
}

The focused methods are:

  • execute(source, options) and evaluate<T>(expression, options)
  • executeFile(path, options) and executeModule(name, argv, options)
  • spawn(source, options) for a live process handle
  • install(packages, options) for Python packages
  • runtime.vm for the underlying AgentOS filesystem, shell, networking, and bindings

All execution methods accept cwd, env, argv, stdin, timeoutMs, an AbortSignal, and stdout/stderr callbacks. Shared process behavior is covered under Runtime.