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


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-27.0.0-py3-none-win_amd64.whl (5.0 MB view details)

Uploaded Python 3 Windows x86-64

wasmtime-27.0.0-py3-none-musllinux_1_2_x86_64.whl (6.4 MB view details)

Uploaded Python 3 musllinux: musl 1.2+ x86-64

wasmtime-27.0.0-py3-none-manylinux1_x86_64.whl (6.4 MB view details)

Uploaded Python 3

wasmtime-27.0.0-py3-none-macosx_11_0_arm64.whl (5.3 MB view details)

Uploaded Python 3 macOS 11.0+ ARM64

wasmtime-27.0.0-py3-none-macosx_10_13_x86_64.whl (5.8 MB view details)

Uploaded Python 3 macOS 10.13+ x86-64

wasmtime-27.0.0-py3-none-any.whl (5.0 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wasmtime-27.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 1272d4e29fc919179d6d4df4d8e80bd064b084fcd5e7100bb23579e1469198c7
MD5 09795b9bc2ea882f604fe42cb8bd36ab
BLAKE2b-256 0082d7d7939a7b8d1383dde67954fc3b37c0deb7dbcaaef396fb116536ec0740

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-27.0.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 651afc8228e1c918b6085d19e5a51d235dd6f237ebeb00e28cd12e59c2b2c26f
MD5 d94af4c5087c7dcb79b1670deaab2521
BLAKE2b-256 60113d54f524cc0c9e006c4bb923600c41575dd9f3084b0e4c88163d25af7171

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-27.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 75cca42c89da036b7c46754ca7803e35ca07a7deaa8270926644763a4846cff3
MD5 4a4ca631483ede419ed439860680c42f
BLAKE2b-256 dd0ac088d20e6910db555c3a82a14e79810ddf59c8f89210ce183e31b926e735

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-27.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b2fb57aabb040f68808e781abe978bf03ccba7925d36b8a7da1d5482c4d14484
MD5 c739afde5839232e5cd0a443fea227bf
BLAKE2b-256 21b390e10056f92f187b0d30647b154fcd1b322055688baceca9f26e768c1672

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-27.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1c5e04a9150388b37521e34639694bdff91a040aea85bfc90a86efff4e3baa4c
MD5 c29d01a087001f1ccfafcf66f2876101
BLAKE2b-256 a3007cbd94ebeeedc3fbdfde22d129927e254045849c954560a00fddba505465

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-27.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 825b8987f2c7fda5db1014d04f39a832eb6e0cfb1133da4a287ba81e05e9f98c
MD5 7a6a0bb48ad3ee70b6615a6a3fe6ff07
BLAKE2b-256 bc700d28d5418f444a013fa489591dff8fa03ff325c6572e9401c9921b320939

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-27.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for wasmtime-27.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f09570f76195c4ada7f728fc9f725cd3fc1ac9b37e1aa42fcc2930cd44c6b93
MD5 9b007bb94143facec306a89eaa9f17ea
BLAKE2b-256 2ff90af6118c82f1058b153fa42c85edc798bbff8f3969941e4f1f8994f7731f

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page