v0.12.0 โ€ข Actively Developed โ€ข View Modules

Python โ†’ WebAssembly

Compile Python functions to WebAssembly with ease. Built in Rust for performance and reliability.

~/your-rust-project
$ cargo add waspy

Waspy is a Rust library. Add it to your project and compile Python to WebAssembly.

Pipeline

How Waspy Works

From Python source to optimized WebAssembly in four steps.

๐Ÿ

Python AST

Parse with RustPython

โ†’
โš™๏ธ

Custom IR

Intermediate Rep.

โ†’
๐Ÿ”ง

WASM Binary

wasm-encoder

โ†’
โšก

Optimized

Binaryen

The Same Pipeline, In Code

Write Python, compile it from Rust, run it from JavaScript or TypeScript.

fibonacci.py
def fibonacci(n: int) -> int:
    if n <= 1:
        return n
    a, b = 0, 1
    for i in range(2, n + 1):
        a, b = b, a + b
    return b

def factorial(n: int) -> int:
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result
1 ยท Write Python Ordinary, type-annotated Python is the compiler's input. Annotations pick the WASM value types: int/bool become i32, float becomes f64.
โ†’
main.rs
// in your Rust project,
// after `cargo add waspy`
use waspy::compile_python_file;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // parse โ†’ typed IR โ†’ codegen โ†’ Binaryen
    let wasm =
        compile_python_file("fibonacci.py", true)?;

    // a standalone module,
    // no Python interpreter inside
    std::fs::write("fibonacci.wasm", &wasm)?;
    Ok(())
}
2 ยท Compile from Rust cargo run turns fibonacci.py into fibonacci.wasm. Every top-level Python function becomes a WASM export; imports of sibling .py files link in automatically.
โ†’
run.mjs
// Node 18+
// (TypeScript works the same)
import { readFile } from "node:fs/promises";

const wasm = await readFile("./fibonacci.wasm");
const { instance } =
    await WebAssembly.instantiate(wasm);

console.log(instance.exports.fibonacci(10));
// 55
console.log(instance.exports.factorial(5));
// 120
3 ยท Run from JS/TS node run.mjs prints 55 and 120: your Python, running as WebAssembly. In a browser, swap readFile for fetch + WebAssembly.instantiateStreaming.
From a source string let wasm = waspy::compile_python_to_wasm(&source)?;

Compile Python you already hold in memory, no files involved.

From a project directory let wasm = waspy::compile_python_project("./my_project", true)?;

Compile a project directory into one module, with __main__.py entry detection and pyproject.toml config.

Waspy vs Other Python Implementations

See how Waspy compares across performance, safety, and portability.

Feature Waspy CPython MicroPython PyPy HPy
Target WebAssembly Native/VM Microcontrollers JIT Compilation C Extensions
Performance Near-native WASM Interpreted Optimized for size JIT optimized Native speed
Type Safety Compile-time Runtime only Runtime only JIT inference Runtime only
Universal Runtime Browser/Server/Edge OS dependent Hardware specific OS dependent OS dependent
Startup Time Instant Module loading Fast JIT warmup Import overhead
Memory Usage Minimal Standard Ultra-low Higher (JIT) Standard
Standard Library Basic support Complete Subset Complete Complete
Security Sandboxed WASM Full system access Hardware limited Full system access Full system access
Use Case Web apps, serverless General purpose IoT, embedded CPU-intensive apps C extension API
Full support
Partial support
Limited / None
Features

What Waspy Does

What the compiler supports today, from parser to binary. Full per-feature status lives on the modules board.

Ahead-of-Time Compilation

Python compiles to a WASM binary before it runs, with no interpreter or VM in the output.

no runtime VM

Typed Code Generation

Type annotations map to WASM value types (i32 for ints/bools, f64 for floats), checked at compile time.

annotations

Binaryen Optimization

The binary runs through Binaryen passes: dead-code elimination, inlining, constant folding.

optimizer

Small Rust API

compile_python_to_wasm() for one source, compile_python_file() for an entry file and its imports, compile_python_project() for a directory.

rust crate

Standard WASM Output

Emits plain .wasm modules that load in browsers, Node.js, wasmtime, and edge runtimes.

.wasm

Language Coverage

Classes, closures, lambdas, generators, comprehensions, exceptions, f-strings, decorators.

python 3 syntax

Project Compilation

Multi-file input with import resolution, __main__.py entry detection, pyproject.toml config.

one .wasm out

Import Analysis

User-written .py modules resolve from disk and link statically, each compiled once, with namespace calls and aliases.

user modules

Standard Library

math, json, re, datetime, random, collections, itertools, functools, and logging implemented.

stdlib subset
Under the hood

Architecture

Hover any node to see what happens at that stage.

Binaryen core::config::load_project_config analysis::project::analyze_dependencies Combine All IRModules Same as Single File from here ๐Ÿ“ Project Directory โš™๏ธ Project Config + File List ๐Ÿ”— Dependency Graph + Import Analysis For Each Python File ๐Ÿ Python Source ๐ŸŒณ AST ๐Ÿ“ฆ IRModule ๐Ÿ”€ Resolve Circular Dependencies ๐Ÿ“ฆ Resolved IRModule ๐Ÿšช IRModule + Entry Points โš™๏ธ Compilation Context WASM ๐Ÿš€ Optimized WASM