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.

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:

world demo {
  import python: interface {
    print: func(s: string)
  }

  default export interface {
    run: func()
  }
}

And this demo.py script

from demo import Demo, DemoImports, imports
from wasmtime import Store

class Host(imports.Python):
    def print(self, s: str):
        print(s)

def main():
    store = Store()
    demo = Demo(store, DemoImports(Host()))
    demo.run(store)

if __name__ == '__main__':
    main()
$ wasm-tools component new demo.wat --wit demo.wit -o demo.wasm
$ python -m wasmtime.bindgen demo.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-8.0.0-py3-none-win_amd64.whl (4.6 MB view details)

Uploaded Python 3Windows x86-64

wasmtime-8.0.0-py3-none-manylinux2014_aarch64.whl (6.7 MB view details)

Uploaded Python 3

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

Uploaded Python 3

wasmtime-8.0.0-py3-none-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

wasmtime-8.0.0-py3-none-macosx_10_13_x86_64.whl (5.4 MB view details)

Uploaded Python 3macOS 10.13+ x86-64

wasmtime-8.0.0-py3-none-any.whl (4.6 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wasmtime-8.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 6298226db33d1e7041111703bad97289c2e5600c5fffdc853239a3692d3a26e0
MD5 46618e4fd355777b2ba4c8bda2042f09
BLAKE2b-256 88a403144360122561614138ff4e9e7087fd0ba127dfc9d582c3e05e70d5be90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-8.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9f06f6978be32f2409be273010079d6e80d704876c00ee531bc87b7d1b412105
MD5 1f9de403e67ae78609dd5d472d4ef2f4
BLAKE2b-256 3eb79e6b4b03596a2fccd29cd9967fed20bbb3e6c2a55b8a1ea89f141ce480ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-8.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ec7d7565310bb8ed5707fa29dab6fc6d8b8bf5ec1bc6630e43293714f46689a1
MD5 84fc350e91db42b53212aa5dbb5dfeef
BLAKE2b-256 714cd318e14f136150b5593d1c9e28c4fa8e6733c3bfb79df3178aff65659f7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-8.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a619e969816bf6ff42c90339cdd970009b4cb0bc586f8dfe0543ce09f69a8ed8
MD5 85a74f40d45b1d88eb998d57ace4ac48
BLAKE2b-256 0561bfb1d71b41f2fede68b343b6e4734724f90d33399fc2ed4c647f2927dda0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-8.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 b30d6b66aa15a1e6c76974a1df475300948d1ca334bdd328cb222d8ff9b8ef3e
MD5 1f5958c27985c76aeab5dca6fb7f906f
BLAKE2b-256 2b93adfc5a201dd85e6bd1420921538328d363fae6202c751cd16c5a7c1fd64b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wasmtime-8.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef7cd5dc8bef0fd77dfa1ce3f0130289917ce4785efe035407722a7ebca3fcb
MD5 dc8613127325eb91f1b2653ab73503bf
BLAKE2b-256 b8d5d6b53945c15f663a6528ca4667e7e2d5f334983e376e59b90111dd1c69cf

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