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 Windows, macOS, and Linux, for x86_64 and arm64 architectures.

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 Distribution

wasmtime-34.0.0.tar.gz (146.9 kB view details)

Uploaded Source

Built Distributions

wasmtime-34.0.0-py3-none-win_arm64.whl (5.8 MB view details)

Uploaded Python 3Windows ARM64

wasmtime-34.0.0-py3-none-win_amd64.whl (6.5 MB view details)

Uploaded Python 3Windows x86-64

wasmtime-34.0.0-py3-none-musllinux_1_2_x86_64.whl (8.3 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

wasmtime-34.0.0-py3-none-manylinux2014_aarch64.whl (7.6 MB view details)

Uploaded Python 3

wasmtime-34.0.0-py3-none-manylinux1_x86_64.whl (8.3 MB view details)

Uploaded Python 3

wasmtime-34.0.0-py3-none-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

wasmtime-34.0.0-py3-none-macosx_10_13_x86_64.whl (7.8 MB view details)

Uploaded Python 3macOS 10.13+ x86-64

wasmtime-34.0.0-py3-none-any.whl (6.5 MB view details)

Uploaded Python 3

wasmtime-34.0.0-py3-none-android_26_x86_64.whl (8.1 MB view details)

Uploaded Android API level 26+ x86-64Python 3

wasmtime-34.0.0-py3-none-android_26_arm64_v8a.whl (7.6 MB view details)

Uploaded Android API level 26+ ARM64 v8aPython 3

File details

Details for the file wasmtime-34.0.0.tar.gz.

File metadata

  • Download URL: wasmtime-34.0.0.tar.gz
  • Upload date:
  • Size: 146.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for wasmtime-34.0.0.tar.gz
Algorithm Hash digest
SHA256 c8cdf5ca3a186a1687f32828ae24e21a6b316629d41bf85be65edb53dd56913c
MD5 c194e612aab074cdff1838e7fb8f64ac
BLAKE2b-256 9ec4ad0acef9c9206150dfad3c72c718bc782e03daca961e635980ac3385f27e

See more details on using hashes here.

File details

Details for the file wasmtime-34.0.0-py3-none-win_arm64.whl.

File metadata

  • Download URL: wasmtime-34.0.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 5.8 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for wasmtime-34.0.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 4679f52298b206cf8f5d544ca0ec19800df04d606f8e0517b63b0c5d62bda85d
MD5 fb0d7f9d5209b2d1b4a86ee7b022045e
BLAKE2b-256 e05f6f6bbb293bcb6180b383af024d862cf8d61772ca468f517dc0d152915f55

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wasmtime-34.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 f8cac47169262e7b0e322c457b9aa82fd1ed274d95542ade68248aeee0c7f7de
MD5 5b8ab24cd3befd0a37834619c6c04941
BLAKE2b-256 022531ff7e417411e9ae8637cfce7877ea02a176aea8948fffd24f638c55c080

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 62e1b8854ede084aaa1bab2b0b6914c9a2c935072146431f48e9319a8818c666
MD5 2346193389b4cc17096ec759311411e7
BLAKE2b-256 b1c9024f2028f2c654e80edd17cda75ef707526ef7dd4dcd202d0781340ed387

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 39363cebe275fc65c94fff557ca4d1e4f3e2af7a3c309abf506a0bdb2c0dd770
MD5 0851a6f8d8c6aeda532712855586c6e0
BLAKE2b-256 8c3d11c63d4d998be69ce369326c6fc496ef2b566264112bd53cc5e761abbb8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 735a30599f98d92d9d631615651ef4b964f3e32f5f0ab80e4e68c00633789726
MD5 bf97a49ed58c3b5d3f29b06867917336
BLAKE2b-256 024991feda2f5a0c69a80fa0b72d47493316b3c11bc9b840c50288ab762facf5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 31fc042c5acfa1b93a12292f508d9bcfed2caed118b20053d573afaffdd4eda8
MD5 b79f5247d25a89eb68842e0a8ea2e5a0
BLAKE2b-256 8081185fce9b6ffc92ed051c9440a070a0a2735dd7493c1275f6b3841a32c097

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 ba7dbcc8633ee6edaa93d8be7e45c613ae55fa020a9dfa032f97dca5f92ea6cf
MD5 4f76435ea9005c34048c4d82b933071c
BLAKE2b-256 c92b381a3e09afc595c6669102f91ebfb21918978e3770ccb31bf1cf61f0bbee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-34.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for wasmtime-34.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad7aa9965ff597ee821e57c15f83a2306626237e6394410ada544afc014522fa
MD5 9155340af65762d27584146771256105
BLAKE2b-256 742942a6556c266d0e3be65ec9ab5b01052ab128b4c397297aa6788bf560f62b

See more details on using hashes here.

File details

Details for the file wasmtime-34.0.0-py3-none-android_26_x86_64.whl.

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-android_26_x86_64.whl
Algorithm Hash digest
SHA256 6b3bcf9621e840bff6f926d424e6f4c864133e0c3d6254adb963dc2f418cedd8
MD5 c620ccabc5511df5494b74da6b2e8dfb
BLAKE2b-256 43074769c6decfa3ecd00cffe38b9800993d1888addfffc054a6f90ea5730e97

See more details on using hashes here.

File details

Details for the file wasmtime-34.0.0-py3-none-android_26_arm64_v8a.whl.

File metadata

File hashes

Hashes for wasmtime-34.0.0-py3-none-android_26_arm64_v8a.whl
Algorithm Hash digest
SHA256 d092db8a322a5e59579b4e52f9cf6f0960f71da9c08eeaad6dfb9489542f539b
MD5 c3efc434f5646241e72245efea694842
BLAKE2b-256 e76f102e1badb58f8a5c9907df48d7e83932992f2d9a7390b58a46b791784682

See more details on using hashes here.

Supported by

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