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.6+ 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

If you're not sure about the file name format, learn more about wheel file names.

wasmtime-12.0.0-py3-none-win_amd64.whl (4.8 MB view details)

Uploaded Python 3Windows x86-64

wasmtime-12.0.0-py3-none-manylinux2014_aarch64.whl (6.9 MB view details)

Uploaded Python 3

wasmtime-12.0.0-py3-none-manylinux1_x86_64.whl (7.0 MB view details)

Uploaded Python 3

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

Uploaded Python 3macOS 11.0+ ARM64

wasmtime-12.0.0-py3-none-macosx_10_13_x86_64.whl (5.6 MB view details)

Uploaded Python 3macOS 10.13+ x86-64

wasmtime-12.0.0-py3-none-any.whl (4.8 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wasmtime-12.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 7eee12e6bb72123b3beec757d6b48f90b09a1097b1c6dadff7013ffbc46df2fe
MD5 497a56968bd748005ae80d4d6bb32f68
BLAKE2b-256 d66a423eda29af10c74c655cbc738731682870285fd79c98257c79294f6a8c4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-12.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c72d1f9b253b97aff19375d6980e3e6380929424fe07b56364413bf54fd80049
MD5 31dd560ba3295ca20a0ffebe57e55d65
BLAKE2b-256 2c5ce27877be6a98fdcdf22d43706354a34304bd6e2167be4bc2ad1954fcd163

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-12.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fa5372d8f3e2bfcaaf7cfffb0d5efc640351889d7aed6d34711f32a85fe9b5ff
MD5 673687c9c195ffe5babdb0aef0fd0698
BLAKE2b-256 03b4d3e537827903e61399e041d82485b7f56bfbc0543f8173942b7226a2725b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-12.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2a931214e62aa3d23c61b5f5d2cc47ce10d708f4d8d02e12ea87004c0837e650
MD5 4f71bd5a58b66cb717a5bbe35dea9ba2
BLAKE2b-256 3215529990f77e8dc094d64d4ac27dffabf9d2bd19c7d0eb0e7fa5ba26e1b080

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-12.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d5e284b37204535a2817f3563102ce8a41451a5f4203db059b051640bda8bcb8
MD5 160bf9425b85965d79c6feb841857bbe
BLAKE2b-256 6fcee987e53bd4d71a27c905acb3ee12a4ecc9496a1a13c7f712b0006ab6794e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-12.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for wasmtime-12.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1fee5ac11726cad1f69611779e44ffcb60e912479d1a3c7c77099c3c2f068c36
MD5 433a639a9a3e98e79837f88cf9e71e17
BLAKE2b-256 a01d6c6bd39768d2f390435ad335a2424c2f645f8c6652138907d54508327e6f

See more details on using hashes here.

Supported by

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