Pyodide: Python in the Browser & Node.js

0.29.3 · active · verified Sun Apr 19

Pyodide is an open-source project that compiles the CPython interpreter to WebAssembly, enabling the full execution of Python directly within web browsers and Node.js environments without requiring a server backend. Its current stable release is 0.29.3, with frequent patch and minor updates; a significant version change to 314.0.0a1 signals alignment with upcoming Python 3.14 releases and a revised ABI stabilization strategy. Key differentiators include seamless bidirectional interoperability between JavaScript and Python, comprehensive support for a vast array of popular scientific Python libraries (such as NumPy, Pandas, and Matplotlib), and a lightweight package manager (`micropip`) that allows dynamic installation of pure Python wheels from PyPI. This enables advanced client-side data processing, interactive visualizations, and powerful educational tools to run entirely within the browser, significantly expanding Python's reach in web development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load Pyodide, execute basic Python code, install a scientific package like NumPy, and facilitate data exchange between JavaScript and Python, all within a browser environment.

import { loadPyodide } from 'pyodide';

async function main() {
  console.log('Initializing Pyodide...');
  const pyodide = await loadPyodide({
    indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.25.0/full/' // Always pin the version!
  });
  console.log('Pyodide initialized.');

  // Redirect Python stdout/stderr to JS console
  pyodide.setStdout((text) => console.log('PY_STDOUT:', text));
  pyodide.setStderr((text) => console.error('PY_STDERR:', text));

  // Run simple Python code
  console.log('Running Python code...');
  const pythonResult = await pyodide.runPython(`
    import sys
    print('Hello from Python!')
    x = 10
    y = 20
    x + y
  `);
  console.log('Python computed:', pythonResult);

  // Load a Python package and use it
  console.log('Loading NumPy...');
  await pyodide.loadPackage('numpy');
  const numpyVersion = pyodide.runPython('import numpy; numpy.__version__');
  console.log('NumPy version:', numpyVersion);

  const jsArray = [1, 2, 3, 4, 5];
  pyodide.globals.set('js_array', jsArray);

  const pythonArrayResult = await pyodide.runPython(`
    import numpy as np
    py_array = np.array(js_array)
    print(f'Python array sum: {py_array.sum()}')
    py_array.tolist()
  `);
  console.log('Python processed array:', pythonArrayResult);

  // Clean up PyProxy objects if necessary (important for preventing memory leaks)
  pyodide.destroy();
  console.log('Pyodide instance destroyed.');
}

main().catch(err => {
  console.error('An error occurred:', err);
});

view raw JSON →