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.8+ 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-20.0.0-py3-none-win_amd64.whl (4.2 MB view details)

Uploaded Python 3Windows x86-64

wasmtime-20.0.0-py3-none-manylinux2014_aarch64.whl (5.2 MB view details)

Uploaded Python 3

wasmtime-20.0.0-py3-none-manylinux1_x86_64.whl (5.2 MB view details)

Uploaded Python 3

wasmtime-20.0.0-py3-none-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

wasmtime-20.0.0-py3-none-macosx_10_13_x86_64.whl (4.9 MB view details)

Uploaded Python 3macOS 10.13+ x86-64

wasmtime-20.0.0-py3-none-any.whl (4.2 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wasmtime-20.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 8ddb958a1b6eed88e56ec4aaae9478c273bed2e4fcd3331f29fda8d9d12def3d
MD5 a93b9e6d1dfd3f91bb7bd7caf6e00124
BLAKE2b-256 6128888301b9c7a0935a08feb53b7a99774a5c78ac426f81891b3839d361a036

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-20.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 69505f8c1f42815c3784f77df5b3be9f4742ede56299a0f2d7d71083893e855d
MD5 c2e8d485d6f2753c2e49ad17c09ccb09
BLAKE2b-256 107392ca5a9327513734eab0a140cbd763ababaea1010c08f5ab5ddf8e75ed42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-20.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 137c92cfde23b9ed18a85d5d1a1bfa844f1322c3bd80bc36c3b5adc3322f4a6e
MD5 f29a960e162f0d8eca8990fd9ab8dfc8
BLAKE2b-256 6d800673a90148034f87a3a4adb8f6be5e99f1b2ee0a770a1990687463ac0e93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-20.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f9ea16cf19039c5dc11d1f9a8f024ebb0d4c6cd1cbb2d0889797326f8ba5c06
MD5 126d6fdaac4a165a1a828803eddf2dde
BLAKE2b-256 6af696d0e70908f6055c84c8972f58c46f2da6160f3abfa08a10a16710873f17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-20.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 2ae53f5d84f071f42bf7c05da3bfe4a18e0b5ad801931188bff2e5092137213e
MD5 86f435ad9d3cd5028700f497e816e38d
BLAKE2b-256 0fd2cdb246e399e1c836ea4e7750ed44ffa4e892833d8d1efa271dc3abcbd948

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-20.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for wasmtime-20.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d9806e34c7cebe3d2ff4ba58a0c7db3648d4d952a0b6821aed4c4d966a4b312
MD5 83eeae8beb3ddfdcd68e07c2d2899967
BLAKE2b-256 1470259e80d3e89d27777aaac6440ae9931d92e2bf0f07c64df9da8ec442cbea

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