Skip to main content

A WebAssembly runtime powered by Wasmtime

Project description

wasmtime-py

Python embedding of Wasmtime

A Bytecode Alliance project

CI status Latest Version Latest Version Documentation Code Coverage

Installation

To install wasmtime-py, run this command in your terminal:

$ pip install wasmtime

The package currently supports 64-bit builds of Python 3.9+ 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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

wasmtime-29.0.0-py3-none-win_amd64.whl (5.4 MB view details)

Uploaded Python 3 Windows x86-64

wasmtime-29.0.0-py3-none-musllinux_1_2_x86_64.whl (6.9 MB view details)

Uploaded Python 3 musllinux: musl 1.2+ x86-64

wasmtime-29.0.0-py3-none-manylinux1_x86_64.whl (6.8 MB view details)

Uploaded Python 3

wasmtime-29.0.0-py3-none-macosx_11_0_arm64.whl (5.7 MB view details)

Uploaded Python 3 macOS 11.0+ ARM64

wasmtime-29.0.0-py3-none-macosx_10_13_x86_64.whl (6.3 MB view details)

Uploaded Python 3 macOS 10.13+ x86-64

wasmtime-29.0.0-py3-none-any.whl (5.4 MB view details)

Uploaded Python 3

File details

Details for the file wasmtime-29.0.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: wasmtime-29.0.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 5.4 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for wasmtime-29.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a1d12ae179a87289f1e9d9ade551b393de9fb3e05030953c7aa9454dabf8ffed
MD5 3e3c4eee2fe5d0f224c551de1c1803d1
BLAKE2b-256 088313fc6ac40d10518f9361653e9782bc74bbc6341dbfb98d6670216d431aa9

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for wasmtime-29.0.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3f69eb347bc399f43b524ef95169e1cc93567afb4ea11e6a855eb4f9b37b9cc9
MD5 33c6e00c1dee14bf2c43a7023eef0a9a
BLAKE2b-256 02b01dca5ea86f02af81eb4a551653567aa0ac7abb8d12f66141bad10c9e260a

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for wasmtime-29.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 11d3b3e3465c4dd174a22b0f23cd2e02b5e27b04961fac14fce5ac46021e4717
MD5 b56c5a500d05a4cb7bbf60d30171a9f8
BLAKE2b-256 cd023cf40814949af073e27a14460c7c579022637547846df6e5ec4c806a8394

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for wasmtime-29.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d41f36544a1767c2095e087a12c53953a75d4b8cc1788b8a830b0afde225799a
MD5 673270044c15fa4e4986118bcaf11919
BLAKE2b-256 3e41dcbcda10b1308046cfc8a3e9a1761df725f498864edd2311b83bda24ac1c

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wasmtime-29.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ee00f406116db93951a27ddeb4a507f77a8d3e949a1a749b674f67927bd9f419
MD5 d223cf9c71279d5db164b2254792f599
BLAKE2b-256 1945d286cadd49b4b731a94a963c1a9a4a62c372f4421b7d23ee4f546a1437d5

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for wasmtime-29.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 9581250dded9af61677813f67929104d750166eae4319fee8cf2dcffa715fb53
MD5 3e3d537214278bc5b8687273262aebac
BLAKE2b-256 17480659172993a2edc7cc6b0a49557b4d348613520d4a9e2c2aecaffba731ee

See more details on using hashes here.

File details

Details for the file wasmtime-29.0.0-py3-none-any.whl.

File metadata

  • Download URL: wasmtime-29.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for wasmtime-29.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d14a57e43378a848b4cb4e14e3edb9a01d5183cda232291b2dc202b1ade5a06
MD5 5f6cf7e9f188fc57ff9cbd3f98e344ea
BLAKE2b-256 74aaa92f587239c8980ffd23bcdf1c2a8f4a9beabbf59d962e27049da6acbcfa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page