<div align="center">
<h1><code>wasmtime-py</code></h1>
<p>
<strong>Python embedding of
<a href="https://github.com/bytecodealliance/wasmtime">Wasmtime</a></strong>
</p>
<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong>
<p>
<a href="https://github.com/bytecodealliance/wasmtime-py/actions?query=workflow%3ACI">
<img src="https://github.com/bytecodealliance/wasmtime-py/workflows/CI/badge.svg" alt="CI status"/>
</a>
<a href="https://pypi.org/project/wasmtime/">
<img src="https://img.shields.io/pypi/v/wasmtime.svg" alt="Latest Version"/>
</a>
<a href="https://pypi.org/project/wasmtime/">
<img src="https://img.shields.io/pypi/pyversions/wasmtime.svg" alt="Latest Version"/>
</a>
<a href="https://bytecodealliance.github.io/wasmtime-py/">
<img src="https://img.shields.io/badge/docs-main-green" alt="Documentation"/>
</a>
<a href="https://bytecodealliance.github.io/wasmtime-py/coverage/">
<img src="https://img.shields.io/badge/coverage-main-green" alt="Code Coverage"/>
</a>
</p>
</div>
## Installation
To install `wasmtime-py`, run this command in your terminal:
```bash
$ pip install wasmtime
```
The package currently supports 64-bit builds of Python 3.9+ 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.
[whitequark/track-pypi-dependency-version]: https://github.com/whitequark/track-pypi-dependency-version
[YoWASP/runtime]: https://github.com/YoWASP/runtime
## Usage
In this example, we compile and instantiate a WebAssembly module and use it from Python:
```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][apidoc] of the `wasmtime-py` package.
[`examples` directory]: https://github.com/bytecodealliance/wasmtime-py/tree/main/examples
[apidoc]: https://bytecodealliance.github.io/wasmtime-py/
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:
```python
# 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:
```sh
$ 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`:
```wasm
(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`:
```text
package my:demo;
world demo {
import python: interface {
print: func(s: string);
}
export run: func();
}
```
And this `demo.py` script
```python
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()
```
```sh
$ 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.
[component model]: https://github.com/WebAssembly/component-model
[`wasm-tools`]: https://github.com/bytecodealliance/wasm-tools
[`*.wit`]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md
## Contributing
See [`CONTRIBUTING.md`](./CONTRIBUTING.md).
Raw data
{
"_id": null,
"home_page": "https://github.com/bytecodealliance/wasmtime-py",
"name": "wasmtime",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "The Wasmtime Project Developers",
"author_email": "hello@bytecodealliance.org",
"download_url": null,
"platform": null,
"description": "<div align=\"center\">\n <h1><code>wasmtime-py</code></h1>\n\n <p>\n <strong>Python embedding of\n <a href=\"https://github.com/bytecodealliance/wasmtime\">Wasmtime</a></strong>\n </p>\n\n <strong>A <a href=\"https://bytecodealliance.org/\">Bytecode Alliance</a> project</strong>\n\n <p>\n <a href=\"https://github.com/bytecodealliance/wasmtime-py/actions?query=workflow%3ACI\">\n <img src=\"https://github.com/bytecodealliance/wasmtime-py/workflows/CI/badge.svg\" alt=\"CI status\"/>\n </a>\n <a href=\"https://pypi.org/project/wasmtime/\">\n <img src=\"https://img.shields.io/pypi/v/wasmtime.svg\" alt=\"Latest Version\"/>\n </a>\n <a href=\"https://pypi.org/project/wasmtime/\">\n <img src=\"https://img.shields.io/pypi/pyversions/wasmtime.svg\" alt=\"Latest Version\"/>\n </a>\n <a href=\"https://bytecodealliance.github.io/wasmtime-py/\">\n <img src=\"https://img.shields.io/badge/docs-main-green\" alt=\"Documentation\"/>\n </a>\n <a href=\"https://bytecodealliance.github.io/wasmtime-py/coverage/\">\n <img src=\"https://img.shields.io/badge/coverage-main-green\" alt=\"Code Coverage\"/>\n </a>\n </p>\n\n</div>\n\n## Installation\n\nTo install `wasmtime-py`, run this command in your terminal:\n\n```bash\n$ pip install wasmtime\n```\n\nThe package currently supports 64-bit builds of Python 3.9+ on x86\\_64 Windows,\nmacOS, and Linux, as well as on arm64 macOS and Linux.\n\n## Versioning\n\n`wasmtime-py` follows the Wasmtime versioning scheme, with a new major version being\nreleased every month. As with Wasmtime itself, new major versions of `wasmtime-py`\ncan contain changes that break code written against the previous major version.\n\nSince every installed Python package needs to agree on a single version of\n`wasmtime-py`, to use the upper bound on the major version in the dependency\nrequirement should be bumped reguarly, ideally as soon as a new `wasmtime-py`\nversion is released. To automate this process it is possible to use\nthe [whitequark/track-pypi-dependency-version][] script. [YoWASP/runtime][] is\nan example of a project that automatically publishes releases on PyPI once a new\nversion of `wasmtime-py` is released if it passes the testsuite.\n\n[whitequark/track-pypi-dependency-version]: https://github.com/whitequark/track-pypi-dependency-version\n[YoWASP/runtime]: https://github.com/YoWASP/runtime\n\n## Usage\n\nIn this example, we compile and instantiate a WebAssembly module and use it from Python:\n\n```python\nfrom wasmtime import Store, Module, Instance, Func, FuncType\n\nstore = Store()\nmodule = Module(store.engine, \"\"\"\n (module\n (func $hello (import \"\" \"hello\"))\n (func (export \"run\") (call $hello))\n )\n\"\"\")\n\ndef say_hello():\n print(\"Hello from Python!\")\nhello = Func(store, FuncType([], []), say_hello)\n\ninstance = Instance(store, module, [hello])\nrun = instance.exports(store)[\"run\"]\nrun(store)\n```\n\nBe sure to check out the [`examples` directory], which has other usage patterns\nas well as the [full API documentation][apidoc] of the `wasmtime-py` package.\n\n[`examples` directory]: https://github.com/bytecodealliance/wasmtime-py/tree/main/examples\n[apidoc]: https://bytecodealliance.github.io/wasmtime-py/\n\nIf your WebAssembly module works this way, then you can also import the WebAssembly module\ndirectly into Python without explicitly compiling and instantiating it yourself:\n\n```python\n# Import the custom loader for `*.wasm` files\nimport wasmtime.loader\n\n# Assuming `your_wasm_file.wasm` is in the python load path...\nimport your_wasm_file\n\n# Now you're compiled and instantiated and ready to go!\nprint(your_wasm_file.run())\n```\n\n## Components\n\nThe `wasmtime` package has initial support for running WebAssembly components in\nPython with high-level bindings. WebAssembly components are defined by the\n[component model] and are a flagship feature under development for Wasmtime and\nits bindings. Components enable communication between the host and WebAssembly\nguests with richer types than the numerical primitives supported by core\nWebAssembly. For example with a component Python can pass a string to wasm and\nback.\n\nComponents are represented as `*.wasm` binaries in the same manner as core\nWebAssembly modules. With a component binary you can generate Python bindings\nwith:\n\n```sh\n$ python -m wasmtime.bindgen the-component.wasm --out-dir the-bindings\n```\n\nAn example of using this can be done with the [`wasm-tools`] repository. For\nexample with this core wasm module at `demo.wat`:\n\n```wasm\n(module\n (import \"python\" \"print\" (func $print (param i32 i32)))\n (memory (export \"memory\") 1)\n\n (func (export \"run\")\n i32.const 100 ;; base pointer of string\n i32.const 13 ;; length of string\n call $print)\n\n (data (i32.const 100) \"Hello, world!\")\n)\n```\n\nand with this [`*.wit`] interface at `demo.wit`:\n\n```text\npackage my:demo;\n\nworld demo {\n import python: interface {\n print: func(s: string);\n }\n\n export run: func();\n}\n```\n\nAnd this `demo.py` script\n\n```python\nfrom demo import Root, RootImports, imports\nfrom wasmtime import Store\n\nclass Host(imports.Python):\n def print(self, s: str):\n print(s)\n\ndef main():\n store = Store()\n demo = Root(store, RootImports(Host()))\n demo.run(store)\n\nif __name__ == '__main__':\n main()\n```\n\n```sh\n$ wasm-tools component embed demo.wit demo.wat -o demo.wasm\n$ wasm-tools component new demo.wasm -o demo.component.wasm\n$ python -m wasmtime.bindgen demo.component.wasm --out-dir demo\n$ python demo.py\nHello, world!\n```\n\nThe generated package `demo` has all of the requisite exports/imports into the\ncomponent bound. The `demo` package is additionally annotated with types to\nassist with type-checking and self-documentation as much as possible.\n\n[component model]: https://github.com/WebAssembly/component-model\n[`wasm-tools`]: https://github.com/bytecodealliance/wasm-tools\n[`*.wit`]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md\n\n## Contributing\n\nSee [`CONTRIBUTING.md`](./CONTRIBUTING.md).\n",
"bugtrack_url": null,
"license": "Apache-2.0 WITH LLVM-exception",
"summary": "A WebAssembly runtime powered by Wasmtime",
"version": "28.0.0",
"project_urls": {
"Bug Tracker": "https://github.com/bytecodealliance/wasmtime-py/issues",
"Documentation": "https://bytecodealliance.github.io/wasmtime-py/",
"Homepage": "https://github.com/bytecodealliance/wasmtime-py",
"Source Code": "https://github.com/bytecodealliance/wasmtime-py"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a5e5d5aec44de54f71297483ac8a04b5507706edaa6aa7b06fb225d16431c0c2",
"md5": "cb5a480e65ef6c4daee9a1782d24308e",
"sha256": "34da39dac8bf80a0e150920a0cd36a33a8397796250b72ccf21560e0ae3f0953"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cb5a480e65ef6c4daee9a1782d24308e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5175004,
"upload_time": "2024-12-20T16:44:23",
"upload_time_iso_8601": "2024-12-20T16:44:23.564516Z",
"url": "https://files.pythonhosted.org/packages/a5/e5/d5aec44de54f71297483ac8a04b5507706edaa6aa7b06fb225d16431c0c2/wasmtime-28.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d89c6bfee50acca176ce451913c94915f3eb4f679d91381e1e904c26266a0516",
"md5": "7beed86a17f236037c029981f96d4606",
"sha256": "6309664eac06b562e80f35f875228e7359e7361f10da5cb81da0545c5c8f9b00"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-macosx_10_13_x86_64.whl",
"has_sig": false,
"md5_digest": "7beed86a17f236037c029981f96d4606",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5987833,
"upload_time": "2024-12-20T16:44:26",
"upload_time_iso_8601": "2024-12-20T16:44:26.200248Z",
"url": "https://files.pythonhosted.org/packages/d8/9c/6bfee50acca176ce451913c94915f3eb4f679d91381e1e904c26266a0516/wasmtime-28.0.0-py3-none-macosx_10_13_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "891c7f12d6b94cd4894cfa74312823a52ad63339c01442d414507cdfbcaaff5b",
"md5": "fc6c4369bc4da950d586de68c1b57abd",
"sha256": "85959652ba04d07b0b5d33d41c3146d556f38fc9153ea696d2eee2d22af54739"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "fc6c4369bc4da950d586de68c1b57abd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5414118,
"upload_time": "2024-12-20T16:44:29",
"upload_time_iso_8601": "2024-12-20T16:44:29.503176Z",
"url": "https://files.pythonhosted.org/packages/89/1c/7f12d6b94cd4894cfa74312823a52ad63339c01442d414507cdfbcaaff5b/wasmtime-28.0.0-py3-none-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "441a2916f63b10ebfb314e01627280181f9d7fa8b434a23e28958b351ef463c0",
"md5": "8962e7fa14073a858636a089a37c2b8c",
"sha256": "e2656429c13f408f8cb42b60dc49b96103525ca5e746e948c2cbd6052a86b047"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-manylinux1_x86_64.whl",
"has_sig": false,
"md5_digest": "8962e7fa14073a858636a089a37c2b8c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6551020,
"upload_time": "2024-12-20T16:44:33",
"upload_time_iso_8601": "2024-12-20T16:44:33.412237Z",
"url": "https://files.pythonhosted.org/packages/44/1a/2916f63b10ebfb314e01627280181f9d7fa8b434a23e28958b351ef463c0/wasmtime-28.0.0-py3-none-manylinux1_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "094713e0ab420bdbaaf2dfa495847e626b5652f420893f67567a6c606f097c58",
"md5": "ca32c4c149081cf018281852c681db30",
"sha256": "99f4b16ab33cd3877fa00ca80ecd7aebf44bbb3ac8101b064fe46fdfe09e737b"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-manylinux2014_aarch64.whl",
"has_sig": false,
"md5_digest": "ca32c4c149081cf018281852c681db30",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6125681,
"upload_time": "2024-12-20T16:44:35",
"upload_time_iso_8601": "2024-12-20T16:44:35.507816Z",
"url": "https://files.pythonhosted.org/packages/09/47/13e0ab420bdbaaf2dfa495847e626b5652f420893f67567a6c606f097c58/wasmtime-28.0.0-py3-none-manylinux2014_aarch64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c45a4afe3b1933b4f79ab5e11d3f9166e99041f4e4446eb403809b0e71f5f3dc",
"md5": "f4ac395baaca97fc49d0850e0518eccf",
"sha256": "34f91f27d2e4147f05a334b22fb2de74bbeaaf81653cf7d424cd26983a7c292e"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-musllinux_1_2_x86_64.whl",
"has_sig": false,
"md5_digest": "f4ac395baaca97fc49d0850e0518eccf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6560081,
"upload_time": "2024-12-20T16:44:37",
"upload_time_iso_8601": "2024-12-20T16:44:37.557610Z",
"url": "https://files.pythonhosted.org/packages/c4/5a/4afe3b1933b4f79ab5e11d3f9166e99041f4e4446eb403809b0e71f5f3dc/wasmtime-28.0.0-py3-none-musllinux_1_2_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8461d57e9d0a6176c5c9288ddfc46a0b00a8017f95c4856fd03d8e7ffe161ac6",
"md5": "d2876b1e808f4d4a99f784136275d763",
"sha256": "74996ca26e5726615dd8d5bd91b855d16373033687e8fe6bc60d5be222da33eb"
},
"downloads": -1,
"filename": "wasmtime-28.0.0-py3-none-win_amd64.whl",
"has_sig": false,
"md5_digest": "d2876b1e808f4d4a99f784136275d763",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5175008,
"upload_time": "2024-12-20T16:44:39",
"upload_time_iso_8601": "2024-12-20T16:44:39.850967Z",
"url": "https://files.pythonhosted.org/packages/84/61/d57e9d0a6176c5c9288ddfc46a0b00a8017f95c4856fd03d8e7ffe161ac6/wasmtime-28.0.0-py3-none-win_amd64.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-20 16:44:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bytecodealliance",
"github_project": "wasmtime-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "wasmtime"
}