A Python to WebAssembly compiler written in Rust.
Waspy compiles a typed subset of Python ahead of time into a standalone WebAssembly module, with no interpreter or VM in the output. A construct either gives CPython's answer or is refused at compile time with a located error and a hint.
Try it without installing anything in the playground.
cargo add waspyOr add it to your Cargo.toml:
[dependencies]
waspy = "0.18.0"The default optimize feature runs Binaryen, a native C++ library, over the output. To build waspy for a target without a C++ toolchain, such as wasm32-unknown-unknown, see Building without Binaryen.
use waspy::compile_python_to_wasm;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let python_code = r#"
def factorial(n: int) -> int:
result = 1
i = 1
while i <= n:
result = result * i
i = i + 1
return result
"#;
let wasm = compile_python_to_wasm(python_code)?;
std::fs::write("factorial.wasm", &wasm)?;
Ok(())
}Every module-level function is exported under its own name:
// Browser or Node.js
const { instance } = await WebAssembly.instantiate(wasmBuffer);
console.log(instance.exports.factorial(5)); // 120A program that prints, reads input, or opens files does so through WASI, so it runs under wasmtime run, Node's node:wasi, or a browser shim. You can also run a Python project directly with wasmrun, which uses waspy as its Python plugin.
- Documentation: getting started, the library API and options, the supported Python subset, numbers, limitations, and running compiled modules
- Modules: the development board, feature by feature
- What a program looks like: one whole program, unedited
- Playground: compile and run Python in the browser
- Changelog
- API docs on docs.rs
Contributions are welcome! See CONTRIBUTING.md for building, running the examples, and testing. If you find a program that compiles and answers differently from CPython, please open an issue.

