A WebAssembly runtime powered by Wasmtime
Project description
Installation
To install wasmtime-py
, run this command in your terminal:
$ pip install wasmtime
The 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.
Versioning
wasmtime-py
follows the Wasmtime versioning scheme, with a new major version being
released every month. As with Wasmtime itself, new major versions of wasmtime-py
can contain changes that break code written against the previous major version.
Since every installed Python package needs to agree on a single version of
wasmtime-py
, to use the upper bound on the major version in the dependency
requirement should be bumped reguarly, ideally as soon as a new wasmtime-py
version is released. To automate this process it is possible to use
the whitequark/track-pypi-dependency-version script. YoWASP/runtime is
an example of a project that automatically publishes releases on PyPI once a new
version of wasmtime-py
is released if it passes the testsuite.
Usage
In this example, we compile and instantiate a WebAssembly module and use it from Python:
from wasmtime import Store, Module, Instance, Func, FuncType
store = Store()
module = Module(store.engine, """
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
""")
def say_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 the examples
directory, which has other usage patterns
as well as the full API documentation of the wasmtime-py
package.
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` files
import wasmtime.loader
# Assuming `your_wasm_file.wasm` is in the python load path...
import your_wasm_file
# Now you're compiled and instantiated and ready to go!
print(your_wasm_file.run())
Components
The wasmtime
package has initial support for running WebAssembly components in
Python with high-level bindings. WebAssembly components are defined by the
component model and 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 *.wasm
binaries in the same manner as core
WebAssembly modules. With a component binary you can generate Python bindings
with:
$ python -m wasmtime.bindgen the-component.wasm --out-dir the-bindings
An example of using this can be done with the wasm-tools
repository. For
example with this core wasm module at demo.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 *.wit
interface at demo.wit
:
package my:demo
world demo {
import python: interface {
print: func(s: string)
}
export run: func()
}
And this demo.py
script
from demo import Root, RootImports, imports
from wasmtime import Store
class Host(imports.Python):
def print(self, s: str):
print(s)
def main():
store = Store()
demo = Root(store, RootImports(Host()))
demo.run(store)
if __name__ == '__main__':
main()
$ wasm-tools component embed demo.wit demo.wat -o demo.wasm
$ wasm-tools component new demo.wasm -o demo.component.wasm
$ python -m wasmtime.bindgen demo.component.wasm --out-dir demo
$ python demo.py
Hello, world!
The generated package demo
has all of the requisite exports/imports into the
component bound. The demo
package is additionally annotated with types to
assist with type-checking and self-documentation as much as possible.
Contributing
See CONTRIBUTING.md
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for wasmtime-25.0.0-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a2a213b9179965db2d2eedececd69a37e287e902330509afae51c71a3a6842 |
|
MD5 | 99f1869a6db9b09d6b6d6bf9e917ca0c |
|
BLAKE2b-256 | 1b21c0f3e031efe5f009215975eb7dbea86b55a69c4f02420965d1f45f43865e |
Hashes for wasmtime-25.0.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53d5f614348a28aabdf80ae4f6fdfa803031af1f74ada03826fd4fd43aeee6c8 |
|
MD5 | dea6a5fd23969738f8414c0e688520a8 |
|
BLAKE2b-256 | 8278e81fee8807fd3f2a1cd32dda65207643ab83449271f38d712a6156edd09a |
Hashes for wasmtime-25.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a07445073cf36a6e5d1dc28246a897dcbdaa537ba8be8805be65422ecca297eb |
|
MD5 | 20634726e0bf8b88623a3d18f40a945f |
|
BLAKE2b-256 | b7365e83b5f7858fe1003cbb89d0d5716f628b9a00fafc3762665f06ca4ef75d |
Hashes for wasmtime-25.0.0-py3-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4364e14d44e3b7afe6a40bf608e9d0d2c40b09dece441d20f4f6e31906b729c |
|
MD5 | 377db3086d0356bfcb3d87db7fe99672 |
|
BLAKE2b-256 | 5cbaf551b829378d65d0e0494fdb492942a0ed2dc7f40681b2438a47d1f4af86 |
Hashes for wasmtime-25.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bdf1214ee3ee78a4a8a92da339f4c4c8c109e65af881b37f4adfc05d02af426 |
|
MD5 | 1b13d26ada690d4a26aa80996c5f2c05 |
|
BLAKE2b-256 | ccf0d415d6b31ba69cfca04b2d5e83fbb5fc05335323712fd91948a1a53d13be |
Hashes for wasmtime-25.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13e9a718e9d580c1738782cc19f4dcb9fb068f7e51778ea621fd664f4433525b |
|
MD5 | a64f21cf14d752f14c13b6146e5e544a |
|
BLAKE2b-256 | 3fd318db03cea097067eef07b655b2357b47da4f27ed909e97d4e3e13b25115f |
Hashes for wasmtime-25.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22aa59fc6e01deec8a6703046f82466090d5811096a3bb5c169907e36c842af1 |
|
MD5 | baf2d15955b9ba3b8191564176dfb7f9 |
|
BLAKE2b-256 | b4439828dd2b2d847348f6bbf0a4c72a1e8881d94e1b0c9e15974bbfb17f4e57 |