Tool to package Python applications as WebAssembly components
Actively maintained. Last release 3 months ago, commits in 9 of the last 26 weeks. A safe choice.
pip install componentize-pycomponentize-py
A Bytecode Alliance project
This is a tool to convert a Python application to a WebAssembly component. It takes the following as input:
- a WIT file or directory
- the name of a WIT world defined in the above file or directory
- the name of a Python module which targets said world
- a list of directories in which to find the Python module and its dependencies
The output is a component which may be run using
e.g. wasmtime.
Getting Started
First, install Python 3.10 or later and
pip if you don't already have them. Then,
install componentize-py:
pip install componentize-py
Next, create or download the WIT world you'd like to target, e.g.:
cat >hello.wit <<EOF
package example:hello;
world hello {
export hello: func() -> string;
}
EOF
If you're using an IDE or just want to examine the bindings produced for the WIT
world, you can generate them using the bindings subcommand:
componentize-py -d hello.wit -w hello bindings hello_guest
Then, use the hello module produced by the command above to write your app:
cat >app.py <<EOF
import wit_world
class WitWorld(wit_world.WitWorld):
def hello(self) -> str:
return "Hello, World!"
EOF
And finally generate the component:
componentize-py -d hello.wit -w hello componentize --stub-wasi app -o app.wasm
To test it, you can install wasmtime-py and use it to generate host-side
bindings for the component:
[!NOTE] Due to compatibility issues with
wasmtime-pyversions beyond 38.x, this example requires version 38.0.0 or earlier: