# Pyodide

> Run Python code in the JavaScript environment using Pyodide.

[Pyodide](https://pyodide.org/) 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](https://micropip.pyodide.org/en/stable/project/usage.html).

GenAIScript provides a convenience layer to start pyodide python runtimes, in the [@genaiscript/plugin-pyodide](https://www.npmjs.com/package/@genaiscript/plugin-pyodide) package.

## Usage

[Section titled “Usage”](#usage)

The `python` starts an instance of Pyodide.

```js
import { python } from "@genaiscript/plugin-pyodide";


const py = await python();
```

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

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

## Globals

[Section titled “Globals”](#globals)

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

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

## Workspace file system

[Section titled “Workspace file system”](#workspace-file-system)

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

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

## Learn more about pyodide

[Section titled “Learn more about pyodide”](#learn-more-about-pyodide)

This features is powered by [Pyodide](https://pyodide.org/). For more information, please refer to the [Pyodide documentation](https://pyodide.org/docs/).