Skip to content

Pyodide

Pyodide is a distribution of Python for Node.js (and the browser).

Pyodide is a port of CPython to WebAssembly/Emscripten. Pyodide makes it possible to install and run Python packages in the browser with micropip.

GenAIScript provides a convinience layer to start pyodide python runtimes.

Usage

The host.python starts an instance of Pyodide.

const py = await host.python()

Each Pyodide instance has a run method that can be used to run Python code.

const result = await py.run(`print('Hello, World!')`)

Globals

You can read and write global variables in the Pyodide environment.

py.globals.set("x", 42)
const x = py.globals.get("x")
await py.run(`print(x)`)

Workspace file system

The current workspace file system is mounted on the /workspace directory in the Pyodide environment.

const result = await runtime.run(`
import os
os.listdir('/workspace')
`)
console.log({ result })

Learn more about pyodide

This features is powered by Pyodide. For more information, please refer to the Pyodide documentation.