wasmtime-py: Python WebAssembly Runtime Powered by Wasmtime

What is wasmtime‑py?

WebAssembly (WASM) is a binary instruction format designed for speed, safety, and portability. It lets you compile code written in languages like C, Rust, or AssemblyScript to a sandboxed module that runs in a variety of runtimes.

wasmtime‑py is a Python wrapper around the Wasmtime engine, an industry‑grade execution engine developed by the Bytecode Alliance. It turns Wasmtime into a first‑class Python package, letting developers compile, instantiate, and run WASM modules directly from their Python code.

Key features:

  • Zero‑dependancy – the wrapper ships a statically linked Wasmtime binary; no external C library is required.
  • Python‑friendly API – expose WASM modules as normal Python objects with clear error handling.
  • Component support – the newer "Component Model" can be used just like core modules.
  • Cross‑platform – works on Windows, macOS, and Linux for x86_64 and arm64.

Why use wasmtime‑py?

If you:

  1. Need to run a small performance‑critical computation written in Rust or C.
  2. Want to sandbox third‑party code without restarting your Python process.
  3. Are building a polyglot server that mixes Python and WASM modules.
  4. Seek a minimal runtime that is easy to install via pip.

then wasmtime‑py is a perfect fit. Unlike heavier runtimes that bundle a VM and many runtime crates, this library keeps its footprint small (under 5 MiB on disk) while providing a familiar Python API.

Installation

pip install wasmtime

The package automatically detects your platform and downloads the matching Wasmtime binary. For custom builds or to use a pre‑built wheel from a specific release you can pin the version:

pip install "wasmtime==0.30.0"

Supported Python versions: 3.9+.

Quick Start – Compiling and Running a Module

from wasmtime import Store, Module, Instance, Func, FuncType

store = Store()
module = Module(store.engine, """
(module
  (func $hello (import \"\" \"hello\"))
  (func (export \"run\") (call $hello))
)"")

# Define the imported host function

def say_hello():
    print("Hello from Python!")

hello = Func(store, FuncType([], []), say_hello)

# Instantiate the module, passing the host function
instance = Instance(store, module, [hello])
run = instance.exports(store)["run"]
run(store)

Output:

Hello from Python!

Importing Pre‑Compiled WASM Modules

WASMTIME‑PY can automatically load a compiled module that is available as a Python module. The loader works for files that follow the PEP 420 namespace package convention.

# Assuming you have compiled my_module.wasm and placed it next to this script
import wasmtime.loader  # registers the custom loader
import my_module

my_module.run()

Behind the scenes the loader compiles the .wasm file on first import and caches the resulting Instance.

Using Components

The Component Model extends the core WebAssembly idea with richer imports/exports and type safety. It is fully supported:

from wasmtime import Store, Component

store = Store()
component = Component(store.engine, """
(component "hello"
  (import "hello" (func (type 0)))
  (export "hello" (func 0)))
)"")

# Component instances take a list of host functions matching the component's import type
instance = component.instantiate(store, [hello])
instance.exports(store)["hello"](store)

Contributing

wasmtime‑py is open source under the Apache‑2.0 license. Contributions are welcome:

  1. Fork the repo.
  2. Create a feature branch.
  3. Run the test suite with pytest.
  4. Pull request with clear description.
  5. Review follows the standard GitHub workflow.

Check out the CONTRIBUTING.md for guidelines and coding style.

Future Plans

  • Incremental API changes tied to core Wasmtime releases.
  • Support for WASI (WebAssembly System Interface) to run more complex apps.
  • Better integration with popular Python async frameworks.
  • New examples targeting data science and machine learning workloads.

Wrap‑up

wasmtime‑py gives you a powerful, lightweight tool to embed WebAssembly logic in Python applications. Whether you’re prototyping a new language feature, sandboxing untrusted code, or building a polyglot microservice, this library makes it simple, with zero runtime overhead beyond the Wasmtime engine. Give it a try, explore the extensive documentation, and contribute to the growing ecosystem of WebAssembly for Python.

Original Article: View Original

Share this article