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.
First, install Python 3.10 or later and
pip if you don't already have them. Then,
install componentize-py:
pip install componentize-pyNext, create or download the WIT world you'd like to target, e.g.:
cat >hello.wit <<EOFpackage example:hello;world hello { export hello: func() -> string;}EOFIf 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_guestThen, use the hello module produced by the command above to write your app:
cat >app.py <<EOFimport wit_worldclass WitWorld(wit_world.WitWorld): def hello(self) -> str: return "Hello, World!"EOFAnd finally generate the component:
componentize-py -d hello.wit -w hello componentize --stub-wasi app -o app.wasmTo 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-py versions beyond 38.x, this example requires version 38.0.0 or earlier: pip install "wasmtime==38.0.0"
pip install wasmtime==38.0.0
python3 -m wasmtime.bindgen app.wasm --out-dir hello_hostNow we can write a simple host app using those bindings:
cat >host.py <<EOFfrom hello_host import Rootfrom wasmtime import Config, Engine, Storeconfig = Config()config.cache = Trueengine = Engine(config)store = Store(engine)hello = Root(store)print(f"component says: {hello.hello(store)}")EOFAnd finally run it:
$ python3 host.py
component says: Hello, World!See the examples directories for more examples, including various ways to run the components you've created.
Currently, the application can only import dependencies during build time, which
means any imports used at runtime must be resolved at the top level of the
application module. For example, if x is a module with a submodule named y
the following may not work:
importxclassHello(hello.Hello):
defhello(self) ->str:
returnx.y.foo()That's because importing x does not necessarily resolve y. This can be
addressed by modifying the code to import y at the top level of the file:
fromximportyclassHello(hello.Hello):
defhello(self) ->str:
returny.foo()This limitation is being tracked as issue #23.
See the issue tracker for other known issues.
See CONTRIBUTING.md for details on how to contribute to the project and build it from source.