LogoFreestyle

Python

Add Python runtime to your VMs.

The Python integration adds Python 3 to your VM and provides helper methods for running Python code.

Installation

npm install @freestyle-sh/with-python freestyle-sandboxes

Usage

import { freestyle } from "freestyle-sandboxes";
import { VmPython } from "@freestyle-sh/with-python";

const { vm } = await freestyle.vms.create({
  with: {
    python: new VmPython(),
  },
});

const res = await vm.python.runCode({
  code: 'import json; print(json.dumps({"hello": "world"}))'
});

console.log(res);
// { result: { hello: 'world' }, stdout: '{"hello": "world"}\n', statusCode: 0 }

API

vm.python.runCode({ code })

Executes Python code in the runtime.

const res = await vm.python.runCode({
  code: `
import json
data = {"sum": 1 + 2, "list": [1, 2, 3]}
print(json.dumps(data))
  `
});

console.log(res.result); // { sum: 3, list: [1, 2, 3] }

Returns: Promise<RunCodeResponse>

type RunCodeResponse<Result> = {
  result: Result;      // Parsed JSON from stdout (if valid JSON)
  stdout?: string;     // Raw stdout output
  stderr?: string;     // Raw stderr output
  statusCode?: number; // Exit code
};

Example: Data Processing

import { freestyle } from "freestyle-sandboxes";
import { VmPython } from "@freestyle-sh/with-python";

const { vm } = await freestyle.vms.create({
  with: {
    python: new VmPython(),
  },
});

const res = await vm.python.runCode({
  code: `
import json

# Process some data
numbers = [1, 2, 3, 4, 5]
result = {
    "sum": sum(numbers),
    "average": sum(numbers) / len(numbers),
    "squared": [n ** 2 for n in numbers]
}

print(json.dumps(result))
  `
});

console.log(res.result);
// { sum: 15, average: 3, squared: [1, 4, 9, 16, 25] }

On this page

Freestyle AI

Documentation assistant

Experimental: AI responses may not always be accurate—please verify important details with the official documentation.

How can I help?

Ask me about Freestyle while you browse the docs.