Package: wasmtime
wasmtime-pyPython embedding ofWasmtimeABytecode AllianceprojectInstallationTo installwasmtime-py, run this command in your terminal:$pipinstallwasmtimeThe package currently supports 64-bit builds of Python 3.8+ on x86_64 Windows,
macOS, and Linux, as well as on arm64 macOS and Linux.Versioningwasmtime-pyfollows the Wasmtime versioning scheme, with a new major version being
released every month. As with Wasmtime itself, new major versions ofwasmtime-pycan contain changes that break code written against the previous major version.Since every installed Python package needs to agree on a single version ofwasmtime-py, to use the upper bound on the major version in the dependency
requirement should be bumped reguarly, ideally as soon as a newwasmtime-pyversion is released. To automate this process it is possible to use
thewhitequark/track-pypi-dependency-versionscript.YoWASP/runtimeis
an example of a project that automatically publishes releases on PyPI once a new
version ofwasmtime-pyis released if it passes the testsuite.UsageIn this example, we compile and instantiate a WebAssembly module and use it from Python:fromwasmtimeimportStore,Module,Instance,Func,FuncTypestore=Store()module=Module(store.engine,"""(module(func $hello (import "" "hello"))(func (export "run") (call $hello)))""")defsay_hello():print("Hello from Python!")hello=Func(store,FuncType([],[]),say_hello)instance=Instance(store,module,[hello])run=instance.exports(store)["run"]run(store)Be sure to check out theexamplesdirectory, which has other usage patterns
as well as thefull API documentationof thewasmtime-pypackage.If your WebAssembly module works this way, then you can also import the WebAssembly module
directly into Python without explicitly compiling and instantiating it yourself:# Import the custom loader for `*.wasm` filesimportwasmtime.loader# Assuming `your_wasm_file.wasm` is in the python load path...importyour_wasm_file# Now you're compiled and instantiated and ready to go!print(your_wasm_file.run())ComponentsThewasmtimepackage has initial support for running WebAssembly components in
Python with high-level bindings. WebAssembly components are defined by thecomponent modeland are a flagship feature under development for Wasmtime and
its bindings. Components enable communication between the host and WebAssembly
guests with richer types than the numerical primitives supported by core
WebAssembly. For example with a component Python can pass a string to wasm and
back.Components are represented as*.wasmbinaries in the same manner as core
WebAssembly modules. With a component binary you can generate Python bindings
with:$python-mwasmtime.bindgenthe-component.wasm--out-dirthe-bindingsAn example of using this can be done with thewasm-toolsrepository. For
example with this core wasm module atdemo.wat:(module
  (import "python" "print" (func $print (param i32 i32)))
  (memory (export "memory") 1)

  (func (export "run")
    i32.const 100   ;; base pointer of string
    i32.const 13    ;; length of string
    call $print)

  (data (i32.const 100) "Hello, world!")
)and with this*.witinterface atdemo.wit:package my:demo

world demo {
  import python: interface {
    print: func(s: string)
  }

  export run: func()
}And thisdemo.pyscriptfromdemoimportRoot,RootImports,importsfromwasmtimeimportStoreclassHost(imports.Python):defprint(self,s:str):print(s)defmain():store=Store()demo=Root(store,RootImports(Host()))demo.run(store)if__name__=='__main__':main()$wasm-toolscomponentembeddemo.witdemo.wat-odemo.wasm
$wasm-toolscomponentnewdemo.wasm-odemo.component.wasm
$python-mwasmtime.bindgendemo.component.wasm--out-dirdemo
$pythondemo.py
Hello,world!The generated packagedemohas all of the requisite exports/imports into the
component bound. Thedemopackage is additionally annotated with types to
assist with type-checking and self-documentation as much as possible.ContributingSeeCONTRIBUTING.md.