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-36.0.0.tar.gz (147.4 kB view details)

Uploaded Source

Built Distributions

wasmtime-36.0.0-py3-none-win_arm64.whl (6.6 MB view details)

Uploaded Python 3Windows ARM64

wasmtime-36.0.0-py3-none-win_amd64.whl (7.6 MB view details)

Uploaded Python 3Windows x86-64

wasmtime-36.0.0-py3-none-musllinux_1_2_x86_64.whl (9.7 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

wasmtime-36.0.0-py3-none-manylinux2014_aarch64.whl (8.7 MB view details)

Uploaded Python 3

wasmtime-36.0.0-py3-none-manylinux1_x86_64.whl (9.7 MB view details)

Uploaded Python 3

wasmtime-36.0.0-py3-none-macosx_11_0_arm64.whl (7.9 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

wasmtime-36.0.0-py3-none-macosx_10_13_x86_64.whl (9.0 MB view details)

Uploaded Python 3macOS 10.13+ x86-64

wasmtime-36.0.0-py3-none-any.whl (7.6 MB view details)

Uploaded Python 3

wasmtime-36.0.0-py3-none-android_26_x86_64.whl (9.4 MB view details)

Uploaded Android API level 26+ x86-64Python 3

wasmtime-36.0.0-py3-none-android_26_arm64_v8a.whl (8.6 MB view details)

Uploaded Android API level 26+ ARM64 v8aPython 3

File details

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

File metadata

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

File hashes

Hashes for wasmtime-36.0.0.tar.gz
Algorithm Hash digest
SHA256 e0523be4b2ebb1344a387fb4ce054a62acbcbc19671a7e7eeb166355d2f3004c
MD5 7a29a1d119bff9d5f176698431473cd6
BLAKE2b-256 6b68231035f96ba0798c0088b1ab0aa59403f5ec6b2fde2cbd5930d95acad7ff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-36.0.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 6.6 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-36.0.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 de932f23d257917652358093226315aeed21a262fdc124fff3486d5dfa62b40b
MD5 24dbc21d309d4aa21ecd9dfc1fbc56e6
BLAKE2b-256 cfd56b59a7f194940e6cdece2516e46f919c006e639a85854dd548799e3d884f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wasmtime-36.0.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 7.6 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-36.0.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 1e826960c02cbcf74cb91de41b63ff55ad6c9260e684fd4c315736b607642934
MD5 39cbcebb1460f4bdc337e03e729c2ccc
BLAKE2b-256 ba7d0b45d172feba7488e67bf85f8f4254dd8c1dc99d6c1a3b9aa410f7a2d907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5ea87199a35616848e20060f3cc00c22d4b2a919fd72bdea4c62b6503ac2bccc
MD5 91ca0ca47822952400cbff73c3f58c58
BLAKE2b-256 9d4970f70d13dedc783b5894ab1f11bd696f0f1c0e8faabc6ae422ce5eb91a67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cd6f69bb744096b9bcca17863c463a96d08af86db1ccc0d74206a33892fac887
MD5 5e0af4f9800c0a34ef79de46271e5495
BLAKE2b-256 9bfad8a27f073757bd2261455017c9c28f1d7343468931048f35c3f23a4f1b83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bca67a384a64a5d9164ebc48c106862881a6d2af67d0740ed29cc50847cbe6f5
MD5 a7380b6f7458946174cc0dcacd8276b6
BLAKE2b-256 b163cc69b41d6db6522adfc79627e7b9ad83d44797d525aae2a53f62d47d6e3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6396ca0178ceffb17464e3cf2b4beae0e0b1da5c95aa5d27ff094f2a7f41106e
MD5 d1c0e729eb7647bb0e036a4760339124
BLAKE2b-256 b452297b9982b135e43ef060f899227aa5e0b3798198f17ee1e0f806bd247fcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 a8ff3bf20f84f3f092ef7bf216afa2706e496ef3e190fb90b88b13b9529ed035
MD5 2e44ccc849e12fc204223eeebd8b79b2
BLAKE2b-256 9f65abf6a435bba624b005d5d6ea3fb9285be662aa3ac7a80f97aeb29b1e1d60

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wasmtime-36.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1e41c3f832752982aced040168b4cea1144c39a5ed43882791b2e2975145ee3
MD5 b4d231d9965015581863f53c21a12364
BLAKE2b-256 a4855654a332a7f51108a672898414c2009cf362b268b514bae02b3f106c02b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-android_26_x86_64.whl
Algorithm Hash digest
SHA256 72c2186c11b255ea8c6874320973712eceb384f5d68594de4348e2a169437af0
MD5 beb79f44d5b1b64d5b61c395e496da04
BLAKE2b-256 12a960bdd9fe50daba4562b4d11e8cd156e7774ccc69fc87f1cd47ec3bd5aca3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wasmtime-36.0.0-py3-none-android_26_arm64_v8a.whl
Algorithm Hash digest
SHA256 eab97e2a54fcfd074dcbddf38d7397a0411914802e072f4fd30ed41c2a5604fe
MD5 ee7c350f6a5f81255d42113830ad5436
BLAKE2b-256 7a3b33fccbb066bcc84f195d0f6c6b4e038f4df8bf9596d24478fa143a7e4f27

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